Skip to content

Control Systems

How a PD controller damps oscillation

A PD controller adds a derivative term to a proportional one. The P term pushes in proportion to the error; the D term pushes against how fast the error is changing. On the way to the target the error shrinks quickly, so D subtracts from the output and starts braking before the target is reached. It's the shock absorber that lets a loop arrive fast without bouncing past.

The control law

output = Kp × error + Kd × (error − prev) ÷ Δt

In code, for a drone holding its height with one pass every 10 ms:

float error = target - height;
float d = (error - prev) / 0.01;   // how fast the error changes, per second
prev = error;                      // ready for the next pass
thrust = 9.8 + 2.0 * error + 2.0 * d;

Worked numbers

One pass the error is 3.00 m; 10 ms later it's 2.98 m. Then d = (2.98 − 3.00) ÷ 0.01 = −2 m/s: the drone is closing at 2 m every second. With Kd 2, the D term takes 4 off the thrust while the P term still adds 2 × 2.98 ≈ 6. The faster the drone closes, the harder D brakes.

Why D matters most behind a lag

With a 200 ms sensor lag, P alone at Kp 2 swings for nearly ten seconds before it settles. A low gain, Kp 0.3, settles without overshoot but takes about seven. Add Kd 2 to Kp 2 and the drone settles in about 3.5 s. The rate shows where the drone is heading, so braking on it starts the correction early, exactly what a late sensor needs.

Live experiment

Add damping to a lagging drone

A drone behind a 200 ms sensor lag, with thrust = 9.8 + 2 × error + Kd × d. Kd 0 swings for ten seconds; Kd 1 or 2 settles in about 3.5 s; Kd 5 brakes so hard it creeps in at 8 s; Kd 20 fights every movement and never holds. Watch the thrust strip as well as the height.

Try it yourself: free lesson

“Damp the swing” is lesson 2 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 before it first.

Too much D

Kd (with Kp 2)What happens
0Swings for about ten seconds
1 or 2Settles in about 3.5 s
5Brakes the approach and creeps in at about 8 s
20Thrust slams between its limits; never holds

Getting D right in code

  • Update prev on every pass, after using it.
  • Divide by the real time between passes. Run the loop every 50 ms and divide by 0.01, and the kick is five times too strong.
  • Real sensors are noisy, and a derivative magnifies noise. Most D terms are low-pass filtered before use.

Robot arms, camera gimbals and quadcopter attitude loops all lean on derivative damping, because each has to move fast and stop dead.

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

“Damp the swing” is lesson 2 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 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 3Kd 20 instead of 2 makes the drone…
    Show the answer

    Answer: Fight every movement so hard it never settles. D is damping; too much of it is a brake on everything, including the approach. The drone creeps and jitters and never gets within half a metre for long.

    • Why not “Hover perfectly”? Every twitch becomes a huge rate times 20, so the thrust slams between its limits instead of holding steady.
    • Why not “Settle faster”? Past a point, more D brakes the approach itself. With Kd 20 the drone creeps and jitters instead of arriving.
    • Why not “Crash”? It doesn't fall: the P term still pulls it towards the target. It just never holds within half a metre.
  2. Question 2 of 3Why does D help most when there's a lag?
    Show the answer

    Answer: It reacts to how fast things are changing, so it starts braking before the stale height reaches the target. The rate of change says where the drone is heading. Braking on that starts the correction early, which is exactly what a late sensor needs.

    • Why not “It adds hover thrust”? Hover thrust is the constant 9.8. The D term adds nothing when the error is steady.
    • Why not “It removes the delay”? Nothing in the controller can remove the sensor's delay. Braking on the rate makes up for part of it.
    • Why not “It makes the sensor faster”? The sensor is just as late with D. The D term works from those same late readings.
  3. Question 3 of 3prev = error goes…
    Show the answer

    Answer: After the derivative is computed, every pass. The D term needs the previous pass's error. Update it after using it, every pass.

    • Why not “Only when the error changes”? Skipping updates makes the next difference span several passes while the code still divides by one step's time.
    • Why not “Inside setup()”? setup() runs once. prev must change on every pass, so the next pass compares with the latest error.
    • Why not “Nowhere”? Never updated, prev stays at its first value, and the D term stops measuring a rate at all.
Keep practising: free lesson

“Damp the swing” is lesson 2 of 6 in Lag Lagoon, Control Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 8 before it first.

Questions people ask

What does the D term do in a PID controller?

It pushes against the rate at which the error is changing. While the system races towards its target, the error is shrinking fast, so the D term subtracts from the output and brakes early. When nothing is moving, it adds nothing.

When is a PD controller enough, without the I term?

When something else takes care of the steady error. If the load is known and fed forward, or the system has no constant load to fight, P and D alone can arrive quickly and hold. Add an I term only if a steady droop remains.

Why does too much Kd make the response worse?

A big Kd treats every small movement as an emergency. It brakes the approach itself, so the system creeps in slowly, and it amplifies sensor noise into violent swings of the output.

Should the D term use the error or the measurement?

Many controllers take the derivative of the measurement instead of the error. They behave the same while the target is fixed, but when the target jumps, the error's derivative spikes and kicks the output, and the measurement's doesn't.

Every topic