如何解决Mpu6050 连接失败
我在我的项目中使用 mpu6050,但它显示以下错误 - 测试设备连接... mpu6050连接失败 仅当我尝试使用 mpu6050 对象时才会显示此错误 但是当我不使用 mpu6050 的实例而是使用 Wire 库时(如跟随),它可以工作 -
Wire.begin(); // Initialize communication
Wire.beginTransmission(mpu); // Start communication with mpu6050 // mpu=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); //end the transmission
但我想使用这个代码 -
mpu.initialize(); //start mpu
Serial.println(F("Testing device connections...")); //debugging serial statement
Serial.println(mpu.testConnection() ? F("mpu6050 connection successful") : F("mpu6050 connection Failed"));
// supply your own gyro offsets here,scaled for min sensitivity
mpu.setXGyroOffset(0);
mpu.setYGyroOffset(0);
mpu.setZGyroOffset(0);
mpu.setZAccelOffset(1688);
请帮帮我
解决方法
我遇到了同样的问题。
问题来自 MPU6050.cpp 文件中的那些行。您可以在您的速写本文件夹中找到它。 为此:打开 Arduino IDE,然后打开 File > Preferences > Sketchbook location。
//I2Cdev device library code is placed under the MIT license
//Copyright (c) 2012 Jeff Rowberg
/** Verify the I2C connection.
* Make sure the device is connected and responds as expected.
* @return True if connection is valid,false otherwise
*/
bool MPU6050::testConnection() {
return getDeviceID() == 0x34;
}
这是 getDeviceId 函数
//I2Cdev device library code is placed under the MIT license
//Copyright (c) 2012 Jeff Rowberg
/** Get Device ID.
* This register is used to verify the identity of the device (0b110100,0x34).
* @return Device ID (6 bits only! should be 0x34)
* @see MPU6050_RA_WHO_AM_I
* @see MPU6050_WHO_AM_I_BIT
* @see MPU6050_WHO_AM_I_LENGTH
*/
uint8_t MPU6050::getDeviceID() {
I2Cdev::readBits(devAddr,MPU6050_RA_WHO_AM_I,MPU6050_WHO_AM_I_BIT,MPU6050_WHO_AM_I_LENGTH,buffer);
return buffer[0];
}
我刚刚修改了 testConnection 函数以返回 true,它对我有用。
//I2Cdev device library code is placed under the MIT license
//Copyright (c) 2012 Jeff Rowberg
bool MPU6050::testConnection() {
return true;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。