Sensors & Signal Processing
Build a digital level: calibrate the gyro, then fuse
A digital level reads tilt from an IMU's two motion sensors. The accelerometer feels gravity, so it's right on average but jitters. The gyro measures turning smoothly but has a small bias, a rate it reports even when still, which piles up into a growing error. Build the level in two steps: average the gyro while the board sits still to find its bias, then subtract it and fuse the two sensors in a complementary filter.
The spec
- The gyro reads 1.5 ° per second too high
- For the first second the board sits still and level
- After that it must read the tilt, and level again, to within 0.6 degrees on average, and stay calm
Step 1: find the bias
Still means the true turning rate is 0, so whatever the gyro reports on average is its own error. Average the first 100 readings, one second at 100 a second.
Step 2: fuse
From then on, subtract the bias and blend: 98 % gyro, to follow tilts smoothly, and 2 % accelerometer, to pull slowly back to the truth.
if (step >= 100) {
// step 2: fuse, with the gyro's error taken out
angle = 0.98 * (angle + (gyro - bias) * 0.01) + 0.02 * accel;
} else {
// step 1: the board is still, so the gyro's average is its bias
bias = bias + gyro / 100.0;
}
Why the bias step matters
The filter's pull from the accelerometer can't fully cancel a steady gyro error. It settles where the pull balances the drift:
A 1.5 ° per second bias leaves about 0.7 degrees of offset, and with the sensors' noise the level reads 0.8 to 1.1 degrees off: a fail. Calibrated, the measured bias is 1.45, only 0.05 is left, and the level reads within about 0.4 degrees.
Live experiment
Test three level designs
A board sits level for 2 s, tips to 30 degrees, then comes back, with a gyro that reads 1.5 ° per second high. Try the accelerometer alone (right on average, but jittery), the filter alone (calm, but 0.8 to 1.1 degrees off), or calibrating first: it finds a bias of 1.45 and reads within 0.4 degrees, calm.
“Project: Digital level” is the project that ends Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.
| Design | Steady error | Jitter |
|---|---|---|
| Accelerometer alone | right on average | about 3 degrees |
| Filter without calibration | 0.8 to 1.1 degrees | calm |
| Calibrate, then filter | 0.12 to 0.38 degrees | calm |
Finishing it
Show the angle on a small display, sound a beep within a tenth of a degree of level, and add a button that recalibrates on demand. The same two steps, find the bias, then fuse, sit inside phone screen rotation, camera stabilisers and every self-balancing robot.
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.
“Project: Digital level” is the project that ends Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 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: Its error, bias, which you subtract from then on. Still means the true rate is 0, so whatever the gyro reads on average is its own error.
- Why not “The tilt”? A gyro measures how fast it turns, not its angle. Held still its true rate is 0, so its average is its own error.
- Why not “The noise”? Averaging cancels the noise. What's left is the steady offset, the bias.
- Why not “Zero”? It would be zero only for a perfect gyro. A real one reports a small rate even when still.
Show the answer
Answer: Is calm but the gyro's drift pushes it about 2 degrees off. 1% isn't enough pull to hold the gyro's 2 ° /s error near zero.
- Why not “Jitters”? At 0.99 only 1 % of the accelerometer's jitter gets in each step, so the angle is calm.
- Why not “Is perfect”? 1 % isn't enough pull to cancel a gyro error of 2 degrees a second, which leaves a steady offset.
- Why not “Can't move”? The gyro part still follows every turn, so the angle tracks the tilt.
Show the answer
Answer: 4 times. √16 = 4.
- Why not “Not at all”? Averaging does cancel random noise, by the square root of the number of readings.
- Why not “16 times”? Random errors partly cancel each other, so the error shrinks by √16 = 4, not 16.
- Why not “2 times”? Halving the error takes only four readings. Sixteen shrink it by √16 = 4.
“Project: Digital level” is the project that ends Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.
Questions people ask
What is gyro bias?
The rate a gyro reports when it isn't turning at all. A gyro with a bias of 1.5 ° per second, left alone, adds up 90 degrees of imaginary tilt every minute. Averaging it while the device is still, then subtracting, removes most of it.
How long should the gyro calibration last?
Long enough for its noise to average away, short enough that nobody minds waiting: one second at 100 readings a second is typical. The device must be completely still, so many products calibrate at power-up and ask you not to touch them.
Why not just read the angle from the accelerometer?
An accelerometer measures gravity, so on average it's right, but every reading jitters and any movement or vibration adds acceleration that looks like tilt. The gyro is smooth over short times, and the filter uses each where it's strong.
Related topics
- How a complementary filter blends a gyro and accelerometerTrust the gyro for quick moves and the accelerometer for the long run.
- How to calibrate a sensor with two known pointsTwo known readings fix a straight line: where it starts and how steep it is.
- How oversampling adds resolution to an ADCAverage enough noisy readings and a 10-bit converter reads like a 14-bit one, for slow signals.
- 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.