⚙️ Control Theory · Engineering
📅 March 2026⏱ 11 min read🟡 Intermediate

PID Controller: From Drone to Thermostat

The PID controller — Proportional-Integral-Derivative — is the most widely deployed feedback control algorithm in existence, estimated to run in 90% of industrial control loops and in every quadcopter drone. Three terms, each fixing a different failure mode of naive feedback control.

1. Feedback Control Basics

A feedback control system compares a measured output (process variable, PV) to a desired target (setpoint, SP). The difference is the error e(t) = SP − PV. The controller uses this error to compute a control signal (output) that drives an actuator to reduce the error.

Example: a thermostat. Setpoint = 22°C. Room temperature = 18°C. Error = +4°C. Heater switches on. Temperature rises. Error shrinks. Heater reduces output. At steady state, temperature ≈ setpoint.

Naive proportional control alone has problems: overshoot, oscillation, and persistent steady-state error. The I and D terms fix these.

2. The Three PID Terms

P
Proportional
Corrects present error. Higher Kp = faster response but more overshoot and oscillation.
I
Integral
Corrects accumulated past error. Eliminates steady-state offset. Too large = wind-up oscillation.
D
Derivative
Anticipates future error (rate of change). Dampens overshoot. Amplifies noise — must be filtered.

3. The PID Equation

u(t) = Kp·e(t) + Ki·∫e(τ)dτ + Kd·(de/dt)

where:
u(t) = controller output (0–100%)
e(t) = setpoint − measured_value
Kp = proportional gain
Ki = integral gain
Kd = derivative gain

Alternatively expressed using Ti (integral time) and Td (derivative time):

u(t) = Kp × [ e(t) + (1/Ti)·∫e dτ + Td·(de/dt) ]

4. What Each Term Does

Proportional (P)

Produces an output proportional to the current error. If Kp is too low, response is sluggish. If Kp is too high, the system oscillates. On its own, P-only control always has a residual steady-state error: to maintain heat output, the temperature must stay below setpoint ("offset").

Integral (I)

Accumulates error over time. Even a small persistent error grows in the integral term, increasing control action until the error reaches zero. This eliminates steady-state offset. Downside: integral windup — if the actuator saturates (e.g., heater at 100%), the integral keeps growing. When the setpoint is reached, the accumulated integral overshoots badly. Anti-windup clamps the integral.

Derivative (D)

Responds to the rate of change of error, acting like a "brake" that anticipates overshoot. If the error is decreasing rapidly, D reduces the control action. This dramatically improves settling time. However, D amplifies high-frequency noise in the measurement — in practice, differentiate only the process variable (not the error) to avoid derivative kick on setpoint changes, and apply a low-pass filter.

5. Digital Implementation

class PIDController {
  constructor(kp, ki, kd, dt) {
    this.kp = kp; this.ki = ki; this.kd = kd; this.dt = dt;
    this.integral    = 0;
    this.prevError   = 0;
    this.outMin = -Infinity;
    this.outMax =  Infinity;
  }

  update(setpoint, measurement) {
    const error = setpoint - measurement;

    // Proportional
    const P = this.kp * error;

    // Integral with anti-windup clamping
    this.integral += error * this.dt;
    const I = this.ki * this.integral;

    // Derivative on measurement (avoids derivative kick)
    const D = this.kd * (this.prevError - error) / this.dt;
    this.prevError = error;

    const output = Math.min(this.outMax,
                    Math.max(this.outMin, P + I + D));

    // Back-calculate anti-windup: remove excess from integral
    if (output !== P + I + D) {
      this.integral -= error * this.dt; // revert last integration step
    }

    return output;
  }
}

6. Tuning Methods

Ziegler-Nichols

Classic empirical method: Set Ki = Kd = 0, increase Kp until the system oscillates with constant amplitude (ultimate gain Ku, ultimate period Tu). Then set: Kp = 0.6Ku, Ki = 2Kp/Tu, Kd = KpTu/8. Often gives aggressive tuning — fine-tune from there.

Manual Tuning

  1. Set Ki = Kd = 0; increase Kp until fast response with acceptable overshoot (~10–20%).
  2. Increase Ki slowly until steady-state error is eliminated without excessive oscillation.
  3. Add Kd to reduce overshoot; keep small to avoid noise amplification.
Most systems use PI only: The derivative term causes issues with noisy sensors (common in industrial settings). Many applications simply set Kd = 0 and accept slightly slower settling. Drones and servo systems typically do use all three terms with careful filtering.

7. Real-World Applications