Read Data of 2 MPU 9255 via a TCA9548A I2C Multiplex

hi guys!

I want to read data from my 2 MPU9255 with the help of the TCA9548A I2C multiplex. I need the Mux because of adress conflict with my RTC on the Datalogging-Shield.

The problem I am facing is that i cant read the two sensors at the same Time. If I use my sketch and just connect one IMU it reads the data perfectly. I don´t know if this Problem is caused by the library or my sketch

#include <Wire.h>
#include <MPU9255.h>

 
#define TCAADDR 0x70
MPU9255 mpu;



//setup the Multiplex 

void tcaselect(uint8_t i) 
{
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}
 
 
void setup(void) 
{
  Serial.begin(115200);
   /* Initialise the 1st sensor */
  tcaselect(2);
   mpu.init();
   
  /* Initialise the 2nd sensor */
  tcaselect(6);
   mpu.init();
  
  }
 
void loop(void) 
{

  tcaselect(2);
 //read data from all sensors 
  mpu.read_acc();
  mpu.read_gyro();
  mpu.read_mag();
//save all readings to 'float' variables
  float ax1=mpu.ax;
  float ay1=mpu.ay;
  float az1=mpu.az;

  float gx1=mpu.gx;
  float gy1=mpu.gy;
  float gz1=mpu.gz;

  float mx1=mpu.mx;
  float my1=mpu.my;
  float mz1=mpu.mz;

//---- Acceleration ---- 
// -> 16384 for +- 2g scale (default value) 
// -> 8192  for +- 4g scale
// -> 4096  for +- 8g scale
// -> 2048  for +- 16g scale 

ax1=ax1/16384;
ay1=ay1/16384;
az1=az1/16384;

//---- Gyroscope data ----
// -> 131   for +- 250  dps scale (default value) 
// -> 65.5  for +- 500  dps scale
// -> 32.8  for +- 1000 dps scale
// -> 16.4  for +- 2000 dps scale 

gx1=gx1/131;
gy1=gy1/131;
gz1=gz1/131;

const float cal=0.06;
mx1=mx1*cal;
my1=my1*cal;
mz1=mz1*cal;
mx1=mx1/0.6;
my1=my1/0.6;
mz1=mz1/0.6;

// print all data 
Serial.print("AX: "); 
Serial.print(ax1); 
Serial.print(" g"); 
Serial.print(" AY: "); 
Serial.print(ay1); 
Serial.print(" g"); 
Serial.print(" AZ: "); 
Serial.print(az1);
Serial.print(" g"); 
Serial.print("     GX: "); 
Serial.print(gx1); 
Serial.print(" dps"); 
Serial.print(" GY: "); 
Serial.print(gy1); 
Serial.print(" dps"); 
Serial.print(" GZ: "); 
Serial.print(gz1);
Serial.print(" dps"); 
Serial.print("     MX: "); 
Serial.print(mx1); 
Serial.print(" qT"); 
Serial.print(" MY: "); 
Serial.print(my1); 
Serial.print(" qT"); 
Serial.print(" MZ: "); 
Serial.print(mz1);
Serial.println(" qT");


//now the same for the other port

 tcaselect(6);
 //read data from all sensors 
  mpu.read_acc();
  mpu.read_gyro();
  mpu.read_mag();
//save all readings to 'float' variables
  float ax2=mpu.ax;
  float ay2=mpu.ay;
  float az2=mpu.az;

  float gx2=mpu.gx;
  float gy2=mpu.gy;
  float gz2=mpu.gz;

  float mx2=mpu.mx;
  float my2=mpu.my;
  float mz2=mpu.mz;



ax2=ax2/16384;
ay2=ay2/16384;
az2=az2/16384;


gx2=gx2/131;
gy2=gy2/131;
gz2=gz2/131;


mx2=mx2*cal;
my2=my2*cal;
mz2=mz2*cal;
mx2=mx2/0.6;
my2=my2/0.6;
mz2=mz2/0.6;

// print all data 
Serial.print("AX2: "); 
Serial.print(ax2); 
Serial.print(" g"); 
Serial.print(" AY2: "); 
Serial.print(ay2); 
Serial.print(" g"); 
Serial.print(" AZ2: "); 
Serial.print(az2);
Serial.print(" g"); 
Serial.print("     GX2: "); 
Serial.print(gx2); 
Serial.print(" dps"); 
Serial.print(" GY2: "); 
Serial.print(gy2); 
Serial.print(" dps"); 
Serial.print(" GZ2: "); 
Serial.print(gz2);
Serial.print(" dps"); 
Serial.print("     MX2: "); 
Serial.print(mx2); 
Serial.print(" qT"); 
Serial.print(" MY2: "); 
Serial.print(my2); 
Serial.print(" qT"); 
Serial.print(" MZ2: "); 
Serial.print(mz2);
Serial.println(" qT"); 
  delay(5000);
  
}

