Sub topic 10
Connecting Multiple Devices to One I2C Bus
This chapter assumes that you know basic Arduino sketches, the Serial Monitor, voltage and resistors, and binary and hexadecimal numbers. It also assumes that you understand SDA, SCL, pull-up resistors, and 7-bit I2C addresses.
Sharing SDA and SCL
I2C devices connect in parallel. Every device shares the same SDA data line and SCL clock line. The controller, usually the Arduino, selects one target by sending its address. Other devices ignore that transaction.
Each device must have a unique address on the same bus. For example, a temperature sensor at 0x48 and an EEPROM at 0x50 can share the bus without special software.
Connect all device grounds together. Keep SDA connected to SDA and SCL connected to SCL. Do not connect a device's power pin to a voltage that it cannot tolerate.
Address conflicts
Some devices use fixed addresses. Others provide address-select pins or solder jumpers. Changing an address pin changes the device's address, allowing two or more identical devices to share one bus.
| Situation | Suitable solution |
|---|---|
| Devices have different addresses | Connect them directly in parallel |
| Two devices support address pins | Set different address pins |
| Devices have the same fixed address | Use an I2C multiplexer |
| Devices use different logic voltages | Use a suitable level shifter |
An I2C multiplexer, such as the TCA9548A, divides one physical bus into separate channels. The controller first selects a channel by writing to the multiplexer at address 0x70. It then communicates with the device on that channel.
#include <Wire.h>
const uint8_t MUX_ADDRESS = 0x70;
bool selectMuxChannel(uint8_t channel) {
if (channel > 7) {
return false;
}
Wire.beginTransmission(MUX_ADDRESS);
Wire.write(1 << channel);
return Wire.endTransmission() == 0;
}
void setup() {
Wire.begin();
Serial.begin(9600);
if (selectMuxChannel(2)) {
Serial.println("Channel 2 selected");
} else {
Serial.println("Multiplexer did not respond");
}
}
void loop() {
}The multiplexer does not change a device's address. It prevents devices on different channels from seeing each other's traffic.
Pull-ups, voltage, and wiring
SDA and SCL are shared open-drain lines. Devices pull a line low but do not actively drive it high. Pull-up resistors provide the high level. Many breakout boards include pull-ups, so connecting several boards can place too many resistors in parallel.
Too much pull-up resistance can produce slow rising edges. Too little resistance increases current when a line is low. The correct value depends on voltage, bus capacitance, and speed. Values from about 2.2 kΩ to 10 kΩ are common, but check the device and board documentation.
All devices must agree on the logic voltage unless a bidirectional level shifter separates voltage domains. A 5 V pull-up can damage a 3.3 V-only device.
Exercise
Exercise
Two identical sensors both answer at address 0x40. Name two ways to connect them to one Arduino.
Show answer
Change one sensor's address using its address pin or jumper, or place the sensors on separate channels of an I2C multiplexer.