Hi,
I was messing around with a gyroscope and accelerometer (MPU 6050) and stumbled on a weird issue...
Here's the code that reads the data from the sensor : (that void is read in a loop with nothing but "getSensorData();" in).
void getSensorData() {
// MPU-6050 data registers :
// 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
// 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
// 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
// 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
// 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
// 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
// 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
// Gyroscope Data
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.requestFrom(MPU, 3*2, true);
Wire.endTransmission(false);
time = (micros()-clock) / (float) 1000000;
clock = micros();
gyro_x = Wire.read()<<8 | Wire.read();
gyro_y = Wire.read()<<8 | Wire.read();
gyro_z = Wire.read()<<8 | Wire.read();
Wire.endTransmission(true);
Serial.print("\n[Gyro X : "), Serial.print(gyro_x), Serial.print(" ]");
Serial.print("\n[Gyro Y : "), Serial.print(gyro_y), Serial.print(" ]");
Serial.print("\n[Gyro Z : "), Serial.print(gyro_z), Serial.print(" ]");
// Accelerometer Data
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.requestFrom(MPU, 3*2, true);
Wire.endTransmission(false);
acc_x = Wire.read()<<8 | Wire.read();
acc_y = Wire.read()<<8 | Wire.read();
acc_z = Wire.read()<<8 | Wire.read();
Wire.endTransmission(true);
Serial.print("\n[Acc X : "), Serial.print(acc_x), Serial.print(" ]");
Serial.print("\n[Acc Y : "), Serial.print(acc_y), Serial.print(" ]");
Serial.print("\n[Acc Z : "), Serial.print(acc_z), Serial.print(" ]");
}
The first gyro register is 0x43 and the first accel register is 0x3B.
You tell both data appart as the gyroscope data should read around 500 on the x axis and the accelerometer's should read around 17000 on its z axis.
My issue is that the data prints out inverted : the accel data is printed in the gyro variables, etc... (I have a screenshot attached)
The variables and registers are attributed to the correct data and printed as they should be.
Everything seems like the code is executing properly but that the gyroscope register became the accelerometer register and vice versa.
The funny part is that if I delete or comment out either the accelerometer part or the gyroscope, as following, the results change drastically :
void getSensorData() {
// Gyroscope Data
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.requestFrom(MPU, 3*2, true);
Wire.endTransmission(false);
time = (micros()-clock) / (float) 1000000;
clock = micros();
gyro_x = Wire.read()<<8 | Wire.read();
gyro_y = Wire.read()<<8 | Wire.read();
gyro_z = Wire.read()<<8 | Wire.read();
Wire.endTransmission(true);
Serial.print("\n[Gyro X : "), Serial.print(gyro_x), Serial.print(" ]");
Serial.print("\n[Gyro Y : "), Serial.print(gyro_y), Serial.print(" ]");
Serial.print("\n[Gyro Z : "), Serial.print(gyro_z), Serial.print(" ]");
// // Accelerometer Data
// Wire.beginTransmission(MPU);
// Wire.write(0x3B);
// Wire.requestFrom(MPU, 3*2, true);
// Wire.endTransmission(false);
//
// acc_x = Wire.read()<<8 | Wire.read();
// acc_y = Wire.read()<<8 | Wire.read();
// acc_z = Wire.read()<<8 | Wire.read();
//
// Wire.endTransmission(true);
//
// Serial.print("\n[Acc X : "), Serial.print(acc_x), Serial.print(" ]");
// Serial.print("\n[Acc Y : "), Serial.print(acc_y), Serial.print(" ]");
// Serial.print("\n[Acc Z : "), Serial.print(acc_z), Serial.print(" ]");
}
I attached a screenshot : I didn't even touch the gyroscope code but it now prints the correct value !
It does the same if I remove the gyroscope code instead : the accelerometer prints the correct value.
They are interacting with eachother, but I can't what is doing that...
The only way I can successfully get the data I want, where I want, is if I invert the registers myself and consider the register 0x3B to actually be the gyroscope and the register 0x43 to actually be the accelerometer.
I'm sure it's a dumb mistake on my part, but I really can't tell why this is happening...