Wiring is the same as here: Adafruit Learning System

Post a link to the library you're using (it's not available in the IDE library manager) and to the used hardware (breakout board) as well!

I assume that you need two different MPU9255 objects but that depends on the implementation of the library.

Also provide a wiring diagram to show the rest of the setup (p.e. connections for the TCA9548A to the datalogging shield).

This is the library I´m using: GitHub - Bill2462/MPU9255-Arduino-Library: Simple arduino library for MPU9255 sensor

Breakoutboard: http://www.watterott.com/de/9-DOF-IMU-Module-With-MPU-9250

I use a shield that looks the same like this:
https://www.gearbest.com/development-boards/pp_69910.html?currency=EUR&vip=2639978&gclid=Cj0KCQiAjO_QBRC4ARIsAD2FsXO-NNqd8WTxBe1xbQ1Om9YVaY6GScHXWMzgYgGd15bBQ7NEAX_5l5YaAqHuEALw_wcB

and i wire it exactly the same way as without the shield.

I also think that there is a problem with the objects but I don´t know how to solve this properly.
Another strange thing is that reading one sensor just works when it is used with the shield, if used just with the arduino it doesnt work.

You need two object as they save the data in the object.

What RTC is on the shield. I couldn't find any documentation on that site. You should never buy from a supplier that doesn't offer documentation for the products.

I'm quite sure you don't need the TCA9548A but that depends on the used RTC.

and i wire it exactly the same way as without the shield.

So you put the shield directly on the Arduino? If you really have an address conflict with the RTC, that won't work. You have to connect the RTC to the TCA9548A too (to another bus on the chip). But if my assumption is correct you don't need the TCA9548A at all.

Yes I put the shield directly on the Arduino, it has the DS1307 as RTC, the I2C address of this part is 0x38.

The address of the IMU is also 0x38 by default, but can be changed to 0x39. I changed the address of both sensors to 0x39 and connected them to my TCA. So I dont have the Address conflict with my shield solved, but now the two Sensors are in conflict.

pylon:
If you really have an address conflict with the RTC, that won't work. You have to connect the RTC to the TCA9548A too (to another bus on the chip).

I doubt that it is possible to connect the data and the clockline to a bus of the TCA, because it is an SMD part. Do you have an idea of how I can do that?

The address of the IMU is also 0x38 by default, but can be changed to 0x39. I changed the address of both sensors to 0x39 and connected them to my TCA. So I dont have the Address conflict with my shield solved, but now the two Sensors are in conflict.

According to the datasheet of the MPU9255, the I2C address is 0x68 or 0x69 depending on the AD0 pin. So there should be no conflict, given that the RTC is really a DS1307.

I know that I can change the Address with the AD0 pin. My goal is to read two MPU 9255 at the same Time, so if I set the Address to 0x39 I get two Sensors with the same address and therefore it is not possible to read them.

Maybe I dont get how the I2C protokol works, but to my knowledge I cant read Sensors with the same address simultaneously or is there a possibility I don´t know about?

I know that I can change the Address with the AD0 pin. My goal is to read two MPU 9255 at the same Time, so if I set the Address to 0x39 I get two Sensors with the same address and therefore it is not possible to read them.

Just set one MPU to 0x68 and the other to 0x69, so you can read both and you have no conflict with the RTC. Do you have other sensors than you mention in the subject of your thread?