The gyro is smooth but drifts; the accelerometer is right on average but jumpy. Blend them and get the best of both.
Key idea
The complementary filter: $\theta \leftarrow \alpha\,(\theta + \omega\,\Delta t) + (1 - \alpha)\,\theta_{\text{acc}}$, with $\alpha$ close to 1, such as 0.98.
Why it works
Each step mostly trusts the gyro's quick update, so the result is smooth. But each step also pulls 2% toward the accelerometer, so over a second or two any drift is pulled back out. That is why it is called complementary: one sensor covers the other's weakness.
Worked example: One step
$\alpha = 0.98$, last $\theta = 10^\circ$, gyro 20 °/s, $\Delta t = 0.01$, accelerometer 12°.
- Gyro guess: $10 + 0.2 = 10.2$.
- $0.98 \times 10.2 + 0.02 \times 12 = 9.996 + 0.24 = 10.236$.
Answer: About 10.24°.
Common mistake
Swapping the weights and trusting the noisy accelerometer most.
Hands-on: a tilt meter
Wire an IMU board to your microcontroller. Print three angles side by side: accelerometer alone, gyro alone, and the complementary blend. Tilt the board and hold it; then shake it. Which one would you trust to balance a robot?