Sub topic 3
Connecting Multiple Devices to One Bus
This chapter assumes you can write a sketch with setup() and loop(), use the Serial Monitor, call the Wire library, and read hexadecimal addresses.
Sharing one bus
Several targets can share the SDA and SCL lines. Each target needs a unique 7-bit address. The controller addresses one target per transaction, and the other targets ignore that traffic. Add pull-up resistors once for the whole bus. Extra pairs lower the effective resistance and can exceed what the controller can sink.
Resolving address conflicts
Many sensors have one fixed address. Two identical chips cannot share a bus unless you change an address pin (usually A0 to A2) or place one chip behind a multiplexer. The TCA9548A is a common multiplexer with default address 0x70. It has eight channels, and each channel is a separate bus segment. Writing one control byte enables a channel:
#include <Wire.h>
const uint8_t MUX_ADDRESS = 0x70;
void selectMuxChannel(uint8_t channel) {
if (channel > 7) return;
Wire.beginTransmission(MUX_ADDRESS);
Wire.write(1 << channel);
Wire.endTransmission();
}Mixing 3.3 V and 5 V devices
A 5 V Arduino Uno pulls SDA and SCL up to 5 V. A 3.3 V sensor can be damaged by 5 V on its pins. Use a bidirectional level shifter between the two sides, and tie each side's pull-ups to that side's supply.
Bus capacitance and cable length
The I2C specification limits bus capacitance to 400 pF. Jumper wires, breadboards, and cables add capacitance, which slows the rising edges of the signal. Keep wires short. For longer runs, lower the bus speed or use a bus extender.
Making an Arduino act as an I2C target
Calling Wire.begin(8) makes the Arduino a target at address 8. Register Wire.onReceive and Wire.onRequest callbacks to handle incoming and outgoing data.
Clock stretching
A target can hold SCL low to ask the controller to wait. The Arduino Wire library handles this automatically. A slow target can still make a transaction take longer, so check the datasheet for its stretch limit.
Troubleshooting a misbehaving bus
Check the pull-ups first, then run an address scanner, confirm each device's address, verify voltage levels, and shorten the wiring.
Exercise
Exercise
Two sensors both have address 0x48. Using the selectMuxChannel function, how do you read from the sensor on channel 1 without disturbing the sensor on channel 0?
Show answer
Call selectMuxChannel(1) so only channel 1 is enabled, then address 0x48 with Wire.beginTransmission and the usual read calls. Channel 0 stays isolated from the traffic.