Heater PID (lag, dead time and a door)
Hold an aluminium block at 60 °C with a 20 W heater and an LM35: a slow plant whose sensor lags and whose heat arrives late, and a door that opens.
Control · Advanced
In a real product: Incubator (Medical): The chamber holds its temperature when the door is opened.
Temperature loops are slow and late. The heater warms a block that takes seconds to respond, the heat takes time to reach the sensor, and the sensor lags behind the block. A loop tuned like a motor's overshoots and rings. Here the block is a simulated plant: a 12 V, 20 W cartridge heater in a finned aluminium block on a MOSFET board, with an LM35 clamped to it. Open the World tool to watch it heat, settle, and fight a draught.
What you'll learn
- PWM as power: a MOSFET switching a heater, its average power the duty times the full 20 W
- A plant with a time constant, dead time and sensor lag, and why each one makes tuning harder
- Proportional only: the steady-state error it always leaves
- The integral that removes it, and why it must not wind up during the warm-up
- Disturbance rejection: the same loop when the door opens
How it works
While SIG is high the MOSFET puts 12 V across the 7.2 Ω cartridge, 20 W; PWM on SIG sets the share of that. The block loses 1 W for every 5 °C above the air, so flat out it heads for 100 °C above ambient with a 15 s time constant. The heat takes half a second to reach the sensor, and the sensor lags a second behind the block.
The LM35 is linked to the block (its "reads" setting in the circuit), so it gives 10 mV a degree of the block's temperature. Every 100 ms the sketch reads it and runs Kp·error + Ki·integral, clamped to 0–255. Holding 60 °C in 25 °C air takes 7 W, about a third of the duty: proportional control alone would need a large error to ask for it, so the integral supplies it. It integrates only while the output is not pinned, or the full-power warm-up would fill it and the block would sail past 60 °C.
The sketch prints setpoint:, temp: and duty: ten times a second, and reads h<°C> for a new setpoint and kp=, ki= for new gains.
Try this
- Run and open the World tool: the block warms flat out, then the loop eases off. Note the overshoot and settling time.
- Open the door from the World tool at 60 °C: the block now loses twice the heat. How far does it drop, and how long until the integral catches up?
- Set
ki=0: the block settles below 60 °C, and lower still with the door open. - Raise Kp until it oscillates: the dead time and the sensor lag are why it rings far sooner than the motor does.
- Change the air temperature from the World tool, as a cold morning would.
sketch.ino
// Heater PID: a 20 W heater block on a MOSFET board, SIG on pin 10, an LM35 on its block on A0.
// Serial: h<degrees C> sets the temperature; kp= and ki= set the gains.
const int HEAT = 10;
const unsigned long PERIOD_MS = 100;
float setpoint = 60, kp = 20, ki = 4;
float temp = 0, integral = 0;
int duty = 0;
unsigned long lastRun = 0;
String line;
void command(const String &c) {
if (c.startsWith("kp=")) kp = c.substring(3).toFloat();
else if (c.startsWith("ki=")) ki = c.substring(3).toFloat();
else if (c.startsWith("h")) { setpoint = c.substring(1).toFloat(); integral = 0; }
}
void setup() {
Serial.begin(115200);
pinMode(HEAT, OUTPUT);
}
void loop() {
while (Serial.available()) {
char ch = Serial.read();
if (ch == '\n') { command(line); line = ""; } else if (ch != '\r') line += ch;
}
unsigned long now = millis();
if (now - lastRun < PERIOD_MS) return;
float dt = (now - lastRun) / 1000.0;
lastRun = now;
temp = analogRead(A0) * 500.0 / 1023.0; // LM35: 10 mV a degree, against the 5 V reference
float error = setpoint - temp;
float u = kp * error + ki * integral;
if (u > 0 && u < 255) integral += error * dt; // anti-windup: not while the output is pinned
duty = constrain((int)u, 0, 255);
analogWrite(HEAT, duty);
Serial.print("setpoint:"); Serial.print(setpoint, 1);
Serial.print(" temp:"); Serial.print(temp, 1);
Serial.print(" duty:"); Serial.println(duty);
}