I2C设备接多了早晚撞上两件事:两颗同地址的传感器没法同时用,总线上的设备一多线一长就集体掉线。TCA9548A就是给总线加了个八选一的开关,一颗芯片把一条I2C总线扩成八条互不干扰的独立总线,同地址设备挂不同通道照样能各说各话。这篇把它的用法一次拆完。

原理:写一个字节就是切通道
它自己只占一个I2C地址0x70,主控往0x70写一个字节,哪个bit是1就打开哪条通道,比如写0x04打开SD4/SC4那一路。没被选中的通道在电气上是隔离的,等于从总线上摘掉了,所以同地址的芯片挂在不同通道互不冲突。板上A0/A1/A2三个焊盘可以改地址,最多能接八颗复用器,扩到64条通道:

左边VIN/GND/SDA/SCL/RST接主控,右边SD0到SD7、SC0到SC7是八条独立通道。复位脚RST拉低一下,所有通道全部关闭,总线卡死时用它救急。
接线:不用任何库
它没有专属库,三根线接主控的SDA、SCL、3.3V或5V、GND就能干活,下面这份扫描器直接验证每个通道上有什么设备:
/**
* TCA9548 I2CScanner.ino -- I2C bus scanner for Arduino
*
* Based on https://playground.arduino.cc/Main/I2cScanner/
*
*/
#include "Wire.h"
#define TCAADDR 0x70
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
// standard Arduino setup()
void setup()
{
while (!Serial);
delay(1000);
Wire.begin();
Serial.begin(115200);
Serial.println("\nTCAScanner ready!");
for (uint8_t t=0; t<8; t++) {
tcaselect(t);
Serial.print("TCA Port #"); Serial.println(t);
for (uint8_t addr = 0; addr<=127; addr++) {
if (addr == TCAADDR) continue;
Wire.beginTransmission(addr);
if (!Wire.endTransmission()) {
Serial.print("Found I2C 0x"); Serial.println(addr,HEX);
}
}
}
Serial.println("\ndone");
}
void loop()
{
}
tcaselect()就是全部精髓:往0x70发一个1<
同地址芯片挂两颗怎么读
拿HMC5883磁力计举例,两颗同地址的芯片分别挂通道0和通道1,读数前先切通道:
#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883_U.h"
#define TCAADDR 0x70
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag1 = Adafruit_HMC5883_Unified(1);
Adafruit_HMC5883_Unified mag2 = Adafruit_HMC5883_Unified(2);
void displaySensorDetails(Adafruit_HMC5883_Unified *mag)
{
sensor_t sensor;
mag->getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
void setup(void)
{
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* Initialise the 1st sensor */
tcaselect(2);
if(!mag1.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
/* Initialise the 2nd sensor */
tcaselect(6);
if(!mag2.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
/* Display some basic information on this sensor */
tcaselect(2);
displaySensorDetails(&mag1);
tcaselect(6);
displaySensorDetails(&mag2);
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
tcaselect(2);
mag1.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("Sensor #1 - ");
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
tcaselect(6);
mag2.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("Sensor #2 - ");
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
delay(500);
}
两个对象共用一套驱动代码,读哪颗就先tcaselect(0)还是tcaselect(1),输出里Sensor #1和#2各自报自己的参数与读数。这个套路对BME280、OLED、MPU-6050等所有I2C设备通用。
板子挂满传感器的样子,八条通道理论上能同时伺候八颗芯片:

实战注意三条
一是切换要干净:切通道前建议先Wire.endTransmission()把当前传输收尾,读写中途切通道数据会乱。二是上电默认全关:初始化后记得显式选通道。三是别把它当线驱动器:通道过长(超过30厘米)还是得上总线缓冲或者降低I2C速率。板子到手先焊排针,配杜邦线就能上手:

项目里传感器越堆越多的阶段,这颗十几块钱的芯片基本是必配。把tcaselect()写进每个设备的读数函数开头,多总线管理就全自动化了。











空通道完全透明这点讲得好,八条里只用三条也不影响别的设备。另外试过把OLED和传感器分别挂不同通道,刷新速度肉眼可见变快。
RST救急那条记下了,之前I2C总线卡死都是整个重启板子,现在接根线到数字口拉低一下就复位复用器,优雅多了。
tcaselect五行的写法太实用了,比想象中简单。已经把两颗同地址的VL53L0X挪到不同通道上,扫描器一跑全都现形,困扰一周的问题十分钟解决。
提醒一下买回来记得焊排针,我第一次插面包板没焊直接虚接,通道时好时坏查了一晚上。焊接完稳如老狗。