Sensors & Signal Processing
How a complementary filter blends a gyro and accelerometer
A complementary filter blends two sensors that fail in opposite ways. A gyro measures how fast a board is turning; added up over time it gives a smooth angle that slowly drifts. An accelerometer measures which way gravity points; it gives an angle that never drifts but jitters. Trust the gyro for the short term, pull gently toward the accelerometer for the long term, and you get a tilt that's both steady and correct.
Two sensors, two flaws
A gyro reports a rate, degrees per second. To get an angle, add up rate × time on every sample. But no gyro is perfect: one that reads 2 °/s high when still adds a phantom 2° every second, and after 10 seconds the angle is 20° off. The error never stops growing.
An accelerometer, sitting still, measures gravity, so the tilt comes straight from its readings, with no adding up and no drift:
But any vibration or acceleration adds to gravity, so the angle jumps several degrees from sample to sample, and during a fast move it's confused by the motion itself.
One line blends them
Each step, the first part moves the angle by the gyro's rate, smooth and quick. The second part pulls it 2% of the way toward the accelerometer's angle. Fast changes come from the gyro, because 2% a step is too little to pass the jitter. Slow errors, like drift, get pulled out by that steady 2% before they can build up.
float angle = 0;
void loop() {
float gyroRate = readGyro(); // degrees per second
float accelAngle = readAccel(); // degrees, from atan2
angle = 0.98 * (angle + gyroRate * 0.01) + 0.02 * accelAngle;
delay(10); // 100 samples a second
}
Live experiment
Two sensors, one angle
The board sits level, tips to 30° over a second, holds, and comes back. The gyro reads 2 °/s high and the accelerometer jitters by about 5°. Gyro alone is smooth but drifts almost 20° off; accelerometer alone never drifts but jitters; the 0.98 and 0.02 blend tracks within about a degree.
“Sensor fusion” is lesson 4 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
Choosing the weight
The weight sets a time constant: errors slower than it are corrected by the accelerometer, changes faster than it come from the gyro.
With α = 0.98 at 100 samples a second, τ ≈ 0.5 s. At 0.5, half and half, the accelerometer's jitter comes straight through. At 0.99 the angle is calm, but the pull is too weak to hold a 2 °/s drift near zero, and it settles a couple of degrees off.
Where you'll meet it
Self-balancing robots, quadcopter flight controllers, camera gimbals and phones' motion sensing all fuse a gyro with an accelerometer. A Kalman filter does the same job with more maths and an estimate of each sensor's noise. On a microcontroller, this one line is usually enough.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Sensor fusion” is lesson 4 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: The accelerometer's angle. 98% gyro estimate, 2% accelerometer: a small, steady pull toward the angle that doesn't drift.
- Why not “The last angle”? The last angle carries the 98% part forward. The small pull each step is toward the accelerometer.
- Why not “Zero”? The pull is toward the accelerometer's measured angle, not toward zero.
- Why not “The gyro's rate”? The gyro's rate is already in the first part, added up into an angle; the 2% pull is toward the accelerometer.
Show the answer
Answer: Jitters: the accelerometer's noise comes straight through. Trusting the accelerometer that much passes most of its jitter. The harness measures about 1.8 degrees of wobble.
- Why not “Drifts”? With half its weight on the accelerometer, the angle is pulled hard toward a reading that doesn't drift.
- Why not “Freezes”? The angle still follows both sensors every step; nothing makes it stop moving.
- Why not “Is perfect”? Half and half passes half of every accelerometer jump straight into the angle, so it can't be perfect.
Show the answer
Answer: Slowly pull the estimate back so the gyro's drift can't build up. Gyro for the quick moves, accelerometer for the long run.
- Why not “Track fast moves”? Fast moves are the gyro's strength. During them the accelerometer is confused by the motion itself.
- Why not “Nothing”? Without the accelerometer's small pull, the gyro's drift would build up for ever.
- Why not “Measure rate”? Measuring rate is the gyro's job. The accelerometer gives an angle from the direction of gravity.
“Sensor fusion” is lesson 4 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 10 before it first.
Questions people ask
What is a complementary filter?
A simple way to combine two sensors that are good at opposite things. For tilt, it adds up the gyro's rate for quick, smooth changes and pulls the result gently toward the accelerometer's angle, which doesn't drift, so the errors of each are covered by the other.
What value should alpha be in a complementary filter?
Usually 0.95 to 0.99. The time constant is about alpha × Δt ÷ (1 − alpha): with 0.98 at 100 samples a second it's about half a second. Longer trusts the gyro more and smooths more jitter; shorter corrects drift faster.
Complementary filter or Kalman filter?
A Kalman filter can weigh the sensors optimally and adapt as conditions change, but it needs a model of the noise and more maths. For tilt on a hobby robot or drone, a complementary filter gets most of the benefit in one line of code.
Related topics
- How a self-balancing robot stays uprightBeat gravity's pull per degree, brake the swing, and a stick stands up.
- How an exponential moving average filter worksNudge a running value a fraction of the way toward each reading.
- How a robot knows where it is with dead reckoningAdd up every step in the direction you face, and you know where you are, roughly.
- How to calibrate a sensor with two known pointsTwo known readings fix a straight line: where it starts and how steep it is.