Has anyone else had trouble connecting to slave devices using the TCA9548A I2C multiplexer? My code works when I connect directly from my microcontroller to the AD7746 sensor I'm using, but if I use the multiplexer I get an "I2C Error 2" error. My code and a sample output are below:
-------------------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------
I2C Error: 2 -- Address Receive NACK
0
--------------------------------------------------------------------------------------------------------------------------------------------------
CODE
--------------------------------------------------------------------------------------------------------------------------------------------------
#include <i2c_t3.h>
void setup() {
// put your setup code here, to run once:
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 100000);
Serial.begin(9600);
mux_select(7); // select channel on the i2c multiplexer
Wire.beginTransmission(0x48); // prepare i2c transmission to ad7746 device
Wire.write(0x0A); // select CONFIGURATION register to read from
Wire.endTransmission(I2C_NOSTOP); // sending a stop condition will reset the address pointer on the ad7746, so don't do that
if( Wire.getError() != 0 ) // check if there was an i2c error
{
switch(Wire.getError()) {
case 1:
Serial.println("I2C Error: 1 -- Data too long");
break;
case 2:
Serial.println("I2C Error: 2 -- Address Receive NACK");
break;
case 3:
Serial.println("I2C Error; 3 -- Data Receive NACK");
break;
case 4:
Serial.println("I2C Error: 4 -- Other Error");
break;
}
}
Wire.requestFrom(0x48,1,I2C_NOSTOP);
uint8_t data;
data = Wire.readByte();
Serial.println(data,BIN);
mux_deselect();
}
void loop() {
// put your main code here, to run repeatedly:
}
void mux_select(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(0x70);
Wire.write(1 << i);
// previous line uses "bitshift". essentially, we multiply 1 by 2^i and use that value to select the input on the multiplexer
// e.g. i<<0 selects sda0 and scl0, etc.
Wire.endTransmission(I2C_STOP);
}
void mux_deselect() {
Wire.beginTransmission(0x70);
Wire.write(B00000000);
Wire.endTransmission(I2C_STOP);
}