|
| 1 | +--- |
| 2 | +title: 'Arduino UNO R4 WiFi Qwiic Connector' |
| 3 | +description: 'Learn how use the Qwiic connector on the Arduino UNO R4 WiFi.' |
| 4 | +tags: |
| 5 | + - Qwiic |
| 6 | + - STEMMA QT |
| 7 | + - I2C |
| 8 | +author: 'Jacob Hylén' |
| 9 | +--- |
| 10 | + |
| 11 | +In this tutorial you will learn how to use the Qwiic connector on the Arduino UNO R4 WiFi. |
| 12 | + |
| 13 | +## Goals |
| 14 | + |
| 15 | +We will walk through the concept of I2C, and how it relates to the Qwiic ecosystem. |
| 16 | + |
| 17 | +You'll learn what Qwiic is, as well as how to set it up and get started making your own Qwiic system with the Arduino UNO R4 WiFi. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Hardware & Software Needed |
| 22 | + |
| 23 | +- Arduino IDE ([online](https://create.arduino.cc/) or [offline](https://www.arduino.cc/en/main/software)) |
| 24 | +- [Arduino UNO R4 WiFi](https://store.arduino.cc/uno-r4-wifi) |
| 25 | +- Qwiic module |
| 26 | +- Qwiic cable |
| 27 | + |
| 28 | +## I2C |
| 29 | + |
| 30 | +We'll go over the conceptual level of how I2C works, if you want to skip ahead you can [click here](#qwiic) to jump directly to the Qwiic section |
| 31 | + |
| 32 | +I2C is a communication protocol that lets you connect multiple I2C compatible devices in a line, using only two pins that are referred to as SCL and SDA. |
| 33 | + |
| 34 | +Each device in the I2C line is functionally independent from the controller, but will respond with information when prompted by the controller. |
| 35 | + |
| 36 | +All devices on the line have an address that is represented by 7 bits. With 7 bits you can create 128 unique combinations, and that means that the theoretical limit of the amount of I2C devices a single line can support is 128. |
| 37 | + |
| 38 | +An I2C message on a lower bit-level looks something like this: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- The controller sends out instructions through the I2C bus on the data pin (SDA), and the instructions are prefaced with the address, so that only the correct device listens. |
| 43 | +- Then there is a bit signifying whether the controller wants to read or write. |
| 44 | +- Every message needs to be acknowledged, to combat unexpected results, once the receiver has acknowledged the previous information it lets the controller know, so it can move on to the next set of bits. |
| 45 | +- 8 bits of data |
| 46 | +- Another acknowledgement bit |
| 47 | +- 8 bits of data |
| 48 | +- Another acknowledgement bit |
| 49 | + |
| 50 | +But how does the controller and peripherals know where the address, messages, and so on starts and ends? That's what the SCL wire is for. It synchronises the clock of the controller with the devices, ensuring that they all move to the next instruction at the same time. |
| 51 | + |
| 52 | +However, you are nearly never going to *actually* need to consider any of this, in the Arduino ecosystem we have the [Wire library](https://www.arduino.cc/reference/en/language/functions/communication/wire/) that handles everything for you. |
| 53 | + |
| 54 | +But what does all of this have to do with Qwiic? |
| 55 | + |
| 56 | +## Qwiic |
| 57 | +Qwiic is an ecosystem of breakout-modules and development boards with a so called Qwiic connector. The Arduino UNO R4 WiFi has one, for example. The Qwiic ecosystem combines the flexibility of I2C with the ease of use of pre-bundled cables making it incredibly easy to create lines of I2C devices, by collecting all the pins to get up and running with a large network of I2C devices in a single cable. |
| 58 | + |
| 59 | +Effectively this means that the wiring of your Qwiic devices is as simple as plugging them in in a series, and you're done. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Arduino UNO R4 WiFi Implementation |
| 64 | + |
| 65 | +The Arduino UNO R4 WiFi has two I2C buses, and the Qwiic connector is connected to the secondary one. |
| 66 | + |
| 67 | +Practically, this means that if you intend to use the Qwiic connector, when you're writing the code for your sketches, you cannot use the primary `Wire` object for I2C that you normally would, but you instead need to use the secondary one, `Wire1`. |
| 68 | + |
| 69 | +This *can* get problematic in some instances depending on the library developed for the breakout module you are using, as if there is no adequate solution for selecting the `Wire1` object when initialising the library you may need to alter the library code slightly, or write your own. |
| 70 | + |
| 71 | +In most cases, however, you will be able to select the `Wire1` object when initialising the library in a fashion similar to this: |
| 72 | + |
| 73 | +```arduino |
| 74 | +Wire1.begin(); |
| 75 | +libraryName.begin( Wire1 ); |
| 76 | +``` |
| 77 | + |
| 78 | +For example, when using SparkFuns AHT20 library, a sketch that read the temperature and prints it to the serial monitor could look like this: |
| 79 | +```arduino |
| 80 | +#include <Wire.h> |
| 81 | +#include <SparkFun_Qwiic_Humidity_AHT20.h> |
| 82 | +
|
| 83 | +AHT20 humiditySensor; |
| 84 | +
|
| 85 | +void setup() { |
| 86 | + Serial.begin(9600); |
| 87 | + Wire1.begin(); //Join I2C bus |
| 88 | + //Check if the AHT20 will acknowledge |
| 89 | + if (humiditySensor.begin(Wire1) == false) { |
| 90 | + Serial.println("AHT20 not detected. Please check wiring. Freezing."); |
| 91 | + while (1) |
| 92 | + ; |
| 93 | + } |
| 94 | + Serial.println("AHT20 acknowledged."); |
| 95 | +} |
| 96 | +
|
| 97 | +void loop() { |
| 98 | +
|
| 99 | +
|
| 100 | + float temperature = humiditySensor.getTemperature(); |
| 101 | + Serial.print("Temperature: "); |
| 102 | + Serial.print(temperature, 2); |
| 103 | + Serial.println(" C"); |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Summary |
| 108 | +In this tutorial, we have gone over the Qwiic connector on the Arduino UNO R4 WiFi. We've learned about how I2C works, and how that relates to Qwiic. We've also learned how the Arduino UNO R4 WiFis Qwiic connector is configured and how to make use of it to conveniently build large I2C networks of nodes for your interactive projects with minimal wiring. |
0 commit comments