cancel
Showing results for 
Search instead for 
Did you mean: 

MPU6050 gyroscope x and y axis data not right

M7890.1
Associate III

I have collected 200 samples of the gyroscope raw data and get the average to calculate the offset. After that, I leave the sensor on table and try to read the raw data. The raw data read should be close to the average value that I obtained at the beginning but they are not the same. I have printed out the value and attached the photo below. The first line is the offset (obtained by getting the average of 200 samples of raw data when the sensor is at rest), all the data after first line is the gyroscope raw data read after collecting the 200 samples. The code is also attached. Anyone has any idea on what's going on here?

0693W00000Bc2MEQAZ.pngEdit: I felt that the problem might be caused by the data type/ size of buffer but I can't point out the exact problem.

Edit: I have changed the type of sprintf buffer to uint8_t since the HAL_UART_Transmit() is expecting uint8_t. This corrected the value of X but Y is still wrong. I received this warning though: pointer targets in passing argument 1 of 'sprintf' differ in signedness [-Wpointer-sign]

4 REPLIES 4
TDK
Guru

Y and Z seem fine, right? Some drift is to be expected.

All of your X values have 0x01 as the lower byte. Only the upper byte changes value. Doesn't seem to be a problem with how you're reading it as far as I can tell though.

All your variables that get modified in the interrupt like gyro_x_raw should be defined volatile, although I'd be surprised if that fixed it.

If you feel a post has answered your question, please click "Accept as Solution".
M7890.1
Associate III

Z is fine. But Y should be somewhere around -60 if we look at the offset.

M7890.1
Associate III

Defining the variable as volatile does not solve the problem.

M7890.1
Associate III

If I print Z first, the value becomes wrong. Changing the size of the sprintf buffer seems to affect the value as well.