Skip to content

Control Systems

How to filter the derivative term in a PID controller

The D term of a PID controller divides the change between two readings by the tiny time between them, so it multiplies sensor noise enormously. A jitter you'd never notice in the height becomes violent kicks in the output. The fix is to low-pass filter the derivative before using it. An exponential filter does it in one line, with one setting, alpha, that trades calm against speed.

How D amplifies noise

Give a drone's height sensor ±0.5 m of noise. Through the P term, with Kp 2, that's at most ±1 of thrust: a shrug. Through the D term it's a disaster. A 0.5 m jump between two readings 10 ms apart looks like a speed of 0.5 ÷ 0.01 = 50 m/s, and Kd 2 turns that into 100 units of thrust, from a drone that isn't moving.

The thrust slams between its limits of 0 and 30. Because it can push up by 20 above the 9.8 that hovers but down by only 9.8, the clipped kicks average out to more than hover, and the drone drifts upward. Noise has no direction; the limits give it one.

One line of filter

dfilt = dfilt + α × (d − dfilt)

Each pass nudges a running value a fraction alpha of the way towards the newest raw rate. Noise points every which way and cancels; a real rate lasts and comes through.

float d = (error - prev) / 0.01;     // raw rate
prev = error;
dfilt = dfilt + 0.05 * (d - dfilt);  // filtered rate
thrust = 9.8 + 2.0 * error + 2.0 * dfilt;

Live experiment

Filter a noisy D term

The lagging drone with ±0.5 m of sensor noise and Kp 2, Kd 2. Raw, the thrust slams between 0 and 30 and the drone drifts up past 26 m. Filter the derivative at alpha 0.2, 0.05 or 0.02: 0.2 still jitters, 0.05 is calm and settles in about 3.4 s, 0.02 is calm but overshoots by a metre.

Try it yourself: free lesson

“Noise and the D term” is lesson 5 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Choosing alpha

Alpha 0.05 averages roughly twenty readings, a time constant of about 0.2 s at 100 passes a second.

AlphaAverages aboutResult on this drone
1 (raw)1 readingThrust thrashes, drone drifts up past 26 m
0.25 readingsStill jittery
0.0520 readingsCalm, settles in about 3.4 s
0.0250 readingsCalm, but the late D lets it overshoot a metre

A filtered D is a slower D. Too little filtering and the noise gets through; too much and the damping arrives too late to stop a swing.

Other cures

  • Measure the rate directly. A gyro reads turning speed with no subtraction at all, which is why flight controllers use it for their D terms.
  • Lower Kd, if the loop can afford less damping.
  • Don't sample faster to fix it: a smaller time step makes the raw derivative noisier, not calmer.

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.

Try it yourself: free lesson

“Noise and the D term” is lesson 5 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3A smaller alpha makes the filtered D…
    Show the answer

    Answer: Calmer but slower to notice a real change. Alpha is the trade: small averages more readings, so noise cancels better, but a real change takes longer to show through.

    • Why not “Stop working”? The filtered D still damps, just a little later. Only an alpha of 0 would freeze it.
    • Why not “Faster and calmer”? Averaging more readings costs speed: a real change takes longer to show through.
    • Why not “Noisier”? A smaller alpha averages over more readings, so the noise cancels better, not worse.
  2. Question 2 of 3Drop the D term entirely to avoid the noise. The loop is…
    Show the answer

    Answer: Calm, but settles in ten seconds behind the lag. No D, no noise problem, and no damping either: back to the slow swing of the first unit.

    • Why not “Calm and fast”? Without D there's no damping, so behind the lag it swings for about ten seconds before settling.
    • Why not “Unstable”? Kp 2 alone still settles behind a 200 ms lag, just slowly.
    • Why not “Jittery”? The jitter came from the D term. With P alone, ±0.5 m of noise moves the thrust by only ±1.
  3. Question 3 of 3Alpha 0.2 instead of 0.05…
    Show the answer

    Answer: Averages only five readings: still jittery. A bigger alpha follows the raw rate more closely, noise included. The check's calm line needs about 0.05.

    • Why not “Stops the D”? The D term still acts. It just passes more of the noise through.
    • Why not “Is calmer”? A bigger alpha follows the raw rate more closely, noise included, so it's less calm.
    • Why not “Is the same”? Alpha sets how much each new reading counts: 0.2 weighs it four times as much as 0.05.
Keep practising: free lesson

“Noise and the D term” is lesson 5 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Questions people ask

Why is the derivative term so sensitive to noise?

It divides the change between two readings by the small time between them. Noise that moves a reading by n can change the difference by up to 2n, and dividing by 0.01 s multiplies that by 100: a gain of up to 200 on the noise, before Kd multiplies it again.

What alpha should I use for the derivative filter?

The smallest that keeps the output calm without making the loop overshoot. Alpha averages roughly 1 ÷ alpha readings: 0.05 is about twenty, a time constant of about 0.2 s at 100 passes a second. Too small and the D term reacts so late it stops damping.

Does sampling faster reduce D-term noise?

No, it makes it worse. The noise in each reading stays the same size while the time step shrinks, so the computed rate gets noisier. Filter the D term, or measure the rate directly with a sensor such as a gyro.

Every topic