MPU6050 Offset Problem

I am trying to find offset for acceleration readings from MPU6050.

Everthing was fine when I try to print one axis only.

  Serial.println(Acc_X());
  //Serial.print(",");
  //Serial.print(Acc_Y());
  //Serial.print(",");
  //Serial.println(Acc_Z());

8.02
5.02
-5.98
3.02

but when I try to print two axis at the same time, the result are different.

  Serial.print(Acc_X());
  Serial.print(",");
  Serial.println(Acc_Y());
  //Serial.print(",");
  //Serial.println(Acc_Z());

-118.32,112.86
-125.32,111.86
-125.32,115.86
-119.32,105.86

Why is that? All I did was uncomment the following two line and the result had changed...
Here is the code

Test1.2.ino (4.64 KB)

#include "Wire.h"
#include "Math.h"

#define AccelerationOnly
//#define AngularVelocityOnly

//Calibration Times
int T = 50;

float AcXOffSet = 0, AcYOffSet = 0, AcZOffSet = 0,
      GyXOffSet = 0, GyYOffSet = 0, GyZOffSet = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();


  /*
      MPU6050 Setup
  */

  //Set acceleration rate
  Wire.beginTransmission(0x68);
  Wire.write(0x1C);
  Wire.requestFrom(0x68, 1, true);
  unsigned char acc_conf = Wire.read();
  //acc_conf = ((acc_conf & 0xE7) | (f << 3));
  //acc_conf = ((acc_conf & 0xE7) | (0 << 3));
  //acc_conf = ((acc_conf & 0xE7) | (1 << 3));
  //acc_conf = ((acc_conf & 0xE7) | (2 << 3));
  acc_conf = ((acc_conf & 0xE7) | (3 << 3));
  Wire.write(acc_conf);
  Wire.endTransmission(true);

  //Set angular velocity rate
  Wire.beginTransmission(0x68);
  Wire.write(0x1B);
  Wire.requestFrom(0x68, 1, true);
  unsigned char gyr_conf = Wire.read();
  //gyr_conf = ((gyr_conf & 0xE7) | (f << 3));
  //gyr_conf = ((gyr_conf & 0xE7) | (0 << 3));
  //gyr_conf = ((gyr_conf & 0xE7) | (1 << 3));
  gyr_conf = ((gyr_conf & 0xE7) | (2 << 3));
  //gyr_conf = ((gyr_conf & 0xE7) | (3 << 3));
  Wire.write(gyr_conf);
  Wire.endTransmission(true);

  //Calibration
  calibrateAcc();
  //calibrateGyr();


  //Acceleration only
#ifdef AccelerationOnly
  Serial.println("Acc_X Acc_Y Acc_Z");
#endif

  //Angular Velocity only
#ifdef AngularVelocityOnly
  Serial.println("Gyr_X Gyr_Y Gyr_Z");
#endif
}

void loop() {
  //Write Data

#ifdef AccelerationOnly
  Serial.print(Acc_X());
  Serial.print(",");
  Serial.print(Acc_Y());
  Serial.print(",");
  Serial.println(Acc_Z());
#endif

#ifdef AngularVelocityOnly
  Serial.print(Gyr_X);
  Serial.print(",");
  Serial.print(Gyr_Y);
  Serial.print(",");
  Serial.println(Gyr_Z);
#endif
}


//Read Acceleration on X axis
float Acc_X()
{
  Wire.beginTransmission(0x68);
  Wire.write(0x3B);
  Wire.requestFrom(0x68, 2, true);
  Wire.endTransmission(true);
  //return Wire.read() << 8 | Wire.read();
  return ((Wire.read() << 8 | Wire.read()) - AcXOffSet);
  //return ((16 * 9.8) * (Wire.read() << 8 | Wire.read()) / 32768);
  //return (16 * 9.8 * ((Wire.read() << 8 | Wire.read()) - AcXOffSet) / 32768);
}

//Read Acceleration on Y axis
float Acc_Y()
{
  Wire.beginTransmission(0x68);
  Wire.write(0x3D);
  Wire.requestFrom(0x68, 2, true);
  Wire.endTransmission(true);
  //return Wire.read() << 8 | Wire.read();
  return ((Wire.read() << 8 | Wire.read()) - AcYOffSet);
  //return ((16 * 9.8) * (Wire.read() << 8 | Wire.read()) / 32768);
  //return (16 * 9.8 * ((Wire.read() << 8 | Wire.read()) - AcYOffSet) / 32768);
}

//Read Acceleration on Z axis
float Acc_Z()
{
  Wire.beginTransmission(0x68);
  Wire.write(0x3F);
  Wire.requestFrom(0x68, 2, true);
  Wire.endTransmission(true);
  //return Wire.read() << 8 | Wire.read();
  return ((Wire.read() << 8 | Wire.read()) - AcZOffSet);
  //return (16 * 9.8 * ((Wire.read() << 8 | Wire.read()) - AcZOffSet) / 32768);
}

//Read Angular velocity on X axis
float Gyr_X()
{
  Wire.beginTransmission(0x68); 
  Wire.write(0x43); 
  Wire.requestFrom(0x68, 2, true); 
  Wire.endTransmission(true); 
  return (1000 * ((Wire.read() << 8 | Wire.read()) - GyXOffSet) / 32768);
}

//Read Angular velocity on Y axis
float Gyr_Y()
{
  Wire.beginTransmission(0x68); 
  Wire.write(0x45); 
  Wire.requestFrom(0x68, 2, true); 
  Wire.endTransmission(true); 
  return (1000 * ((Wire.read() << 8 | Wire.read()) - GyYOffSet) / 32768); 
}

//Read Angular velocity on Z axis
float Gyr_Z()
{
  Wire.beginTransmission(0x68); 
  Wire.write(0x47); 
  Wire.requestFrom(0x68, 2, true); 
  Wire.endTransmission(true); 
  return (1000 * ((Wire.read() << 8 | Wire.read()) - GyZOffSet) / 32768); 
}

void calibrateAcc()
{
  float X = 0;
  Acc_X();
  for (int i = 0; i < T; i++)
  {
    X += Acc_X();
    delay(10);
  }
  AcXOffSet = X / T;

  float Y = 0;
  Acc_Y();
  for (int i = 0; i < T; i++)
  {
    Y += Acc_Y();
    delay(10);
  }
  AcYOffSet = Y / T;

  float Z = 0;
  Acc_Z();
  for (int i = 0; i < T; i++)
  {
    Z += Acc_Z();
    delay(10);
  }
  AcZOffSet = Z / T;
}

void calibrateGyr()
{
  //Calibrate Gyr X
  float X = 0;
  for (int i = 0; i < T; i++)
  {
    X += Gyr_X();
    delay(10);
  }
  GyXOffSet = X / T;

  //Calibrate Gyr Y
  float Y = 0;
  for (int i = 0; i < T; i++)
  {
    Y += Gyr_Y();
    delay(10);
  }
  GyYOffSet = Y / T;

  //Calibrate Gyr Z
  float Z = 0;
  for (int i = 0; i < T; i++)
  {
    Z += Gyr_Z();
    delay(10);
  }
  GyZOffSet = Z / T;
}

Sorry, first time posting. How do I move it to the forum?

WLRM:
Sorry, first time posting. How do I move it to the forum?

It's already there