Learn / Sensors and actuators / Motion and distance

IMUs and Sensor Fusion

ReviewedAdvanced ProAbout 6 min
BME280
In short

IMUs and Sensor Fusion: the short answer

An IMU (inertial measurement unit) such as the MPU6050 combines a 3-axis accelerometer and a 3-axis gyroscope; nine-axis parts such as the MPU9250 or BNO055 add a magnetometer. The accelerometer measures acceleration including gravity, so when the board is still it tells you which way is down, and tilt = atan2(ay, az).

Level
Advanced
Time
About 6 min
Topic
Sensors and actuators
You will learn to

Read an accelerometer and gyroscope, understand why each alone fails, and fuse them with a complementary filter for a stable tilt angle.

Before this: I²C Addressing & Shared Buses, Distance and Motion Sensors

An IMU (inertial measurement unit) such as the MPU6050 combines a 3-axis accelerometer and a 3-axis gyroscope; nine-axis parts such as the MPU9250 or BNO055 add a magnetometer. The accelerometer measures acceleration including gravity, so when the board is still it tells you which way is down, and tilt = atan2(ay, az). The gyroscope measures rotation rate in degrees per second, which you integrate over time to get an angle.

Neither is good enough alone. The accelerometer is noisy and is fooled by any movement: a robot accelerating forward looks tilted. The gyroscope is smooth and fast but has a small bias that integrates into drift, so its angle wanders by degrees per minute. A complementary filter combines their strengths: angle = α × (angle + gyro_rate × dt) + (1 − α) × accel_angle, with α around 0.98 at 100 Hz. The gyro handles quick changes and the accelerometer slowly pulls the angle back to the truth. A Kalman filter does the same job with a statistical model and is used when you need the best estimate; the BNO055 runs its own fusion and outputs orientation directly.

Calibrate before fusing. Keep the sensor still at start-up and average a few hundred gyro readings to measure the bias, then subtract it. Choose sensible full-scale ranges (±2 g and ±250 °/s give the best resolution for gentle motion) and enable the MPU6050's digital low-pass filter. Use a fixed loop rate and measure dt with micros(). Mount the IMU firmly near the centre of rotation, away from motors that vibrate it, and remember that heading from a magnetometer is disturbed by nearby steel and motor magnets.

Each sensor is good at one thing

A gyroscope measures turn rate and, integrated, gives the angle accurately over a short time but drifts. An accelerometer measures gravity and gives the tilt without drifting, but it is disturbed by any movement of the board. A magnetometer gives heading but is disturbed by nearby metal and currents.

Fusion combines them so that each covers the weakness of the others.

A simple filter that works

A complementary filter takes most of the angle from the integrated gyro and a small share from the accelerometer: angle = 0.98 × (angle + gyro × dt) + 0.02 × accelAngle. The gyro tracks quick movement, and the accelerometer slowly pulls the estimate back and removes drift. A Kalman filter or the Madgwick algorithm does a more complete job with similar ideas.

Use a steady, correct time step dt, and calibrate the gyro by averaging its reading while the board is still, then subtract that offset.

Limits to expect

Without a magnetometer, the heading (yaw) drifts and cannot be corrected. Vibration adds noise to the accelerometer, so mount the board firmly and filter it. Euler angles have a problem at ninety degrees of pitch, which is why quaternions are used inside most fusion code.

Common mistakes
  • Integrating a gyro alone and wondering why the angle drifts.
  • Using the accelerometer for tilt while the board is accelerating.
  • Using a changing time step in the gyro integration.
Advanced lesson · Pro plan

Practise this lesson with Pro

The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.

With Pro you also get: a worked example, a hands-on task, a 3-question check.

Common questions

What will I learn in "IMUs and Sensor Fusion"?

Read an accelerometer and gyroscope, understand why each alone fails, and fuse them with a complementary filter for a stable tilt angle.