I have an MPU6050 sensor, and I've ran and tested it many times. Every time I test it, the acceleration on axis Y and on axis Z is always zero, no matter what movement I do. The acceleration on axis X works just fine, and so do the angular accelerations on all three axes.
I've also bought a new MPU6050 from a different shop, and soldered the header pins myself. I've tested it and the exact same problem occurs. So I think it's not a hardware problem. Does anyone know what could be wrong?
Undoubtedly, the problem is in lines 45 through 49 of the code.
Its explained in the sticky threads how to post a question that can be answered.
You provide no useful information enabling any kind of answer, neither the circuit nor code,
so all that can be said is you have a problem somewhere in either or both...
The code I'm using is this:
```#include<Wire.h>
#include<MPU.h>
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,AcAbs,GyAbs;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
//----------------------------------------
Wire.beginTransmission(MPU_addr); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0b00011000); //Setting the gyro scale
Wire.endTransmission();
//----------------------------------------
Wire.beginTransmission(MPU_addr); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00011000); //Setting the accel scale
Wire.endTransmission();
Serial.begin(9600);
}
void loop(){
//read sensor values
Wire.beginTransmission(MPU_addr); //I2C address of the MPU
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(MPU_addr, 14); //Request Accel Registers (3B - 40)
while (Wire.available() < 14);
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)
//Units with division: m/s, rad/s
Serial.print("AcX = "); Serial.print(AcX/1000);
delay(1);
Serial.print(" | AcY = "); Serial.print(AcY/1000);
delay(1);
Serial.print(" | AcZ = "); Serial.print(AcZ/1000);
delay(1);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
delay(1);
Serial.print(" | GyX = "); Serial.print((GyX/50)*0.01745);
delay(1);
Serial.print(" | GyY = "); Serial.print((GyY/50*0.01745));
delay(1);
Serial.print(" | GyZ = "); Serial.print((GyZ/50)*0.01745);
delay(1);
Serial.print(" | AcAbs = "); Serial.print(AcX/1000+AcY/1000+AcZ/1000);
delay(1);
Serial.print(" | GyAbs = "); Serial.println((GyX/50+GyY/50+GyZ/50)*0.01745);
delay(100);
}
And the circuit is the one shown in this image: https://howtomechatronics.com/wp-content/uploads/2019/04/Arduino-and-MPU6050-Circuit-Diagram-768x400.png
One problem is the division by 1000, which throws away most of the bits in the result. And why are you setting new scale factors?
Try this code instead:
// 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);
}