Sensors
Motion & IMU — gyroscopes, drift, and sensor fusion
An accelerometer alone can't tell you which way something is facing when it's rotating around the vertical axis, and can't stay stable while genuinely moving. A gyroscope fills exactly that gap — and combining the two, imperfect as each is alone, is most of what "IMU" actually means in practice.
- What it is
- A gyroscope measures rotation rate; paired with an accelerometer (an IMU) and blended through a fusion filter, together they give a stable orientation.
- Mental model
- Gyro = fast but forgetful (drifts); accelerometer = slow but honest (noisy, but right on average). Fusion trusts each where it's strong.
- Key numbers
- 6-DOF = accel + gyro; 9-DOF adds a magnetometer for yaw; complementary filter alpha typically ~0.98.
- Reach for it when
- You need a stable orientation (roll/pitch, or full heading with 9-DOF) rather than raw, drifting or noisy sensor data.
How a MEMS gyroscope senses rotation
A MEMS gyroscope keeps a small mass continuously vibrating in one direction. When the whole chip rotates, the Coriolis effect deflects that vibrating mass sideways, perpendicular to both the drive direction and the rotation axis — and the size of that sideways deflection is proportional to how fast the chip is rotating. The deflection is picked up capacitively, essentially the same sensing principle as the accelerometer's proof mass, just measuring a Coriolis-induced motion instead of an inertial one. The output is angular velocity — degrees (or radians) per second around each axis — not an angle.
Integration drift
Since a gyroscope reports a rate, getting an actual orientation angle out of it means integrating that rate over time:
angle += gyro_rate * dt; // called every sample, dt in seconds
Any small, constant bias error in the gyroscope's reading — and every real gyroscope has one — gets integrated right along with the real signal. A bias of even a fraction of a degree per second, integrated continuously, adds up to a noticeably wrong orientation estimate within seconds to minutes, even while the sensor is sitting completely still. This is drift, and it's not a defect to be fixed in firmware so much as a fundamental property of integrating a noisy rate signal — it has to be corrected against some other, absolute reference instead.
Sensor fusion: complementary and Kalman filters
The accelerometer provides exactly the absolute reference the gyroscope lacks: gravity always points the same physical direction, so the accelerometer's reading gives an absolute tilt estimate — but it's noisy sample to sample, and briefly wrong whenever the sensor experiences any acceleration besides gravity (a bump, a turn). The two failure modes are complementary: gyroscope good short-term, bad long-term; accelerometer good long-term (on average), bad short-term. Sensor fusion blends them.
A complementary filter is the simplest version — cheap enough to run on almost anything:
angle = alpha * (angle + gyro_rate * dt) + (1 - alpha) * accel_angle;
// alpha close to 1 (e.g. 0.98) — mostly trust the gyro moment to moment,
// slowly pull the estimate back toward the accelerometer's answer
A Kalman filter does the same job more rigorously — modeling the uncertainty of each sensor statistically and computing an optimal blend rather than a fixed weighting — at the cost of real implementation complexity. In practice, most flight controllers and motion-tracking firmware use a purpose-built variant tuned for this exact problem (Madgwick and Mahony filters are two well-known examples) rather than a textbook Kalman filter from scratch.
6-DOF, 9-DOF, and what a magnetometer adds
An IMU (Inertial Measurement Unit) packages an accelerometer and gyroscope together — 3 axes each, commonly called 6-DOF (six degrees of freedom). That combination can stabilize roll and pitch (tilt relative to gravity) but still can't pin down yaw — rotation around the vertical axis — because gravity looks identical no matter which compass direction the sensor faces. Adding a magnetometer (sensing Earth's magnetic field, which does have a consistent horizontal direction) gives a 9-DOF system capable of a full, drift-corrected heading, at the cost of new problems: magnetometers are easily disturbed by nearby motors, magnets, and ferrous metal, so their correction has to be trusted far less aggressively than the accelerometer's.
Not the same "motion sensor": PIR detectors
"Motion sensor" in casual speech often means something completely different from anything above: a PIR (Passive Infrared) sensor, common in security lighting and alarm systems. A PIR sensor doesn't measure its own motion at all — it detects changes in infrared radiation within its field of view, which is what a moving warm body (a person, an animal) produces as it crosses between the sensor's segmented lenses. It's a completely different physical principle from a MEMS accelerometer or gyroscope, measuring something in the environment rather than anything happening to the sensor itself.
Applications
- Image/gimbal stabilization — fast gyro response counteracts shake faster than a control loop reacting to position alone could.
- Drone and robot attitude control — a fused roll/pitch/yaw estimate is the core input to any flight or balance controller.
- Gesture recognition — recognizing a shake, flip, or tilt pattern from raw or lightly filtered IMU data.
- Dead reckoning — estimating position change by double-integrating acceleration; workable for short distances, but errors compound quickly (double integration means position error grows even faster than the single-integration drift a gyroscope suffers) — not a substitute for GNSS over any real distance or time.
Real-world applications
- Drone flight controllers — fused IMU orientation is the core input every autopilot (e.g. Betaflight, ArduPilot) stabilizes against.
- Camera/gimbal stabilization — action cameras and handheld gimbals use fast gyro response to counteract shake in real time.
- Game controllers and VR headsets — 9-DOF fusion tracks controller and headset orientation for motion-based input.
- Robot balancing — self-balancing robots and segways depend on low-latency, drift-corrected tilt to stay upright.
- Step/gesture-based UI — shake-to-undo and flip-to-mute gestures on phones are pattern recognition over lightly filtered IMU data.
Gotchas
- Gyroscope bias drifts with temperature — a calibration done once at room temperature can be visibly wrong on a device that heats up in operation; some designs re-calibrate the zero-rate offset at startup while known to be stationary.
- A complementary filter's
alphais a real tuning tradeoff, not a fixed constant to copy from an example — too close to 1 and drift correction is too slow to matter; too far and accelerometer noise leaks straight into the output. - Sensor fusion corrects orientation drift, not position drift — it does nothing to make dead-reckoned position estimates trustworthy over any meaningful distance.