无法使用ESP32找到MPU6050
我想和我的 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了问题的根源。
解决方案:
adafruit_mpu6050.h
更改为0x98,
此“ 0x68”必须仅在此行中
而不是在这里:
必须保持相同的0x68
I found the root of the problem.
solution:
Adafruit_MPU6050.h
this "0x68" must be changed to 0x98
ONLY in this line
NOT here :
it must stay the same 0x68
我为我的特定问题找到了解决方案。
我一生的时间花了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.