'Mpu6050 Connection failed

I am using mpu6050 for my project but it shows following error - Testing device connections... MPU6050 connection failed this error is showed only when iI am trying to use object of MPU6050 but when I don't use instance of MPU6050 and use Wire library instead (like follow) it works -

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

but I want to use this code -

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

please help me



Solution 1:[1]

I had the same problem.

The problem comes from those lines in the MPU6050.cpp file. You can find it in your sketchbook folder. To do so: Open the Arduino IDE then 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;
}

This is the getDeviceId function

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

I just modified the testConnection function to return true, and it worked for me.

//I2Cdev device library code is placed under the MIT license
//Copyright (c) 2012 Jeff Rowberg

bool MPU6050::testConnection() {
    return true;
}

Solution 2:[2]

The problem is that the device identifier is not 0x34 (in my case it is 0x39). In order not to "cheat" I made the following change: Comment out this line:

Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

And inserted these lines:

Serial.print("DeviceID: ");
Serial.println(accelgyro.getDeviceID());

So the program will write the device identifier instead of giving a response that it didn't find device 0x34.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shunya