Im working on the project to read data from the imu9250 with two TCA9548A mux by connecting each imu on the both mux

im working on esp32 with two TCA95488a I2C multiplexer to read mpu 9250 (imu sensor). I'm` facing issues while i updated the code with two mux. i got results from single multiplexer. when i try to connect with the second one code compiles and im not receiving any data from the serial port.
i had attached the code for the reference purposes.

#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.endTransmission();  
}
void tcaselect1(uint8_t j) {
  if (j > 7) return;
 
  Wire.beginTransmission(TCAADDR1);
  Wire.write(1 << j);
  Wire.endTransmission();  
}

// void tcaselect(uint8_t i) {
//   if (i > 7) return;
// unsigned int mask = 1 << i;
//   Wire.beginTransmission(TCAADDR0);
//   Wire.write(lowByte(mask));
//   Wire.endTransmission();
//   Wire.beginTransmission(TCAADDR1);
//   Wire.write(highByte(mask));
//   Wire.endTransmission();
// }

// void tcaselect(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();

  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);   
 
}

Please edit your post above and correct it for use of code tags. Then post your schematic.

Also please explain why you are using 2 mux, and how many sensors you are using.

Why not just set the 2nd MPU to the alternate address?

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.

Please post an image of your project.

Please describe the problem better then you just did.

i have updated the post and thanks for reply!

i have updated the post. thank you!

To make those mux respond to different addresses, their address pins need to be connected differently. As far as I can see from your "schematic", you haven't done that. Therefore there two mux have the same address, and if you connect them to the same bus, neither will work and the bus will lock up as soon as your code tries to access them.

Sorry if this sounds harsh, but there's a reason you were asked for schematics. If you don't know what a schematic is, take a look on Google.

i had connected mux 0x70 address lines were not connected. Mux1 0x71 address lines a0 has connected to vcc. but the same issue persist

Are you deselecting the mux channel for the IMU that is inactive?
Additionally, why not a single multiplexer with the IMUs on different channels?

Im trying to read the 10 nos of imu so im in need of use both the mux. im deselecting the rest of the imu channel.

Which library are you using for the IMU? I have not found the correct one yet, so can't compile the code.

A few things I notice in the code you posted:

In setup, there are calls to begin() for the IMUs before you call the functions to select the multiplexer channels.

In loop, you have a call to the function to select the multiplexer channel for the IMU you want to read, but do not have anything to de-select the channel on the other multiplexer. That leaves both IMUs connected to the SPI bus at the same time.

The last tcaselect() function that you have commented out looks like it would work properly to de-select all the unwanted multiplexer channels over a 16-IMU system.

sorry i can't upload the file. i attached the link of the library for reference purpose.

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