2

I am trying to read multiple MPU 9250 sensors using TCA9548A multiplexer in ESP 32 and Arduino IDE. Has anyone done that successfully?

I am new to the area of Arduino coding. I am trying to edit the code in this tutorial for MPU 9250 (https://www.bluedot.space/tutorials/connect-multiple-sensors-using-i2c-multiplexer/). I am facing issues with libraries and commands while shifting from BME 280 to MPU 9250. Once I am changing to MPU 9250 and trying to call instance MPU9250 IMU_0, I am getting error "no matching function for call to 'MPU9250", which I understand is because the library I've used doesn't support the call. I am unable to find alternate library or command. How can this be sorted out?

#include <Wire.h>
#include <MPU9250.h>
MPU9250 mpu_0;
MPU9250 mpu_1;
MPU9250 mpu_2;
MPU9250 mpu_3;

#define TCAADDR 0x70
void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println(F("##############################"));            
  Serial.println(F("Starting Initialization"));
  Serial.println(F("##############################"));       
 
  //*************INITIALIZING FIRST SENSOR*******************************   
  tcaselect(0);
    
  if (mpu_0.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"));}
  //**********************************************************************
  //*************INITIALIZING SECOND SENSOR*******************************  
  tcaselect(1);  
    
  if (mpu_1.begin() != 0x68)
  { Serial.print(F("MPU Nr.2 detected?\t")); Serial.println(F("No"));}
  else
  { Serial.print(F("MPU Nr.2 detected?\t")); Serial.println(F("Yes"));}
  //*********************************************************************      
  //**********************************************************************  
  Serial.println();
  Serial.println(F("##############################"));            
  Serial.println(F("Initialization Finished"));
  Serial.println(F("##############################"));            
  Serial.println();
  Serial.println();
}
5
  • 1
    "I am facing issues". What issues?
    – timemage
    Dec 2 '20 at 17:38
  • 1
    Your code is nearly impossible to read. You might consider editing your question and using the code formatting facilities on the page.
    – jwh20
    Dec 2 '20 at 17:47
  • 1
    Which MPU9250 library are you using? Did you look at its examples to fix your code?
    – Mat
    Dec 2 '20 at 18:23
  • I see no question being asked
    – jsotola
    Dec 2 '20 at 19:32
  • I am using MPU 9250 library from bolder-flights system. In the tutorial about multiplexer, they have used BME280 and BlueDot_BME280library. Once I am changing to MPU 9250 and trying to call instance MPU9250 IMU_0, I am getting error "no matching function for call to 'MPU9250", which I understand is because the library I've used doesn't support the call. I am unable to find alternate library or command. What alternatives can I use or how can I achieve taking readings from Multiple MPU9250 simultaneously using a multiplexer? Thanks
    – Ajay Raj
    Dec 5 '20 at 14:14
1

You need to adapt the example you're following to follow how the MPU library does its construction and setup.

Looking at the documentation for the MPU library you are using, you should change the definitions to:

MPU9250 mpu_0(Wire, 0x68);  // 0x69 if you pulled AD0 high

Then if you read further to the "Common Setup Functions" section, you'll see that this library's setup function has this behavior:

This function returns a positive value on a successful initialization and returns a negative value on an unsuccesful initialization

So you need to adapt that too:

Serial.print(F("MPU.1 detected?\t"));
if (mpu_0.begin() < 0) { // the examples assume zero is ok too
  Serial.println(F("No"));
} else {
  Serial.println(F("Yes"));
}

You'll save yourself some trouble if you use an array for your sensors too.

MPU9250 mpus[] = { MPU9250(Wire, 0x68), MPU9250(Wire, 0x68) }; 
const int num_mpus = sizeof(mpus)/sizeof(mpus[0]);

Then your initialization can be done with a loop:

for (int i = 0; i < num_mpus; ++i) {
   tcaselect(i);
   if (mpus[i].begin() < 0) {
     // ...
   }
 }

And you can do similar things for interacting with the sensor. e.g.:

for (int i = 0; i < num_mpus; ++i) {
   tcaselect(i);
   mpus[i].readSensor();
   Serial.println(mpus[i].getAccelX_mss());
   // ...
 }
1
  • Thankyou very much... I would try implementing these..
    – Ajay Raj
    Dec 5 '20 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.