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

Ajay Raj
  • 63
  • 6

1 Answers1

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());
   // ...
 }
Mat
  • 464
  • 1
  • 4
  • 8