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