MPU6050: Connection and Acceleration Offset failure

Dear Arduino Community,

I am struggling with the connection and setting of acceleration offsets for my MPU6050 with GY-521 breakout board. I am using the MPU6050 Arduino Library by Electronic Cats Version 0.0.2.

The following code and output accurately illustrate the problem:

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

MPU6050 mpu(0x68);

int16_t ax, az, gy;

void setup() {
  
    Serial.begin(9600);


    // Initialize MPU6050, test connection, set & get accel/gyro offsets
    
    mpu.initialize();

    Serial.println("Testing Connection...");
    Serial.print(mpu.testConnection() ? "Connection Success" : "Connection Failed");
    Serial.print("\n");

    Serial.println("Device ID..."); 
    Serial.print(mpu.getDeviceID());
    Serial.print("\n");
    
    mpu.setXAccelOffset(100);
    mpu.setYAccelOffset(100);
    mpu.setZAccelOffset(100);
    mpu.setXGyroOffset(100);
    mpu.setYGyroOffset(100);
    mpu.setZGyroOffset(100);
    
    Serial.print("ax_Off: "); Serial.print(mpu.getXAccelOffset()); Serial.print("\t");
    Serial.print("ay_Off: "); Serial.print(mpu.getYAccelOffset()); Serial.print("\t");
    Serial.print("az_Off: "); Serial.print(mpu.getZAccelOffset()); Serial.print("\t");
    Serial.print("gx_Off: "); Serial.print(mpu.getXGyroOffset()); Serial.print("\t");
    Serial.print("gy_Off: "); Serial.print(mpu.getYGyroOffset()); Serial.print("\t");
    Serial.print("gz_Off: "); Serial.print(mpu.getZGyroOffset()); Serial.print("\t");
    Serial.print("\n");   

}

void loop() {

    // Get Raw sensor data and Serial Print
    
    ax = mpu.getAccelerationX();
    az = mpu.getAccelerationZ();
    gy = mpu.getRotationY(); 

    Serial.print("ax: "); Serial.print(ax); Serial.print("\t");
    Serial.print("az: "); Serial.print(az); Serial.print("\t");
    Serial.print("gy: "); Serial.println(gy);

    delay(1000);
}

The output of the code:

Testing Connection...
Connection Failed
Device ID...
57
ax_Off: 0 ay_Off: 0 az_Off: 0 gx_Off: 100 gy_Off: 100 gz_Off: 100
ax: -992 az: 16228 gy: 636
ax: -968 az: 16156 gy: 630
ax: -1036 az: 16120 gy: 664
ax: -1040 az: 16184 gy: 622
ax: -968 az: 16076 gy: 653
ax: -964 az: 16080 gy: 635
ax: -968 az: 16172 gy: 646
ax: -1092 az: 16080 gy: 623
ax: -1044 az: 16036 gy: 630
ax: -1000 az: 16160 gy: 670
ax: -1064 az: 16088 gy: 632
ax: -1016 az: 16152 gy: 639
ax: -1036 az: 16108 gy: 622

Connection issue: The first issue is that mpu.testConnection() returns a boolean false. When searching for my device ID I am returned with 57 (or hexadecimal 0x39). After comparing this with the code in the MPU6050.ccp file, the testConnection() method is defined as follows:

bool MPU6050::testConnection() {
    return getDeviceID() == 0x34;
}

It is clear why this is returning me a boolean false and why I am getting a "Connection failed" response. Does that matter though? The MPU6050 still seems to be returning acceleration and gyro values that alter when physically handing the sensor.

Accleration Offset issue: The second issue is that I cannot set/get my acceleration offsets, while setting/getting gyro offsets works. Again, I checked the code within the MPU6050.cpp file and found the functions to be defined as follows:

int16_t MPU6050::getXAccelOffset() {
	uint8_t SaveAddress = ((getDeviceID() < 0x38 )? MPU6050_RA_XA_OFFS_H:0x77); // MPU6050,MPU9150 Vs MPU6500,MPU9250
	I2Cdev::readBytes(devAddr, SaveAddress, 2, buffer);
	return (((int16_t)buffer[0]) << 8) | buffer[1];
}
void MPU6050::setXAccelOffset(int16_t offset) {
	uint8_t SaveAddress = ((getDeviceID() < 0x38 )? MPU6050_RA_XA_OFFS_H:0x77); // MPU6050,MPU9150 Vs MPU6500,MPU9250
	I2Cdev::writeWord(devAddr, SaveAddress, offset);


int16_t MPU6050::getYGyroOffset() {
    I2Cdev::readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer);
    return (((int16_t)buffer[0]) << 8) | buffer[1];
}
void MPU6050::setYGyroOffset(int16_t offset) {
    I2Cdev::writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset);
}

I am assuming that the getDeviceID() < 0x38 portion of the acceleration offset code is causing the problem (because my getDivceID() provides me with a hexadecimal 0x39).

I am a totally new to Arduino and programming and am not sure how to proceed. Could I somehow set a new ID for my device (0x34)? Would that in fact solve these problem, or would I somehow be creating issues on other places within the code?

Your input, help and explanations would be very much appreciated!
Thanks,
Pedro

The device address depends on whether the AD0 input on the board is HIGH or LOW.

There is no need to set the offset registers in the MPU-6050. Just apply the offsets to the raw data after reading it out.

Finally, you don't need an MPU6050 library to read out the data, as this simple program demonstrates. You might need to change the device address to 0x69.

    // MPU-6050 Short Example Sketch
    // By Arduino User JohnChi
    // August 17, 2014
    // Public Domain
    #include<Wire.h>
    const int MPU_addr=0x68;  // I2C address of the MPU-6050
    int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
    void setup(){
      Wire.begin();
      Wire.beginTransmission(MPU_addr);
      Wire.write(0x6B);  // PWR_MGMT_1 register
      Wire.write(0);     // set to zero (wakes up the MPU-6050)
      Wire.endTransmission(true);
      Serial.begin(9600);
    }
    void loop(){
      Wire.beginTransmission(MPU_addr);
      Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
      Wire.endTransmission(false);
      Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      Serial.print("AcX = "); Serial.print(AcX);
      Serial.print(" | AcY = "); Serial.print(AcY);
      Serial.print(" | AcZ = "); Serial.print(AcZ);
      Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
      Serial.print(" | GyX = "); Serial.print(GyX);
      Serial.print(" | GyY = "); Serial.print(GyY);
      Serial.print(" | GyZ = "); Serial.println(GyZ);
      delay(333);
    }