How to use two TCA9548a for mpu 9250

Hi,

I have 2 TCA9548A I2C Multiplexers connected to a ESP32. Also connected 2 I2C sensors with same addresses to each of the TC9548A on different channels. How do I call each of the TCA9548A I2C MUX with their address to read the sensor data continuously in a loop?
So far I can only read the sensors off one TCA9548A MUX. But unable to do so off the two MUX's. i want to read the mux channels (0-9) like wise.

#include <Wire.h>
#include "MPU9250.h"
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU0(Wire, 0x68);
MPU9250 IMU1(Wire, 0x68);

int status;
// #define I2C_ADDR 0x68

#define TCAADDR0 0x70

#define TCAADDR1 0x71

void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR0);
  Wire.write(1 << i);
  Wire.write(0x01); 
  Wire.endTransmission();  
  delay(100); 
}

void tcaselect1(uint8_t j) {
  if (j > 7) return;
 
  Wire.beginTransmission(TCAADDR1);
  Wire.write(0x01); 
  Wire.write(1 << j);
  
  Wire.endTransmission();  
}



void setup() {
  Serial.begin(9600);
  Wire.begin();

  while (!Serial) {}

  // start communication with IMU
  status = IMU0.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while (1) {}
  }

status = IMU1.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while (1) {}
  }

  Serial.println(F("##############################"));            
  Serial.println(F("Starting Initialization"));
  Serial.println(F("##############################"));       
 

  //*************INITIALIZING FIRST SENSOR*******************************   
  tcaselect(0);
 if (IMU0.begin() != 0x68)
  { Serial.print(F("MPU.1 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("MPU.1 detected?\t")); Serial.println(F("Yes"));}

  tcaselect1(0);
 if (IMU1.begin() != 0x68)
  { Serial.print(F("MPU.1 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("MPU.1 detected?\t")); Serial.println(F("Yes"));}

}
  //*********************************************************************
  //*************NOW LET'S START MEASURING*******************************
void loop() 
{ 
   tcaselect(0);
   IMU0.readSensor();
    // display the data
  Serial.print("Accel_X: ");
  Serial.print(IMU0.getAccelX_mss(), 6);
  Serial.print("\t");
   Serial.print("Accel_Y: ");

  Serial.print(IMU0.getAccelY_mss(), 6);
  Serial.print("\t");
   Serial.print("Accel_Z: ");

  Serial.print(IMU0.getAccelZ_mss(), 6);
  Serial.print("\t");
  Serial.print("Gyro_X: ");

  Serial.print(IMU0.getGyroX_rads(), 6);
  Serial.print("\t");
   Serial.print("Gyro_Y: ");
  Serial.print(IMU0.getGyroY_rads(), 6);
  Serial.print("\t");
   Serial.print("Gyro_Z: ");
  Serial.print(IMU0.getGyroZ_rads(), 6);
  Serial.print("\t");
   Serial.print("Mag_X: ");
  Serial.print(IMU0.getMagX_uT(), 6);
  Serial.print("\t");
   Serial.print("Mag_Y: ");
  Serial.print(IMU0.getMagY_uT(), 6);
  Serial.print("\t");
   Serial.print("Mag_Z: ");
  Serial.print(IMU0.getMagZ_uT(), 6);
  Serial.print("\t");
   Serial.print("TEMP: ");
  Serial.println(IMU0.getTemperature_C(), 6);
  delay(100); 
   tcaselect1(0);
IMU1.readSensor();
    // display the data
  Serial.print("Accel_X: ");
  Serial.print(IMU1.getAccelX_mss(), 6);
  Serial.print("\t");
   Serial.print("Accel_Y: ");

  Serial.print(IMU1.getAccelY_mss(), 6);
  Serial.print("\t");
   Serial.print("Accel_Z: ");

  Serial.print(IMU1.getAccelZ_mss(), 6);
  Serial.print("\t");
  Serial.print("Gyro_X: ");

  Serial.print(IMU1.getGyroX_rads(), 6);
  Serial.print("\t");
   Serial.print("Gyro_Y: ");
  Serial.print(IMU1.getGyroY_rads(), 6);
  Serial.print("\t");
   Serial.print("Gyro_Z: ");
  Serial.print(IMU1.getGyroZ_rads(), 6);
  Serial.print("\t");
   Serial.print("Mag_X: ");
  Serial.print(IMU1.getMagX_uT(), 6);
  Serial.print("\t");
   Serial.print("Mag_Y: ");
  Serial.print(IMU1.getMagY_uT(), 6);
  Serial.print("\t");
   Serial.print("Mag_Z: ");
  Serial.print(IMU1.getMagZ_uT(), 6);
  Serial.print("\t");
   Serial.print("TEMP: ");
  Serial.println(IMU1.getTemperature_C(), 6);
  delay(100); 

  

   delay(1000);   
 
}

This will show you how to use it.

Note using a 3V3 processor might mean you have to have a bidirectional voltage level shifter on the incoming I2C signal.

First question would be why you have 2 TCA9548A chips? And the second one is why you have your sensors connected to two different TCA9548A chips? Are you planning to use up to 16 of those unknown sensors in your setup?

Life would be a lot easier if you connected both sensors to the same TCA9548A.

Didn't you post the same question a couple of weeks ago?

In setup, you are calling the begin() function for each IMU unit twice, once for each IMU before selecting any of the multiplexer channels, and again for each IMU after selecting its respective multiplexer channel.

In both setup and loop, you are selecting the multiplexer channel for each IMU, but nowhere do you ever de-select the multiplexer channel, which will leave both IMUs on the I2C bus at the same time.

i would like ton use 12 mpu for reading so i need to use two TCA9548a mux

Try this and see if you get any better results:

#include <Wire.h>
#include "MPU9250.h"
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU0(Wire, 0x68);
MPU9250 IMU1(Wire, 0x68);

int status;
// #define I2C_ADDR 0x68

#define TCAADDR0 0x70

#define TCAADDR1 0x71

void tcaselect(uint8_t i) {
  if (i > 7) return;

  Wire.beginTransmission(TCAADDR0);
  Wire.write(1 << i);
  Wire.write(0x01);
  Wire.endTransmission();
  delay(100);
}

void tcaselect1(uint8_t j) {
  if (j > 7) return;

  Wire.beginTransmission(TCAADDR1);
  Wire.write(0x01);
  Wire.write(1 << j);

  Wire.endTransmission();
}



void setup() {
  Serial.begin(9600);
  Wire.begin();

  while (!Serial) {}

  Serial.println(F("##############################"));
  Serial.println(F("Starting Initialization"));
  Serial.println(F("##############################"));


  //*************INITIALIZING FIRST SENSOR*******************************
  tcaselect(0);
  if (IMU0.begin() != 0)
  {
    Serial.print(F("MPU.1 detected?\t"));
    Serial.println(F("No"));
  }
  else
  {
    Serial.print(F("MPU.1 detected?\t"));
    Serial.println(F("Yes"));
  }
  tcaselect(7);
  tcaselect1(0);
  if (IMU1.begin() != 0)
  {
    Serial.print(F("MPU.1 detected?\t"));
    Serial.println(F("No"));
  }
  else
  {
    Serial.print(F("MPU.1 detected?\t"));
    Serial.println(F("Yes"));
  }
  tcaselect1(7);
}
//*********************************************************************
//*************NOW LET'S START MEASURING*******************************
void loop()
{
  tcaselect(0);
  IMU0.readSensor();
  // display the data
  Serial.print("Accel_X: ");
  Serial.print(IMU0.getAccelX_mss(), 6);
  Serial.print("\t");
  Serial.print("Accel_Y: ");

  Serial.print(IMU0.getAccelY_mss(), 6);
  Serial.print("\t");
  Serial.print("Accel_Z: ");

  Serial.print(IMU0.getAccelZ_mss(), 6);
  Serial.print("\t");
  Serial.print("Gyro_X: ");

  Serial.print(IMU0.getGyroX_rads(), 6);
  Serial.print("\t");
  Serial.print("Gyro_Y: ");
  Serial.print(IMU0.getGyroY_rads(), 6);
  Serial.print("\t");
  Serial.print("Gyro_Z: ");
  Serial.print(IMU0.getGyroZ_rads(), 6);
  Serial.print("\t");
  Serial.print("Mag_X: ");
  Serial.print(IMU0.getMagX_uT(), 6);
  Serial.print("\t");
  Serial.print("Mag_Y: ");
  Serial.print(IMU0.getMagY_uT(), 6);
  Serial.print("\t");
  Serial.print("Mag_Z: ");
  Serial.print(IMU0.getMagZ_uT(), 6);
  Serial.print("\t");
  Serial.print("TEMP: ");
  Serial.println(IMU0.getTemperature_C(), 6);
  delay(100);
  tcaselect(7);
  
  tcaselect1(0);
  IMU1.readSensor();
  // display the data
  Serial.print("Accel_X: ");
  Serial.print(IMU1.getAccelX_mss(), 6);
  Serial.print("\t");
  Serial.print("Accel_Y: ");

  Serial.print(IMU1.getAccelY_mss(), 6);
  Serial.print("\t");
  Serial.print("Accel_Z: ");

  Serial.print(IMU1.getAccelZ_mss(), 6);
  Serial.print("\t");
  Serial.print("Gyro_X: ");

  Serial.print(IMU1.getGyroX_rads(), 6);
  Serial.print("\t");
  Serial.print("Gyro_Y: ");
  Serial.print(IMU1.getGyroY_rads(), 6);
  Serial.print("\t");
  Serial.print("Gyro_Z: ");
  Serial.print(IMU1.getGyroZ_rads(), 6);
  Serial.print("\t");
  Serial.print("Mag_X: ");
  Serial.print(IMU1.getMagX_uT(), 6);
  Serial.print("\t");
  Serial.print("Mag_Y: ");
  Serial.print(IMU1.getMagY_uT(), 6);
  Serial.print("\t");
  Serial.print("Mag_Z: ");
  Serial.print(IMU1.getMagZ_uT(), 6);
  Serial.print("\t");
  Serial.print("TEMP: ");
  Serial.println(IMU1.getTemperature_C(), 6);
  delay(100);
  tcaselect1(7);


  delay(1000);

}

thank you so much. it works

Now you can read my previous post to see what I changed in your sketch.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.