Electronic – arduino – Using multiple TCA9548A multiplexers

arduinoi2cmicrocontrollermultiplexer

The case is that I have 5 temperature sensors.

I want to connect 4 of them to the first TCA9548A (TCAADDR0), the SDA and SCL busses (from the ports 0 to 3) and connect the fifth one to the second TCA9548A (TCAADDR1).

If the fifth sensor connect is connected to the busses 0 to 3 of TCAADDR1, I receive an incorrect temperature. However, if I connect it the busses No.4 of the TCAADDR1, I receive the correct temperature.

I think the problem is that I can not switch off the TCAADDR0 and only talk to the TCAADDR1.
On the other hand, if I connect A0 of the TCAADDR1 to the 5V, I have 3 addresses 0x70 0x71 and 0x77. If I don't connect it, I have 0x70 and 0x77. So I decided not to connect A0 to 5V.

This is my program code:

#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;

Adafruit_BMP085 bmp180_0;
Adafruit_BMP085 bmp180_1;
Adafruit_BMP085 bmp180_2;
Adafruit_BMP085 bmp180_3;
Adafruit_BMP085 bmp180_4;

#define TCAADDR0 0x70
#define TCAADDR1 0x77


void tcaselect0(uint8_t sensor) { 
  unsigned int mask = 1 << sensor;
  Wire.beginTransmission(TCAADDR0);
  Wire.write(lowByte(mask));
  Wire.endTransmission();
  Wire.beginTransmission(TCAADDR1);
  Wire.write(highByte(mask));
  Wire.endTransmission();
  delay(1);
}

void tcaselect1(uint8_t sensor) { 
  unsigned int mask = 1 << sensor;
  Wire.beginTransmission(TCAADDR0);
  Wire.write(lowByte(mask));
  Wire.endTransmission();
  Wire.beginTransmission(TCAADDR1);
  Wire.write(highByte(mask));
  Wire.endTransmission();
  delay(1);
}

void setup() {
  Serial.begin(9600);
  Wire.begin();  //enabling the I2C protocol through the Wire.begin()

  //FIRST SENSOR**                
  tcaselect0(0); 
  if (bmp180_0.begin() != 0x60)
  { Serial.print(F("BMP180 Nr.1 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("BMP180 Nr.1 detected?\t")); Serial.println(F("Yes"));}


  //SECOND SENSOR
  tcaselect0(1); 
  if (bmp180_1.begin() != 0x60)
  { Serial.print(F("BMP180 Nr.2 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("BMP180 Nr.2 detected?\t")); Serial.println(F("Yes"));}


  //THIRD SENSOR
  tcaselect0(2);  
  if (bmp180_2.begin() != 0x60)
  { Serial.print(F("BMP180 Nr.3 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("BMP180 Nr.3 detected?\t")); Serial.println(F("Yes"));}


  //FOURTH SENSOR
  tcaselect0(3);  
  if (bmp180_3.begin() != 0x60)
  { Serial.print(F("BMP180 Nr.4 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("BMP180 Nr.4 detected?\t")); Serial.println(F("Yes"));}



Wire.beginTransmission(TCAADDR0);
Wire.write(0);
Wire.endTransmission();

  //*FIFTH SENSOR
  tcaselect1(4);  
  if (bmp180_4.begin() != 0x60)
  { Serial.print(F("BMP180 Nr.5 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("BMP180 Nr.5 detected?\t")); Serial.println(F("Yes"));}

  Serial.println();
  Serial.println(F("##############################"));            //30-times #
  Serial.println(F("Initialization Finished"));
  Serial.println(F("##############################"));            //30-times # 
  Serial.println();
  Serial.println();

}

void loop() 
{

  //SENSOR-FIRST
    tcaselect0(0);
    Serial.println("*****SENSOR NO.1***** ");
    Serial.print("Temperature = ");
    Serial.print(bmp180_0.readTemperature());
    Serial.println(" *C");

//*SENSOR-SECOND
    tcaselect0(1);
    Serial.println("*****SENSOR NO.2***** ");
    Serial.print("Temperature = ");
    Serial.print(bmp180_1.readTemperature());
    Serial.println(" *C");

//**SENSOR-THIRD
    tcaselect0(2);
     Serial.println("*****SENSOR NO.3***** ");
    Serial.print("Temperature = ");
    Serial.print(bmp180_2.readTemperature());
    Serial.println(" *C");

//*SENSOR-FOURTH
    tcaselect0(3);
     Serial.println("*****SENSOR NO.4***** ");
    Serial.print("Temperature = ");
    Serial.print(bmp180_3.readTemperature());
    Serial.println(" *C");


Wire.beginTransmission(TCAADDR0);
Wire.write(0);
Wire.endTransmission();


//*SENSOR-FIFTH
    tcaselect1(4);
     Serial.println("*****SENSOR NO.5***** ");
    Serial.print("Temperature = ");
    Serial.print(bmp180_4.readTemperature());
    Serial.println(" *C");


    Serial.println();
    delay(2000);

}

Best Answer

The TCA9548A has three address pins, A0-A2 which let you set the address of the mux. Therefore, you can place two of them on your main buss, at different addresses (for example, 0x70 and 0x71). I’d place 5 of your sensors on each mux.

To switch between mixes, first write to the first mux, disabling all channels and then write to the second mux, enabling the selected channel. This could be generalized as always disabling the existing channel before enabling the new channel. You could create a table, indexed by the sensor number, containing the addresses of the mux and the value to write into the mux register.