Sub topic 11
Resolving Address Conflicts with Address Pins and Multiplexers
This chapter assumes you can wire a basic I2C bus with pull-ups, know that devices use 7-bit addresses, and have run the Wire bus scanner. It covers what happens when two devices want the same address and the three ways to fix it.
Why conflicts happen
Every target on an I2C bus must answer to a unique 7-bit address. Many sensors ship with one fixed address burned in by the manufacturer. If you buy two identical sensors and wire both to the same SDA and SCL lines, both respond to the same address byte.
The symptoms depend on what the devices do:
- On a write, both devices ACK. The controller sees one ACK and cannot tell that two devices heard it. Both devices receive the same data.
- On a read, both devices drive SDA at once. Because the lines are open-drain, a 0 from either device wins. The controller reads the bitwise AND of both replies. You get garbage that looks plausible.
- The scanner shows only one address, so the conflict is invisible to it.
A scanner that lists one address cannot tell you whether one or two devices answer there. If readings look wrong only after you add a second identical part, suspect a conflict first.
Option 1: address pins and jumpers
Many parts expose one to three address-select pins. Tying each pin to GND or VCC changes the low bits of the address. For example, the TCA9548A multiplexer has pins A0 to A2 and the base address 0x70. Three pins give eight combinations, so you can place up to eight of them on one bus.
| A2 | A1 | A0 | Address |
|---|---|---|---|
| 0 | 0 | 0 | 0x70 |
| 0 | 0 | 1 | 0x71 |
| 0 | 1 | 0 | 0x72 |
| 1 | 1 | 1 | 0x77 |
Other parts work differently. Some have a single pin that selects between two addresses. Some use a pin that is sampled at power-up. Some derive the address from a resistor network. Always check the datasheet's address table, and do not assume the pins act as binary digits in the order you expect.
Two rules prevent the most common mistakes:
- Never leave an address pin floating. A floating input can read as either level and may change with noise or hand proximity. Tie it firmly to GND or VCC.
- Breakout boards often route the pin to a solder jumper or a pad with a weak pull-down. Read the silkscreen and the board schematic to learn the default and how to change it.
After you change a pin, rerun the scanner. You should see a new address and the old one should disappear if you moved the only device.
Address pins are the cheapest fix. They cost no code and no extra parts. Their limit is that the number of addresses is small. A part with one address pin supports two devices at most.
Option 2: reserved addresses and the usable range
Even with address pins, the usable range is smaller than it looks. The 7-bit space has 128 values, but addresses 0x00 to 0x07 and 0x78 to 0x7F are reserved for special uses such as general call and 10-bit addressing. That leaves 0x08 to 0x77, which is 112 addresses. The scanner in the earlier chapter checks 1 to 126, so a reply at a reserved address means something unusual is on the bus.
Many popular sensors cluster in the same few addresses. For example, several common displays, IMUs, and sensors use 0x3C, 0x68, 0x76, or 0x77. Two different part types can collide just as two identical parts can. When you plan a bus, list the address options of every part before you buy.
Option 3: an I2C multiplexer
When address pins run out, use a multiplexer. The TCA9548A has one upstream port connected to your Arduino and eight downstream channels, each a separate I2C bus. A single control register holds one bit per channel. A set bit connects that channel to the upstream lines. A clear bit isolates it.
Because only the selected channel is connected, two identical sensors never see each other. The multiplexer itself sits at its own address, 0x70 by default, which must not clash with anything else on the upstream bus.
Selecting a channel
The helper below writes one control byte. Bit n of the byte enables channel n.
#include <Wire.h>
const uint8_t MUX_ADDRESS = 0x70; // TCA9548A default address with A0-A2 tied low
void selectMuxChannel(uint8_t channel) {
if (channel > 7) return;
Wire.beginTransmission(MUX_ADDRESS);
Wire.write(1 << channel); // one control byte; bit n enables channel n
Wire.endTransmission();
}After you call this, every transaction goes to the devices on that channel only. Writing 0 disables all channels. Writing a value with several bits set connects several channels at once, which reintroduces the conflict if they hold identical devices.
Reading two identical sensors
This sketch reads a 16-bit register from the same address on two channels. It uses the register-read function from the earlier chapter on registers, and the register number is a placeholder for your part.
#include <Wire.h>
const uint8_t MUX_ADDRESS = 0x70;
const uint8_t SENSOR_ADDRESS = 0x76;
const uint8_t SENSOR_REGISTER = 0x00; // replace with a register from your datasheet
void selectMuxChannel(uint8_t channel) {
if (channel > 7) return;
Wire.beginTransmission(MUX_ADDRESS);
Wire.write(1 << channel);
Wire.endTransmission();
}
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;
}
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
for (uint8_t channel = 0; channel < 2; channel++) {
selectMuxChannel(channel);
uint16_t value;
if (readRegister16(SENSOR_ADDRESS, SENSOR_REGISTER, value)) {
Serial.print("Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.println(value);
} else {
Serial.print("Channel ");
Serial.print(channel);
Serial.println(": no response");
}
}
delay(1000);
}Multiplexer gotchas
- The selection is state. The multiplexer remembers the last channel until you change it or power-cycle it. If other code assumes a different channel, it talks to the wrong device. Select the channel immediately before each group of transactions, or wrap each sensor in a function that does it.
- A reset leaves the mux in its old state. If the Arduino resets while the multiplexer keeps power, the channel stays selected. Select a channel explicitly in
setup(), or use the mux's reset pin if your breakout exposes it. - Check the return value.
endTransmission()returns nonzero if the mux does not ACK, for example when its address pins differ from your constant. - Scanning shows only the active channel. Run the scanner after selecting each channel in turn to map what is attached. With no channel selected, the scanner sees only the multiplexer.
- Pull-ups are per side. The upstream bus needs pull-ups, and each downstream channel needs its own. Many breakouts include them on both sides. Count the parallel resistance if you stack several boards, as the pull-up chapter explains.
- Voltage levels can differ per channel. The TCA9548A lets each downstream channel run at a different voltage from the upstream side, provided the multiplexer's supply and the channel's pull-up rails are chosen to match. This can replace a separate level shifter in some designs, but check the datasheet for the allowed range.
- Added delay and capacitance. The multiplexer adds a small amount of capacitance. Because each channel is isolated, it can reduce total load on the upstream bus, which helps when you have many devices.
Choosing a method
| Situation | Best fix |
|---|---|
| Part has address pins and you need two or three copies | Set the address pins |
| Different part types collide at one address | Move one with its address pin, or put it behind a multiplexer |
| Many identical parts with a fixed address | Multiplexer, one part per channel |
| One long cable run would load the bus | Multiplexer to split the capacitance |
| Part has no address options and you need only one more bus | A second hardware I2C port if the board has one |
A second I2C port is worth knowing about. Boards such as the Arduino Mega and Due, and many ARM-based boards, have more than one hardware I2C interface. Each interface is an independent bus, so the same address can appear on each. On the classic Uno there is only one hardware port.
Debugging checklist
- Run the scanner with only one of the identical devices connected. Note its address.
- Add the second device. If the address list does not change but readings degrade, you have a conflict.
- Confirm each address pin is tied hard to GND or VCC.
- With a multiplexer, confirm the mux address appears, then select each channel and scan again.
- Confirm that
endTransmission()returns 0 for the mux call before you talk to the sensor.
Exercise
You wire two identical sensors with the fixed address 0x76 to an Arduino Uno and a TCA9548A, one on channel 0 and one on channel 3. Your code calls selectMuxChannel(3) once in setup() and then reads address 0x76 in loop() for both "sensors". What do you observe, and what is the fix?
Show answer
Both reads return data from the sensor on channel 3, because the multiplexer keeps channel 3 selected until told otherwise. The sensor on channel 0 is never reached. Fix it by calling selectMuxChannel(0) before reading the first sensor and selectMuxChannel(3) before reading the second, inside loop().
Exercise
A scanner run shows one device at 0x68 after you connect two identical IMUs to the same lines. The readings are noisy and sometimes wrong. Give the cause and two ways to fix it.
Show answer
Both devices answer at 0x68. On reads their outputs combine as a wired AND, so the data is corrupted, and the scanner cannot show the duplicate. Fixes: set the address-select pin on one IMU to its alternate level so it moves to its second address (for many parts, 0x69), or place each IMU on its own channel of a TCA9548A.