TCA9548A I2C multiplexer
The TCA9548A eight-channel I2C switch on its breakout: one address on the board's bus, and behind it eight more buses, joined to the main one by a byte.
Aliases: TCA9548A, PCA9548A, I2C mux, I2C multiplexer, I2C switch.
It is the usual answer to two sensors with the same address: two BME280s at 0x76, or
two OLEDs at 0x3C. Each goes on its own channel, and the sketch selects the one it
wants before it talks to it. It needs no library: Wire.h writes the byte.
void tcaselect(uint8_t channel) {
Wire.beginTransmission(0x70);
Wire.write(1 << channel);
Wire.endTransmission();
}
Pins
| Pin | What it does |
|---|---|
VIN |
Supply, 1.65 to 5.5 V. |
GND |
Ground. |
SDA, SCL |
The board's I2C bus, with 10k pull-ups on the module. |
RST |
Reset, active low, pulled up on the module. Low clears every channel. |
A0, A1, A2 |
The address straps, pulled down on the module. |
SD0..SD7, SC0..SC7 |
The eight channels' data and clock. |
The address is 0x70 plus A2 A1 A0: wire A0 to VIN for 0x71, all three for
0x77. Up to eight of them share a bus, 64 channels in all.
What the model gets right
One register. A byte written to the chip's address selects the channels: bit n joins channel n, so 0x04 is channel 2 and 0x05 channels 0 and 2 together. 0x00 disconnects them all. A read returns the byte. As the TI sheet says, a new selection takes effect at the STOP that ends the write.
The switches pass both ways. A sensor on a selected channel is on the main bus as far as the board can tell: its acknowledge and its data come back through the switch, and whichever side pulls a line low wins, which is open drain. A sensor on a channel that is not selected does not hear the bus at all.
The channels have no pull-ups of their own. They rely on the pull-ups of the modules plugged into them, as every sensor breakout has.
What it does not model
The real chip's pass transistors limit how high a channel is pulled to about the chip's supply less a threshold, which is what lets a 3.3 V sensor sit behind a 5 V board. Here a channel is pulled to whatever its own pull-ups are tied to. Bus capacitance and the reset's timing are not modeled either.
Common mistakes
Forgetting to select a channel. At power-on every channel is disconnected, so an I2C scan finds only 0x70.
Two channels selected at once with the same address on both: the two sensors then answer together, as on the bench.