无法找到带有ESP32的MPU6050

问题描述 投票:0回答:3

我想将我的 MPU6050 与我的 ESP32 一起使用。 (链接到特定的 ESP 板)

这不是我第一次使用MPU6050。我之前在一个带有 Arduino Uno 和 MPU6050_light.h 库的项目中使用过它。效果非常好。现在我正在用我的新 ESP32 尝试它。

我尝试了 MPU6050_light 和 Adafruit MPU6050 库,但第一个返回了

MPU6050状态:2

最后抛出错误:

找不到MPU6050芯片

电线绝对正确:

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

我使用 I2C 扫描仪检查是否找到 MPU 以及它的地址。 这也有效。这是标准地址

0x68
。 我观察到,当我拔出一根电线并再次连接它时,找到的地址会变为
0x69

于是MPU就找到了。 有谁知道什么可以解决我的问题?

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

使用 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();
  }
}
arduino esp32 platformio arduino-esp32 mpu6050
3个回答
1
投票

我找到了问题的根源。

解决方案: Adafruit_MPU6050.h

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

这个“0x68”必须改为0x98

仅此行

不在这里:

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

它必须保持不变 0x68


0
投票

我找到了针对我的具体问题的解决方案。 这花了我一生的4个小时。 如果您使用 MPU6050_light.h,示例草图中会有错误的函数:

mpu.calcOffsets();

你必须将其更改为:

mpu.calcOffsets(true, true);

我不明白这个函数为什么要这样设计,但是不带参数使用是可以的。

我仍然不知道为什么其他库不起作用,但我希望我将来可以节省其他人的时间。


-1
投票

你能给我你已经更正的完整代码吗?我遇到了同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.