Sub topic 10
Connecting multiple devices to one bus
This chapter assumes you know the basics of I2C. You should know that SDA carries data and SCL carries the clock, that both lines are open-drain with pull-up resistors, and that each target has a 7-bit address. You should also be able to run the Wire library bus scanner and read a register with beginTransmission, endTransmission and requestFrom. Here you will put several devices on one bus and make them work together reliably.
Why one bus can serve many devices
I2C is a shared bus. Every device connects to the same two wires. The controller (your Arduino) starts each transaction by sending an address. Only the target with that address answers. All other targets ignore the traffic until the next START condition.
Wiring is therefore simple. Connect all SDA pins together, all SCL pins together, and all grounds together. Add one set of pull-ups for the whole bus, not one per device.
Step 1: Check addresses before you wire
Look up the address of each device in its datasheet. Many modules have a fixed address. Others have address pins or solder jumpers that change one or more address bits. Write the addresses down before you connect anything. If two devices share an address, you have a conflict, and you must resolve it first. The next chapter covers that.
Some common 7-bit addresses are listed below. Treat them as examples, because manufacturers and module makers vary.
| Device type | Typical address | Notes |
|---|---|---|
| SSD1306 OLED display | 0x3C or 0x3D | Selected by a jumper |
| DS3231 real-time clock | 0x68 | Fixed |
| MPU-6050 motion sensor | 0x68 or 0x69 | Selected by the AD0 pin |
| BMP280 pressure sensor | 0x76 or 0x77 | Selected by the SDO pin |
| TCA9548A multiplexer | 0x70 to 0x77 | Selected by A0 to A2 |
The table already shows a clash. A DS3231 and an MPU-6050 with AD0 low both use 0x68. Setting AD0 high moves the MPU-6050 to 0x69 and removes the clash.
Step 2: Wire the bus
- Connect all grounds first. Devices that do not share a ground reference cannot decode each other's signals.
- Join all SDA pins and all SCL pins.
- Power each device at its own rated voltage. Mixed voltages need level shifting, which a later chapter covers.
- Count the pull-ups already on your modules.
- Run the scanner and confirm that every expected address appears.
Counting pull-ups
Many breakout boards include their own pull-up resistors, often 4.7 kΩ or 10 kΩ. Because the resistors sit in parallel, their effective value falls as you add boards. Parallel resistors combine like this:
R_total = 1 / (1/R1 + 1/R2 + ...)
Four boards with 10 kΩ pull-ups give 2.5 kΩ on each line. The Arduino Uno board has no pull-ups on its I2C pins, but the ATmega328P enables weak internal pull-ups of about 20 kΩ to 50 kΩ when Wire.begin() runs. Those are too weak to rely on.
A lower resistance gives faster rising edges but makes each device pull more current when it drives the line low. The I2C specification limits sink current to 3 mA. At 5 V, the minimum safe pull-up is about (5 V − 0.4 V) / 3 mA ≈ 1.5 kΩ. At 3.3 V it is about 1 kΩ.
The following sketch computes the combined value so you can check your setup against that range.
// Combined pull-up resistance for several boards in parallel.
const float boardPullups[] = {10000.0, 10000.0, 4700.0, 4700.0}; // ohms
const int boardCount = sizeof(boardPullups) / sizeof(boardPullups[0]);
void setup() {
Serial.begin(9600);
float conductance = 0.0;
for (int i = 0; i < boardCount; i++) {
conductance += 1.0 / boardPullups[i];
}
float combined = 1.0 / conductance;
Serial.print("Combined pull-up: ");
Serial.print(combined);
Serial.println(" ohms");
if (combined < 1500.0) {
Serial.println("Warning: too strong for 5 V, remove some pull-ups");
} else if (combined > 10000.0) {
Serial.println("Warning: may be too weak, add a pull-up");
} else {
Serial.println("Within the usual range");
}
}
void loop() {
}For the values above, the sketch prints about 1.6 kΩ, which is just inside the range. If a bus has too many strong pull-ups, desolder resistors from some boards or cut the pull-up jumpers where the board provides them.
Step 3: Talk to each device in turn
The Wire library works with one device at a time. You address each device inside its own transaction. This sketch reads a TMP102-style temperature sensor at 0x48 and a DS3231 clock at 0x68 on the same bus.
#include <Wire.h>
const uint8_t TEMP_ADDRESS = 0x48;
const uint8_t RTC_ADDRESS = 0x68;
bool readRegister16(uint8_t address, uint8_t reg, uint16_t &value) {
Wire.beginTransmission(address);
Wire.write(reg);
if (Wire.endTransmission(false) != 0) {
return false;
}
if (Wire.requestFrom(address, (uint8_t)2) != 2) {
return false;
}
value = (uint16_t)Wire.read() << 8;
value |= Wire.read();
return true;
}
bool readRegister8(uint8_t address, uint8_t reg, uint8_t &value) {
Wire.beginTransmission(address);
Wire.write(reg);
if (Wire.endTransmission(false) != 0) {
return false;
}
if (Wire.requestFrom(address, (uint8_t)1) != 1) {
return false;
}
value = Wire.read();
return true;
}
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
uint16_t rawTemp;
uint8_t seconds;
if (readRegister16(TEMP_ADDRESS, 0x00, rawTemp)) {
int16_t signedTemp = (int16_t)rawTemp >> 4; // 12-bit left-justified value
Serial.print("Temperature: ");
Serial.print(signedTemp * 0.0625);
Serial.println(" C");
} else {
Serial.println("Temperature sensor did not respond");
}
if (readRegister8(RTC_ADDRESS, 0x00, seconds)) {
Serial.print("RTC seconds (BCD): 0x");
Serial.println(seconds, HEX);
} else {
Serial.println("RTC did not respond");
}
delay(1000);
}Each helper function checks every step and returns false on failure. The sketch reports which device failed, so a missing device does not stop the other one. The DS3231 stores time as binary-coded decimal (BCD), so the value 0x45 means 45 seconds.
Sharing the bus safely
Several habits keep a shared bus predictable.
- Finish one transaction before you start the next. Wire is not designed for interleaved transactions. Complete the read or write with a device before you address another.
- Use the return values.
endTransmission()returns 0 on success. It returns 2 for a NACK on the address, 3 for a NACK on data, and 4 for other errors. Value 5 means a timeout on newer cores.requestFrom()returns the number of bytes received. Compare it with the number you asked for. - Use one clock speed for the whole bus.
Wire.setClock()applies to every device. If one device supports only 100 kHz, keep the entire bus at 100 kHz. - Keep interrupt handlers short. Do not call Wire functions inside an interrupt service routine. Set a flag and handle the transfer in
loop(). - Give slow devices time. Some sensors need a conversion delay between the command and the result. Wait for it, or poll a status register.
Bus load grows with every device
Each device adds capacitance to both lines, typically 5 to 10 pF per chip, plus the wires between them. The I2C specification allows 400 pF total. Too much capacitance rounds off the rising edges, and the bus eventually fails at higher speeds. If you see errors only after adding the fourth or fifth device, try these steps in order:
- Shorten the wires.
- Lower the clock to 100 kHz.
- Reduce the pull-up resistance within the safe range.
- Split the bus with a multiplexer.
Verifying your setup
After wiring, run the scanner from the earlier chapter. Compare the list of addresses with your notes. A missing address usually means a wiring fault, no power, or a wrong address assumption. An extra address can mean a module has an onboard chip, such as an EEPROM on a real-time clock board. Many DS3231 boards include an AT24C32 EEPROM at 0x57.
Exercise
A bus has three breakout boards, each with 4.7 kΩ pull-ups on SDA and SCL, and the system runs at 5 V. What is the combined pull-up resistance on SDA, and is it safe for a device that can sink 3 mA?
Show answer
Three 4.7 kΩ resistors in parallel give 4.7 kΩ / 3 ≈ 1.57 kΩ. The current through it when a device pulls the line low is about (5 V − 0.4 V) / 1.57 kΩ ≈ 2.9 mA. That is just under the 3 mA limit, so it is acceptable but has little margin. Removing the pull-ups from one board would give about 2.35 kΩ and about 2.0 mA, which is safer.
Exercise
You connect a DS3231 clock and an MPU-6050 motion sensor to the same bus. The scanner shows only one device at 0x68. Name two ways to fix this.
Show answer
Set the MPU-6050 AD0 pin high so it moves to 0x69. Alternatively, put one of the devices behind a TCA9548A multiplexer and select its channel before each transaction.