无法使用ESP32找到MPU6050

发布于 2025-02-13 21:27:46 字数 3338 浏览 1 评论 0 原文

我想和我的 ESP32 。 (链接到特定的ESP板)

使用MPU6050对我来说不是第一次。我以前在带有Arduino Uno和MPU6050_LIGHT.H库的项目中使用了它。这很好。现在,我正在尝试使用新的ESP32。

我尝试了MPU6050_LIGHT和ADAFRUIT MPU6050库,但第一个返回

MPU6050状态:2

和最后一个错误:

找不到MPU6050芯片

绝对正确:

  • gnd-> gnd
  • v3v-&gtcc
  • sda-​​> pin 21(sda pin)
  • scl-&gt-&gt-> pin; pin 22(scl pin)

我使用I2C scanner来检查是否使用I2C Scanner来检查发现MPU以及它具有什么地址。 那也奏效了。它是Standart Adress 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();
  }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2025-02-20 21:27:46

我找到了问题的根源。

解决方案:
adafruit_mpu6050.h

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

更改为0x98,

此“ 0x68”必须仅在此行中

而不是在这里:

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

必须保持相同的0x68

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

胡大本事 2025-02-20 21:27:46

我为我的特定问题找到了解决方案。
我一生的时间花了4小时。
如果使用mpu6050_light.h,则在示例 - 凯奇中有一个错误的函数:

mpu.calcoffsets();

您必须将其更改为:

mpu.calcoffsets(true,true,true);

我不明白为什么以这种方式设计此功能,但是可以使用它没有参数。

我仍然不知道为什么其他图书馆无法使用,但我希望我将来能节省其他时间。

I found a fix for my specific problem.
It took 4h of my lifetime.
If you use the MPU6050_light.h there is a wrong function in the example-sketch:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文