ESP32 not connecting to MPU9500/6500

170 Views Asked by At

I am currently using a ESP-32s WROOM dev board v1.1 as a control module. I have connected this to a MPU9250/6500 as an IMU, using the following connections: Diagram of connections between the MPU9500/6500 and ESP32 As code for the ESP-32s WROOM dev board v1.1 i have used the Arduino IDE 2.2.1 (ALKS ESP32 as board) with the following code:

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

This is code from the turtorial here using the metioned library (which I have installed). Now, when I this code is run on the ESP32, the following result is returned in the Serial Monitor:

    IMU initialization unsuccessful
    Check IMU wiring or try cycling power
    Status: -1

This is obviously a problem, and this is why i am asking for help. Because if i understand this correctly, the ESP32 can't connect to the MPU9250/6500. (And yes i have checked the wiring is correct)

I tried the following:

1

There are 1 best solutions below

0
On BEST ANSWER

I figured it out. So it turns out that initializing the I2C-protocol using the Wire.begin(), for some reason doesn't use the standard SDA- and SCL-pins. To do that you have to initialize the I2C-communication using the line Wire.begin(21, 22), which sets the SDA- and SCL-pins to 21 and 22. To implement this you do:

TwoWire MainWire = TwoWire(0);
MPU9250 IMU = MPU9250(MainWire, 0x68);

in the start of the code. And the following in setup:

MainWire.begin(21,22); 
IMU.begin();