Is the idea of connecting AD0 pins of MPU9250 is still valid like the 2nd answer of this question? - How to change i2c address for mpu9250?
I have connected 2 sensor as test. SCL & SDA of both parallel with D1 & D2 of node mcu. Then connected AD0 of both sensors to different I/O pin of node mcu but it didnt work. What could be the solution?
Code to control 1 sensor:- It worked when I set AD0 to high and changed address to 0x69
#include "MPU9250.h"
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;
void setup() {
// serial to display data
Serial.begin(115200);
while(!Serial) {}
// start communication with IMU
status = IMU.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) {}
}
}
void loop() {
// read the sensor
IMU.readSensor();
// display the data
Serial.print(IMU.getAccelX_mss(),6);
Serial.print("\t");
Serial.print(IMU.getAccelY_mss(),6);
Serial.print("\t");
Serial.print(IMU.getAccelZ_mss(),6);
Serial.print("\t");
Serial.print(IMU.getGyroX_rads(),6);
Serial.print("\t");
Serial.print(IMU.getGyroY_rads(),6);
Serial.print("\t");
Serial.print(IMU.getGyroZ_rads(),6);
Serial.print("\t");
Serial.print(IMU.getMagX_uT(),6);
Serial.print("\t");
Serial.print(IMU.getMagY_uT(),6);
Serial.print("\t");
Serial.print(IMU.getMagZ_uT(),6);
Serial.print("\t");
Serial.println(IMU.getTemperature_C(),6);
delay(100);
}
Code to control 2 sensors:-
#include "MPU9250.h"
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU1(Wire,0x68);
MPU9250 IMU2(Wire,0x68);
int status;
void setup() {
// serial to display data
Serial.begin(115200);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
}
void IMU_1() {
digitalWrite(D7, LOW);
digitalWrite(D8, HIGH);
IMU1.begin();
IMU1.readSensor();
Serial.print("IMU 1 is active: "); Serial.print(IMU1.getAccelX_mss(),6);
digitalWrite(D8, LOW);
}
void IMU_2() {
digitalWrite(D7, HIGH);
digitalWrite(D8, LOW);
IMU2.begin();
IMU2.readSensor();
Serial.println("IMU 2 is active: "); Serial.print(IMU2.getAccelX_mss(),6);
digitalWrite(D7, LOW);
}
void loop() {
IMU_1();
IMU_2();
delay(500);
}
Result:- It Shows reading on one sensor but the 2nd sensor shows '0' every time.
What's the best way to use 3 I2C sensors when there is only 1 I2C bus?