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.

Quick reference
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.

Accelerometer and gyroscope readings both feed a fusion filter that outputs a stable orientation estimate Accelerometer absolute, noisy Gyroscope smooth, drifts over time Fusion filter complementary / Kalman Orientation stable roll / pitch
Neither sensor alone gives a clean answer; the filter continuously trusts the gyro for fast changes and leans on the accelerometer to correct long-term drift.

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

Worth knowing

"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

Real-world applications

Gotchas

Field notes