首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mpu6050连接失败

Mpu6050连接失败
EN

Stack Overflow用户
提问于 2021-05-25 12:35:04
回答 2查看 443关注 0票数 0

我正在为我的项目使用mpu6050,但它显示了以下错误测试设备连接...MPU6050连接失败仅当iI尝试使用MPU6050的对象时才显示此错误,但当我不使用MPU6050的实例而使用Wire库时(如follow),它可以工作-

代码语言:javascript
复制
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

但我想用这段代码-

代码语言:javascript
复制
 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);

请帮帮我

EN

回答 2

Stack Overflow用户

发布于 2021-05-26 18:00:48

我也有同样的问题。

问题来自MPU6050.cpp文件中的这些行。您可以在sketchbook文件夹中找到它。为此:打开Arduino IDE,然后选择File > Preferences > Sketchbook location。

代码语言:javascript
复制
//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函数

代码语言:javascript
复制
//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,这对我来说很有效。

代码语言:javascript
复制
//I2Cdev device library code is placed under the MIT license
//Copyright (c) 2012 Jeff Rowberg

bool MPU6050::testConnection() {
    return true;
}
票数 0
EN

Stack Overflow用户

发布于 2022-02-03 00:23:52

问题是设备标识符不是0x34 (在我的例子中是0x39)。为了不“作弊”,我做了以下更改:注释掉这一行:

代码语言:javascript
复制
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

并插入以下代码行:

代码语言:javascript
复制
Serial.print("DeviceID: ");
Serial.println(accelgyro.getDeviceID());

因此,程序将写入设备标识符,而不是给出它没有找到设备0x34的响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67681830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档