Try Embedded

Tune a PID loop (simulated heater)

Hold a heater at a set temperature with a PID controller, and tune Kp, Ki and Kd while watching the Serial Plotter.

Firmware · Advanced

In a real product: 3D printer hot end (Consumer electronics): A PID loop holds the nozzle at its temperature.

PID control holds something at a target: a temperature, a motor's speed, a drone's angle. Its three terms are easy to write and hard to tune, and tuning is learned by watching a loop respond. This project gives you a loop you can watch: a heater whose physics run inside the sketch, a setpoint on a knob, and three lines on the Serial Plotter.

What you'll learn

  • The proportional, integral and derivative terms, and what each one does to the response
  • A fixed sample period, measured with millis(), which every term depends on
  • Integral windup, and clamping the integral while the output is saturated
  • Taking the derivative of the measurement instead of the error, so a setpoint change does not kick the output

How it works

The plant is a first-order model of a small heater block: each 100 ms its temperature rises with the heater's power and falls towards the 20 °C room. It lives in the sketch so the loop has something to fight, and you can read every line of it. For a heater the simulator models itself, with dead time, sensor lag and a door that opens, see Heater PID.

The controller runs every 100 ms. The error is the setpoint (the potentiometer, 20–80 °C) minus the temperature. The output, 0–255, is Kp·error + integral − Kd·(change in temperature), clamped to the PWM range; the integral only accumulates while the output is not clamped (anti-windup). The LED on pin 9 shows the heater's power.

Open the Serial Plotter (the chart button above the Serial Monitor): setpoint, temp and power are plotted with a legend.

Try this

  • Set KI and KD to 0: the temperature settles below the setpoint. That gap is proportional control's offset.
  • Raise KP to 40: faster, then oscillating. Add KD to damp it.
  • Remove the anti-windup condition, set 80 °C, and watch the overshoot when it gets there.

sketch.ino

// A PID controller holding a simulated heater at the potentiometer's setpoint (A0: 20-80 C). The heater's power is
// the LED on pin 9; setpoint, temperature and power go to the Serial Plotter.
const float KP = 12.0, KI = 0.6, KD = 20.0;
const unsigned long PERIOD_MS = 100;

// ---- the plant: a heater block, warmed by its power and cooled towards the room ----
float temperature = 20.0;
void plantStep(int power) {
  const float room = 20.0, heating = 0.012, cooling = 0.02;
  temperature += heating * power - cooling * (temperature - room);
}

// ---- the controller ----
float integral = 0, lastTemperature = 20.0;
int power = 0;

int pid(float setpoint, float measured) {
  float error = setpoint - measured;
  float derivative = measured - lastTemperature;  // of the measurement: no kick when the setpoint moves
  lastTemperature = measured;
  float output = KP * error + integral - KD * derivative;
  if (output > 0 && output < 255) integral += KI * error;  // anti-windup: hold it while saturated
  return constrain((int)output, 0, 255);
}

void setup() {
  Serial.begin(115200);
  pinMode(9, OUTPUT);
}

void loop() {
  static unsigned long next = 0;
  if (millis() < next) return;
  next += PERIOD_MS;
  float setpoint = 20 + analogRead(A0) * 60.0 / 1023;
  power = pid(setpoint, temperature);
  analogWrite(9, power);
  plantStep(power);
  Serial.print("setpoint:");
  Serial.print(setpoint, 1);
  Serial.print(",temp:");
  Serial.print(temperature, 1);
  Serial.print(",power:");
  Serial.println(power / 255.0 * 100, 0);
}

Lessons behind it