Skip to content
RoboLearn
Not completed

Accelerometers and gyroscopes (IMU)

SensorsIntermediate30 minTilt a virtual IMU ↓Before this: What is a sensor?

What you’ll learn

  • What accelerometers and gyroscopes measure
  • How to get tilt from gravity
  • Why gyroscopes drift, and how robots combine both

Accelerometer

Measures acceleration in three directions (x, y, z), including gravity. Lying flat and still, it reads about 9.8 m/s² (1 g) on the axis pointing up and about 0 on the others. Tilt it and gravity shifts between axes, which gives the tilt angle.

Gyroscope

Measures rotation speed around each axis (degrees or radians per second). Add up (integrate) the rotation speed over time and you get the angle turned.

IMU = both together

The popular MPU6050 has a 3-axis accelerometer and a 3-axis gyroscope, and talks over I²C (on an UNO: SDA → A4, SCL → A5).

Drift and noise

  • The accelerometer is noisy and gets confused by bumps and turns (they’re acceleration too), but it doesn’t drift.
  • The gyroscope is smooth, but tiny errors add up when you integrate, so the angle drifts over time.

Robots combine them with a complementary filter: mostly trust the gyro for quick changes, and slowly correct towards the accelerometer’s angle.

angle = 0.98 * (angle + gyroRate * dt) + 0.02 * accelAngle;

Robot uses

  • Turn exactly 90° (gyro)
  • Detect when the robot is tipping or has been picked up (accelerometer)
  • Balancing robots (both, plus PID control)

Finished this lesson?