arduinoesp32platformioarduino-esp32mpu6050

Failed to find MPU6050 with ESP32


I would like to use my MPU6050 together with my ESP32. (Link to the specific ESP Board)

It is not the first time for me using the MPU6050. I used it before in a project with an Arduino Uno and the MPU6050_light.h library. That worked perfectly. Now i'm trying it with my new ESP32.

I tried the MPU6050_light and Adafruit MPU6050 libraries but the first returns

MPU6050 status: 2

and the last throws the error:

Failed to find MPU6050 chip

The wires are definitely correct:

I used an I2C scanner to check if the MPU is found and what adress it has. And that worked too. It is the standart adress 0x68. I observed that when I pull out one wire and than connecting it again, the found adress changes to 0x69.

So the MPU was found. Has anyone any idea what could be the solution to my problem?

Code with MPU6050_light.h

#include <Arduino.h>
#include <MPU6050_light.h>
#include "Wire.h"

MPU6050 mpu(Wire);

long angle = 0;
unsigned long timer = 0;

void gyroSetUp()
{
  byte status = mpu.begin();
  Serial.print("MPU6050 status: ");
  Serial.println(status);
  while (status != 0)
  {
  } // stop everything if could not connect to MPU6050

  Serial.println("Calculating offsets, do not move MPU6050");
  delay(1000);
  mpu.calcOffsets(true, true); // gyro and accelero
  Serial.println("Done!\n");
}

void PrintAngles()
{
  Serial.print("X: ");
  Serial.print(mpu.getAngleX());
  Serial.print("\tY: ");
  Serial.print(mpu.getAngleY());
  Serial.print("\tZ: ");
  Serial.print(mpu.getAngleZ());
  Serial.print("\n");
}

void setup()
{
  Serial.begin(115200);

  Serial.println("Starting");
  Wire.begin();

  gyroSetUp();
}

void loop()
{
  mpu.update();

  if ((millis() - timer) > 1000)
  {
    PrintAngles();
    timer = millis();
  }
}

Code with Adafruit MPU6050

/* Get tilt angles on X and Y, and rotation angle on Z
 * Angles are given in degrees
 *
 * License: MIT
 */

#include "Wire.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;
sensors_event_t a, g, temp;
float gyroX, gyroY, gyroZ;
float gyroXerror = 0.07;
float gyroYerror = 0.03;
float gyroZerror = 0.01;
unsigned long lastTime = 0;
unsigned long gyroDelay = 10;

void initMPU()
{
  if (!mpu.begin())
  {
    Serial.println("Failed to find MPU6050 chip");
    while (1)
    {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");
}

void getGyroReadings()
{
  mpu.getEvent(&a, &g, &temp);

  float gyroX_temp = g.gyro.x;
  if (abs(gyroX_temp) > gyroXerror)
  {
    gyroX += gyroX_temp / 50.00;
  }

  float gyroY_temp = g.gyro.y;
  if (abs(gyroY_temp) > gyroYerror)
  {
    gyroY += gyroY_temp / 70.00;
  }

  float gyroZ_temp = g.gyro.z;
  if (abs(gyroZ_temp) > gyroZerror)
  {
    gyroZ += gyroZ_temp / 90.00;
  }

  Serial.print("X: ");
  Serial.print(String(gyroX));
  Serial.print("\tY: ");
  Serial.print(String(gyroY));
  Serial.print("\tZ: ");
  Serial.print(String(gyroZ));
  Serial.print("\n");
}

void setup()
{
  Serial.begin(115200);
  initMPU();
}

void loop()
{
  if ((millis() - lastTime) > gyroDelay)
  {
    getGyroReadings();
    lastTime = millis();
  }
}

Solution

  • I found the root of the problem.

    solution: Adafruit_MPU6050.h

    #define MPU6050_DEVICE_ID 0x68 ///< The correct MPU6050_WHO_AM_I value
    

    this "0x68" must be changed to 0x98

    ONLY in this line

    NOT here :

    #define MPU6050_I2CADDR_DEFAULT \
    0x68 ///< MPU6050 default i2c address w/ AD0 high
    

    it must stay the same 0x68

    In some case the device id may not be 0x98

    I met one chip returned 0x70

    Device WhoAmI reports as: 0x70
    

    You can use this code to read it, I got it from https://forum.arduino.cc/t/mpu-6050-a-module-problems-who-am-i-reports-0x98-not-0x68-as-it-should-fake-mpu-6050/861956

    it works for me

    void MPU6050_Read(int address,
                      uint8_t *data) {                                                    // Read from MPU6050. Needs register address, data array
        int size = sizeof(*data);                          //
        Wire.beginTransmission(MPU6050_I2C_ADDRESS);       // Begin talking to MPU6050
        Wire.write(address);                               // Set register address
        Wire.endTransmission(false);                       // Hold the I2C-bus
        Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); // Request bytes, release I2C-bus after data read
        int i = 0;                                         //
        while (Wire.available()) {                          //
            data[i++] = Wire.read(); // Add data to array
        }
    }
    
    
    void WhoAmI() {
        uint8_t waiByte;                                 // Data will go here
        MPU6050_Read(MPU6050_WHO_AM_I, &waiByte);        // Get data
        Serial.print(F("Device WhoAmI reports as: 0x")); //
        Serial.println(waiByte, HEX);                    // Report WhoAmI data
    }