30% off your first 3 months

Get the deal

How an IMU knows which way is down

by DuckyHelperNov 12, 20264 min read
Robotics

TL;DR:

  • At rest, an accelerometer measures gravity, which points the same way as "down", so its three readings give tilt.

  • A gyroscope measures rotation rate and fills in when the robot moves.

  • Motion adds acceleration that the accelerometer cannot tell apart from gravity, so tilt readings go wrong.

A robot picks itself up after a fall and needs to know whether it is lying flat or leaning on a wall. It has no eyes pointed at the floor, yet a chip smaller than a fingernail can answer. That chip is an inertial measurement unit, or IMU, and its trick for finding "down" is surprisingly simple.

What is inside an IMU

Most IMUs combine two sensors, each with three axes. An accelerometer measures acceleration along x, y and z. A gyroscope measures how fast the sensor is rotating about each axis, in degrees or radians per second. Some units add a magnetometer for heading, which makes a nine-axis unit.

Why an accelerometer at rest reads gravity

An accelerometer does not measure how fast something is moving. It measures the force on a tiny internal mass, expressed as acceleration. When the sensor sits still, the mass is being held up against gravity, so the accelerometer reports an acceleration of about 1 g, or 9.8 m/s², pointing up, away from the ground. That is what a scale in a lift would report as your weight.

So a sensor lying flat reports roughly (0, 0, 1) g, if its z axis points up. The signs depend on how the chip is mounted, so always check your sensor's axes. The key point is that the reading is a vector that points opposite to gravity, and that vector tells you the tilt.

Worked example: an accelerometer at rest reads gravity

Suppose a stationary robot's accelerometer reads (ax, ay, az) = (0.00, 0.50, 0.866) in units of g. First check that it really is gravity by taking the length of the vector:

√(0.00² + 0.50² + 0.866²) = √(0 + 0.25 + 0.75) = √1.00 = 1.00 g.

A length of 1 g confirms the sensor is at rest. Now find the tilt. The roll angle, the lean about the forward axis, is the angle between the vertical axis and the y component:

roll = atan2(ay, az) = atan2(0.50, 0.866) = 30°.

Check by going backwards: sin 30° = 0.50 and cos 30° = 0.866, which match the readings. For the other axis, the pitch comes from atan2(−ax, √(ay² + az²)). Here ax is 0, so the pitch is 0°. The robot is leaning 30° to one side and is not pitched at all.

Different libraries use different sign conventions for these formulas, so test yours: tilt the board by hand and check the sign of the angle that you get.

Why motion confuses it

The accelerometer cannot distinguish gravity from any other acceleration. If the robot speeds up forward at 0.2 g while sitting level, the reading becomes about (0.2, 0, 1) g. The tilt formula would then report a pitch of atan(0.2) ≈ 11.3° even though the robot is perfectly level. A sudden bump, a vibration from a motor or a hard turn does the same thing.

This is why the accelerometer is a trustworthy tilt sensor only when the robot is still or moving at a steady speed. During motion, its answer is noisy and sometimes wrong.

What the gyroscope adds

The gyroscope measures how fast the robot rotates, not where it points. If you add up rotation rate multiplied by time, you get the change in angle. This responds smoothly and ignores linear acceleration, so it handles the moments when the accelerometer is fooled. It has its own weakness: tiny errors build up over time (see the article on gyroscope drift).

Sensor

Good at

Bad at

Accelerometer

Absolute tilt when still, no drift

Fooled by acceleration, vibration and bumps

Gyroscope

Smooth, fast rotation tracking

Drifts slowly over time

Magnetometer

Heading (compass direction)

Disturbed by nearby metal and motors

Why gravity cannot give you yaw

Turn the robot on a flat floor, and the gravity vector does not change. Only the gyroscope (or a magnetometer) can say how the robot is turning about the vertical axis. That is why tilt, meaning roll and pitch, can come from gravity, while heading needs another source.

Tip: Check your sensor at rest before you trust it. If the length of the acceleration vector is not close to 1 g, either the unit is moving or it needs calibration.

Common mistakes

  • Using the accelerometer tilt formula while the robot is accelerating.

  • Forgetting the sensor's mounting orientation, so that the axes are swapped.

  • Mixing degrees and radians in the code.

  • Expecting an IMU to give position. Integrating acceleration twice drifts away very fast.

Your plan for this week

If you have an IMU board, print the three accelerometer values while you hold it still at different angles and confirm that the length stays near 1 g. Compute roll from the readings and compare it with a protractor. If you want to talk through the geometry, DuckyHelper can work through the tilt calculation with you on a blackboard, and then you can try it on the real sensor.

Frequently asked questions

Why does an accelerometer in free fall read zero?

It measures the force on its internal mass. In free fall, nothing holds the mass up, so the reading drops to near zero even though gravity is acting.

Is an IMU the same as a gyroscope?

No. An IMU usually bundles an accelerometer and a gyroscope, and sometimes a magnetometer, in one package.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.