Search code examples
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:

  • GND->GND
  • V3V->VCC
  • SDA->PIN 21 (SDA PIN)
  • SCL->PIN 22 (SCL PIN)

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