You have a microcontroller and two identical sensors, meaning that they have the same I2C address. Without a multiplexer or the possibility to configure the I2C address of the sensor you cannot attach them to the same I2C bus. However, the Arduino Uno R4 Wifi provides multiple I2C buses on the board. One I2C bus is available on the pin header and a second one over the Qwiic connector.
The IngredientsIn this example we use an Arduino Uno R4 Wifi. As sensor boards, we have one Sensirion SCD41 CO2 sensor evaluation board to connect to the pin headers and one Adafruit SCD41 board to connect with a Qwiic cable. You can use any two sensors, whether they are the same product or different sensors with the same I2C address.
General working principleThe two SCD41 sensors we want to connect have an I2C address of 0x62, which cannot be changed. Therefore, to communicate with both sensors from the microcontroller, we will use a separate I2C bus for each. The I2C bus attached to the Qwiic connector and the I2C bus you can connect two over pins are two separate I2C buses. Thus, we can have a sensor with the same address once on each of them. On the software side we use the standard Wire library. You can use the preconfigured "Wire" (Pin connector) and "Wire1" (Qwiic connector) instances. As a final step, we will create two instances of our sensor driver class and initialize one with the standard I2C bus and the other with the custom I2C bus.
WiringPull Ups
Having pull-up resistors in place on the I2C data (SDA) and clock (SCL) lines is important for a good signal quality and robust communication. You can read more about it on I2C Pull-Up Resistors Intro.
The Arduino Uno R4 WIFI provides no pull-ups on the board for the I2C bus on the pin connector. The SEK SCD41 board we are going to connect to this I2C bus has no pull-ups built in neither. Thus, we need to connect a pull-up resistor between SDA and VDD and SCL and VDD. Two 8.26kOhm resistors were used in this example and the wiring was done using a breadboard so that no soldering was needed.
The second I2C bus is on the Qwiic. We use here the breakout board from Adafruit. This board includes 10K pull-up on SDA and SCL lines, thus no additiona Pull-Up resistors need to be connected for this sensor.
Wiring diagram
The list below describes the wiring, where R.1 and R.2 are resistors with a value of 8.26kOhm.
- SEK-SCD41 Pin 1 to R.1 (SCL, yellow)
- R.1 to Arduino Pin SCL (yellow)
- R.1 to Arduino 3V3
- SEK-SCD41 Pin 2 to Arduino GND
- SEK-SCD41 Pin 3 to Arduino 3V3
- SEK-SCD41 Pin 4 to R.2 (SDA, green)
- R.2 to Arduino Pin 14 (green)
- R.2 to Arduino 3V3
- Adafruit SCD41 to Arduino Qwiic
First, you need to include the Wire library:
#include <Wire.h>
For the Arduino Uno R4, this library defines two I2C buses. The first, "Wire" is configured for the SDA/SCL pins on the pin header, where "Wire1" is configured for the Qwiic connector. We can use those two instances without any further configuration. You just need to initialize them:
Wire.begin();
Wire1.begin();
Then, the code sending the commands to the sensors over the I2C bus needs to know which bus to use for which sensor. Thus, you need to configure the sensor instances accordingly. First, create a driver instance per sensor. Their scope should be global, such that those can be referred to from within `setup()` and `loop()`.
SensirionI2cScd4x sensorOnPins;
SensirionI2cScd4x sensorOnQwiic;
Next, in the `setup()` function, assign the I2C buses to the sensors:
sensorOnPins.begin(Wire, SCD41_I2C_ADDR_62);
sensorOnQwiic.begin(Wire1, SCD41_I2C_ADDR_62);
Look out that you really have `Wire1` assigned for `sensorOnQwiic`.
You can now send any I2C command to the sensor, such as initiating the measurement and retrieving values.
sensorOnPins.startMeasurement();
sensorOnQwiic.startMeasurement();
...
Example sketch
You find a complete example sketch for Arudino Uno R4 Wifi on GitHub arduino-i2c-different-buses-example. Look out for the sketch specific to Aruduino Uno R4 named exampleArduinoUnoR4.
Other Arduino BoardsDocumentation for Arduino boards can be found under Arduino Wire Library.
Note that not all boards support multiple I2C buses and that it is recommended to use a custom pull-up resistor configuration, as the built in resistors are likely not strong enough (resistor value is too big). Read more about it on I2C Pull-Up Resistors Intro.
Comments
Please log in or sign up to comment.