I am integrating gyroscope values to get orientation data, but I have a few problems with my code.

When I move my IMU and make like 100 degrees, the IMU can detect the movement somewhat accurately. When I turn it back to its first place, it doesn't read 0. It reads something around 40 degrees which means a huge 40 degrees of error.

I'm not really sure of the reason for this problem. My best guess is my sampling of the IMU being too slow. In my code I also use an RF module and an SD card module which makes my code really slow. My code runs at a speed of 5 times a second (my MCU runs at 154MHz.) That is too slow for fast movements, I think.

Summary of the question is:

Is this error happening because of the slow sampling rate, or is it caused bysomething else? If it's because of slow sampling rate, how can I solve this problem and get 10s of datas from my IMU?

  • 2
    \$\begingroup\$ You will never fix this by sampling faster. You are integrating. So, as with all integrations there is that annoying "+C" at the end -- the unknown constant. In your case, you are finding that it is 40 degrees after that one integration process. Welcome to the real world. You'll need more sensors. \$\endgroup\$
    – jonk
    Aug 2, 2021 at 18:58
  • \$\begingroup\$ What do you mean by IMU? The IMU is a bunch of sensors calculated together by a fusion algorithm that outputs AHRS, but it could be also a mechanical device. Surely it is not made only of MEMS Gyro. \$\endgroup\$ Aug 2, 2021 at 22:14
  • 1
    \$\begingroup\$ 1 What integration scheme are you using ? 2 Are the forward rotation to 100 deg and the backward rotation to 0 deg at similar angular velocities ? 3 To see if drift /bias is the problem, keep the gyroscope still and see how much the angle drifts in a given duration of time. \$\endgroup\$
    – AJN
    Aug 3, 2021 at 13:06

2 Answers 2


Get a 9dof imu. If you are stuck using only gyroscopes: I would check your documentation since IMUs generally have features to help you out. I personally would use an IMUs low pass filter since it has a very high sample rate and as long as you aren't moving too fast should have a better response. It is important to have a high sample rate so that you can ignore higher frequency noise that doesn't alter your position significantly. The MPU6050 datasheet says you should read at a bare minimum of 200Hz. So yes your sample rate is way, way too slow. You are going to get high frequency noise folding over to low frequency and causing a total mess.

Also, don't assume that your gyros have a zero mean gausian noise distribution. They probably have an offset you may have to correct.


Gyroscopes have drift and error, to avoid this problem combine them with another sensor (like a magnetometer and feed the data into a kalman filter) to avoid the error with drift. OR get a 9dof sensor with sensor fusion.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.