简体   繁体   English

使用 ESP32 找不到 MPU6050

[英]Failed to find MPU6050 with ESP32

I would like to use my MPU6050 together with my ESP32 .我想将我的 MPU6050 与我的ESP32一起使用。 (Link to the specific ESP Board) (链接到特定的 ESP 板)

It is not the first time for me using the MPU6050.这不是我第一次使用 MPU6050。 I used it before in a project with an Arduino Uno and the MPU6050_light.h library.我之前在一个带有 Arduino Uno 和 MPU6050_light.h 库的项目中使用过它。 That worked perfectly.那工作得很好。 Now i'm trying it with my new ESP32.现在我正在尝试使用我的新 ESP32。

I tried the MPU6050_light and Adafruit MPU6050 libraries but the first returns我尝试了 MPU6050_light 和 Adafruit MPU6050 库,但第一个返回

MPU6050 status: 2 MPU6050状态:2

and the last throws the error:最后一个抛出错误:

Failed to find MPU6050 chip找不到MPU6050芯片

The wires are definitely correct:电线绝对正确:

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

I used an I2C scanner to check if the MPU is found and what adress it has.我使用 I2C 扫描仪检查是否找到了 MPU 以及它的地址。 And that worked too.这也奏效了。 It is the standart adress 0x68 .它是标准地址0x68 I observed that when I pull out one wire and than connecting it again, the found adress changes to 0x69 .我观察到,当我拔出一根电线并再次连接时,找到的地址变为0x69

So the MPU was found.所以找到了MPU。 Has anyone any idea what could be the solution to my problem?有谁知道什么可以解决我的问题?

Code with MPU6050_light.h带有 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使用 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();
  }
}

I found a fix for my specific problem.我找到了解决我的具体问题的方法。 It took 4h of my lifetime.这花了我一生的 4 个小时。 If you use the MPU6050_light.h there is a wrong function in the example-sketch:如果您使用 MPU6050_light.h,则示例草图中有错误的功能:

mpu.calcOffsets();

You have to change it to:您必须将其更改为:

mpu.calcOffsets(true, true);

I don't understand why this function is designed in that way, but it is ok to use it without parameters.我不明白为什么这个函数是这样设计的,但是不带参数使用它是可以的。

I still don't know why the other library won't work, but I hope I've saved others time in the future.我仍然不知道为什么其他图书馆不能工作,但我希望我以后能节省其他人的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM