# From thermostat to PID in one post

> A fast recap of feedback control for programmers: one heated box, a thermostat, and what each term of a PID controller fixes and breaks.

- Author: Radu Angelescu
- Published: 2026-11-24
- Topic: control
- Web page: https://raduangelescu.com/post/thermostat-to-pid/


This is the first post of a series about control. The series ends at BroodBoard, my controller board for an egg incubator, whose whole job is to keep a box at 37.5 °C.

Before the board there is the loop that runs on it. So this is a fast recap for programmers who have never tuned one: from the thermostat on the wall to PID, all on one simulated box. Every step is here because the one before it fails in a specific way, and you can make it fail yourself.

<!--more-->

## The box

The thing you control is called the plant. Mine is a box with a heater and a temperature sensor in it. The next post derives a model for it properly, today I only state the usual one, a first-order lag with a transport delay:

$$ \tau \frac{dT}{dt} = -(T - T_a) + K u(t - L) $$

$T$ is the temperature in the box, $T_a$ is the room and $u$ is the heater power, from 0 to 100 percent. $K$ says how many degrees one percent of power buys once everything has settled, and $\tau$ says how slowly the box gets there. $L$ is the dead time: for $L$ seconds after you touch the heater the sensor sees nothing at all, because the heat has to travel from the element to the sensor first.

All the widgets below run the same invented box: $T_a = 22$ °C, $K = 0.3$ °C per percent (so full power settles at 52 °C), $\tau = 180$ s and $L = 15$ s. These are not measurements of anything, I picked them so the plots are easy to read.

Between two ticks of a controller the heater power is constant, and for a constant input the equation has an exact solution: the temperature decays exponentially towards $T_a + K u$. So the simulation needs no numerical integrator and cannot blow up, whatever the step. The dead time is a queue of old heater values:

```javascript
function step(u, h) {    // u in percent, h in seconds
	queue.push(u);
	var target = TA + GAIN * queue.shift();
	T = target + (T - target) * Math.exp(-h / TAU);
}
```

## A thermostat

The simplest controller is one `if`: heater on below the setpoint $r$, off above it. To stop it from switching on every sample, you give it a band of width $w$. On below $r - w/2$, off above $r + w/2$, and inside the band it keeps doing what it was doing.

```c
if (temp < setpoint - band / 2) heater = true;
else if (temp > setpoint + band / 2) heater = false;
```

That bit of memory is called hysteresis. My game [Vatra](https://vatrathegame.com) has a night director that uses a hysteresis band for the same reason a thermostat does: a value sitting right on a single threshold would flip the state on every update.

<div class="lab" data-lab="thermostat" data-height="340">
<canvas></canvas>
<div class="lab__controls">
<label>Band <input type="range" name="band" min="0.1" max="3" step="0.1" value="1"><output data-unit=" °C"></output></label>
<label>Dead time <input type="range" name="dead" min="0" max="40" step="1" value="15"><output data-unit=" s"></output></label>
<button type="button" data-set="band=1,dead=15">Reset</button>
</div>
<p class="lab__hint">Shrink the band to 0.1 °C and look at the ripple. Then set the dead time to 0 and shrink the band again.</p>
</div>

It never settles. A thermostat has two outputs, 0 and 100 percent, and this box needs about 52 percent to stay at 37.5 °C, so all it can do is cycle forever.

The interesting part is that the temperature leaves the band on both sides. When the heater switches off, the heat that is already on its way keeps arriving for $L$ more seconds, and the same happens in reverse at the bottom. Put that into the exact solution from above and the peak to peak ripple comes out as

$$ \Delta T = w e^{-L/\tau} + 100 K (1 - e^{-L/\tau}) $$

The first term is the band. The second is what the dead time adds, and no band can remove it. For my box it is 2.4 °C: with the default 1 °C band the widget swings by 3.3 °C, and at 0.1 °C it still swings by 2.5 °C. All you got for the narrower band is a heater that switches on 60 times an hour instead of 45.

With no dead time the ripple really is the band, and the price is obvious. A 0.1 °C band switches the heater more than a thousand times an hour, and whatever does the switching wears out a little every time. That is the trade of on-off control: a narrow band costs switching, a wide band costs ripple, and the dead time sets a floor under both.

Stuart Bennett estimates that more than 75 000 automatic controllers were sold in the United States between 1925 and 1935, and most of them were simple on-off devices ([Bennett, 1993](https://doi.org/10.1109/37.248006)).

## Proportional control and the droop

The obvious fix is to stop slamming the heater between 0 and 100 and make the power proportional to the error $e = r - T$. So $u = K_p e$, clamped to 0..100: full power when the box is cold, less and less as it gets close.

<div class="lab" data-lab="pid" data-height="380">
<canvas></canvas>
<div class="lab__controls">
<label>Kp <input type="range" name="kp" min="1" max="100" step="1" value="30"><output></output></label>
<label>Dead time <input type="range" name="dead" min="0" max="40" step="1" value="15"><output data-unit=" s"></output></label>
<button type="button" data-action="step">Setpoint step</button>
<button type="button" data-set="kp=30,dead=15">Reset</button>
</div>
<p class="lab__hint">Raise the gain to close the gap between the two lines, with a setpoint step now and then. Then set the dead time to 0 and do it again.</p>
</div>

The ripple is gone, and so is the setpoint :). The temperature settles below it and stays there. This offset is called droop, and it is not a bug. To hold 37.5 °C the heater needs about 52 percent, and a proportional controller outputs nothing at zero error. It can only hold the box at a temperature where the error is big enough to pay for the heat. Set $dT/dt = 0$ in the model and you get that error:

$$ e_{ss} = \frac{r - T_a}{1 + K K_p} $$

At $K_p = 30$ this is 1.55 °C. More gain means less droop, so why not a gain of 1000?

Without dead time you could. A first-order lag never delays a sine wave by more than a quarter of a cycle, so the correction always arrives early enough to help. The dead time delays by $\omega L$, which grows with frequency without limit. At some frequency the two delays add up to half a cycle, the correction pushes exactly the wrong way, and if the loop gain there is at least one the oscillation feeds itself.

For $L \ll \tau$ the lag is close to its quarter cycle, so the dead time has to supply the other quarter: $\omega L = \pi / 2$. The lag passes that frequency with a gain of about $K / (\omega \tau)$, and the loop gain reaches one at

$$ K K_p \approx \frac{\pi \tau}{2 L} $$

For my box that is $K_p \approx 63$, and the widget gives up between 60 and 65. Past that point the output bangs between 0 and 100 percent: a proportional controller with too much gain is a thermostat again.

Put that limit into the droop formula and the best offset this box allows is about 0.8 °C, right on the edge of oscillating. At a sane gain it is twice that. Notice that $L / \tau$ set the floor for the thermostat and now sets it for the droop. If you want one number that says how hard a plant is to control, it is this one.

There is an old trick: add a fixed bias to the output, 52 percent here, so that zero error gives the right power. It works until something changes. Leave the door open or let the room cool down at night, and the right bias is a different number. In early controllers this bias was called the reset, and somebody adjusted it by hand ([Åström and Murray, chapter 11](https://www.cds.caltech.edu/~murray/books/AM08/pdf/fbs-pid_24Jul2020.pdf)).

## Integral and derivative

The integral term does that adjusting for you. Keep a running sum of the error and add it to the output. While there is any error left the sum keeps moving, so the loop can only come to rest at zero error. It needs no model of the box and it works for any constant disturbance. Åström and Murray call this "the magic of integral action".

It is not free. The sum reacts late by construction, so it adds delay to a loop that already has too much of it, and the response starts to ring.

The derivative term looks at how fast the error changes. Since $K_p e + K_d \dot e = K_p (e + T_d \dot e)$ with $T_d = K_d / K_p$, it is the proportional term applied to the error extrapolated $T_d$ seconds into the future. A prediction is exactly what a loop with dead time is missing, and it shows up as damping. All three together:

$$ u(t) = K_p e(t) + K_i \int_0^t e(s) ds + K_d \frac{de}{dt} $$

None of this was invented for heaters. In 1922 Nicolas Minorsky published [Directional stability of automatically steered bodies](https://doi.org/10.1111/j.1559-3584.1922.tb04958.x), where he analysed what a good helmsman does to keep a ship on course and ended up with the same three terms. His trials on the battleship New Mexico in 1923 went well, and the gear was removed anyway. A Navy captain explained why: "the operating personnel at sea were very definitely and strenuously opposed to automatic steering" ([Bennett, 1984](https://doi.org/10.1109/MCS.1984.1104827)). The process industry got there on its own, with pneumatic instruments. By 1939 the Taylor Fulscope and the Foxboro Stabilog both had all three terms. They called the integral "reset" and the derivative "pre-act" or "Hyper-reset" ([Bennett, 1993](https://doi.org/10.1109/37.248006)).

In firmware the integral becomes a sum and the derivative a difference. BroodBoard runs its controller once per second with an output from 0 to 100 percent, so the discrete version looks like this:

```c
#define DT 1.0f    // seconds between two calls

float pid_step(float setpoint, float temp) {
	float e = setpoint - temp;
	integral += ki * e * DT;
	float d = (prev_temp - temp) / DT;
	prev_temp = temp;
	float u = kp * e + integral + kd * d;
	return u < 0 ? 0 : u > 100 ? 100 : u;
}
```

This is the parallel form: three gains, one per term. The integral gain is applied before summing, so changing `ki` on a running loop does not make the output jump. And `d` is the derivative of the measurement, not of the error. They are the same while the setpoint stands still, but a setpoint step makes the error jump in one sample, and its derivative is a spike that goes straight to the heater.

<div class="lab" data-lab="pid" data-height="420">
<canvas></canvas>
<div class="lab__controls">
<label>Kp <input type="range" name="kp" min="1" max="100" step="1" value="30"><output></output></label>
<label>Ki <input type="range" name="ki" min="0" max="3" step="0.05" value="0"><output></output></label>
<label>Kd <input type="range" name="kd" min="0" max="1000" step="10" value="0"><output></output></label>
<button type="button" data-set="kp=30,ki=0,kd=0">P only</button>
<button type="button" data-set="kp=30,ki=1,kd=0">PI</button>
<button type="button" data-set="kp=30,ki=1,kd=200">PID</button>
<button type="button" data-action="step">Setpoint step</button>
<button type="button" data-action="door">Open the door</button>
<button type="button" data-action="cold">Cold start</button>
<label><input type="checkbox" name="noise" checked> Sensor noise</label>
</div>
<p class="lab__hint">Press "P only", "PI" and "PID" in turn. After each one press "Setpoint step" and then "Open the door", and watch the output as well as the temperature.</p>
</div>

With P only you get the droop again, and it depends on the load. An open door (50 percent more heat loss in this model) takes the offset from 1.55 to 2.2 °C, so no fixed bias would have saved you.

With PI the two lines meet, door open or closed. Look at the output: it settles at 52 percent with the door closed and at 78 with it open. Nobody told the controller those numbers, the sum found them. But every change now rings for five or six swings before it dies out.

With PID, same $K_p$ and $K_i$, the ringing is gone after one overshoot. And the output has turned into fuzz. The sensor noise in this widget is 0.03 °C, a thin fur on the temperature trace. Two noisy samples differ by about 0.04 °C, and $K_d = 200$ turns that into 8 percent of heater power on every sample. Push $K_d$ to 600 and the derivative makes the loop oscillate all by itself.

This is why the D is the term people leave out. Åström and Murray quote a survey of the controllers in more than 100 boiler-turbine units: 94.4% were PI and 3.7% were PID.

## What is still broken

The ten lines above are the textbook controller, and I would not let them near a real heater yet.

**Windup.** Select "PID" and press "Cold start". The heater sits at 100 percent for more than two minutes, and all that time the sum keeps growing over an error the controller can do nothing about. When the box reaches 37.5 °C the sum is huge, the heater stays on, and the temperature sails on to 46 °C. It stays above the setpoint for four minutes, which is not what you want next to eggs :).

**Derivative noise.** The fuzz has to go before anything switches a heater with it, without losing the damping.

**The gains.** I found 30, 1 and 200 by dragging sliders on a model that I made up, which is cheating twice. The first people with three knobs had the same problem. Bennett writes that "there was no established method of choosing the appropriate settings", until Ziegler and Nichols published one in 1942: [Optimum settings for automatic controllers](https://doi.org/10.1115/1.2899060) (the link is the 1993 reprint).

**Mains.** The output is a number from 0 to 100, and a heater on mains is either on or off.

Those are the next posts. First the box gets a real model.

## Further reading

* Karl Johan Åström and Richard Murray, [Feedback Systems: An Introduction for Scientists and Engineers](https://fbsbook.org), second edition. Free online, chapter 11 is about PID.
* Karl Johan Åström and Tore Hägglund, [PID Controllers: Theory, Design, and Tuning](https://portal.research.lu.se/en/publications/1aa6ddcb-00bd-4d86-92d2-b66d981c534f), ISA, 1995.
* Stuart Bennett, [Development of the PID controller](https://doi.org/10.1109/37.248006), IEEE Control Systems, 1993.
* Stuart Bennett, [Nicolas Minorsky and the automatic steering of ships](https://doi.org/10.1109/MCS.1984.1104827), IEEE Control Systems Magazine, 1984.

<script src="/js/labs/thermostat-to-pid.js"></script>

