Industry

PID controller

A PID controller is a control algorithm that keeps a measured value, such as a temperature, a motor's speed or a tank's level, at its target by combining proportional, integral and derivative corrections.

Orange robot arms on an automotive assembly line

Photo: Simon Kadula / Unsplash

A PID controller is a piece of logic that keeps a quantity where you want it. It compares a reading from a sensor, say the water temperature in a tank, with the target value, called the set point, and works out how hard an actuator such as a heater, a valve or a motor should work. The gap between target and reading is called the error, and the name lists the three ways the controller reacts to it: Proportional, Integral and Derivative. The idea is old: an early theoretical study of this kind of control, published by N. Minorsky in 1922 in the Journal of the American Society for Naval Engineers, dealt with steering ships automatically. Today the same arithmetic runs in factory controllers, ovens, drones, 3D printers and cruise control.

What do P, I and D do, in simple words?

Each term answers a different question about the error. Take an electric water heater set to 60 °C.

  • Proportional: how far am I from the target right now? The heater’s power is proportional to the gap: full power when the water is cold, a little when it is close. P alone usually settles slightly below the target: at zero gap its output is zero, yet the tank keeps losing heat.
  • Integral: how long have I been off? The integral term adds up the error over time, so a small gap that lasts keeps raising the power until it closes. Too much of it makes the temperature overshoot and swing around the target.
  • Derivative: how fast is the error changing? If the water is warming quickly toward the target, D eases off the power early, the way a driver lifts off the accelerator before a red light. It reduces overshoot but also reacts to random flicker in the sensor reading, so many loops use little or no D and are called PI controllers.

The output is the sum of the three terms, each multiplied by its own weight, called a gain: Kp, Ki and Kd. The same logic holds a motor’s speed when the load changes, or a tank’s level by opening a valve more or less.

What is a PID loop?

A PID loop is the closed circle of measuring, calculating, acting and measuring again. It repeats at a fixed interval. Engineers call this closed-loop control, as opposed to open-loop control such as a toaster’s timer, which never checks whether the bread is done.

How often it runs depends on how fast the process moves: every few seconds for slow heating, many times a second for a motor. The maxon EPOS4 motor controllers, for example, list a “PID positioning controller” running at 2.5 kHz, that is 2,500 times a second. Loops are often stacked in a cascade: an outer loop for position sets the target of an inner loop for speed. A process plant runs many loops side by side, watched by operators on the screens of a distributed control system (DCS).

How do you tune a PID controller?

Tuning means choosing the three gains for a specific process; there are no universal values, because a kettle and a furnace react very differently.

  • By hand. Start with P only and raise it until the response is quick with a slight swing, add I until the offset disappears, and add D only if overshoot is a problem.
  • By rule. One recipe in the classic paper by Ziegler and Nichols raises P until the process swings steadily, then turns that gain and the length of one swing into the three gains with simple formulas. The result tends to be aggressive, so it is often only a starting point.
  • Automatically. The controller tests the process itself. ESPHome, open-source firmware for home devices, has a PID climate component whose autotune “works by repeatedly switching the heat/cool output to full power and off”, measures the swings and calculates the gains with a method based on Ziegler-Nichols. The Marlin firmware for 3D printers does the same with its M303 command, heating and cooling the nozzle or bed for several cycles.

Every tuning is a trade-off between a fast response, a small overshoot and wear on the valve or motor that has to follow the commands.

Where does a PID controller run?

Anywhere a processor sits between a sensor and an actuator. In factories it runs in a programmable logic controller (PLC) as a ready-made function block: Siemens describes its PID_Compact block as “a continuous PID controller with integrated optimization” that calculates the three parameters during pretuning and refines them with fine tuning.

In products, it runs in microcontroller firmware. Arm’s CMSIS-DSP library for its Cortex-M and Cortex-A processors includes PID functions. At home, a thermostat built on an ESP32 board with ESPHome can run a PID loop; simpler thermostats switch the heating fully on and off around the set point instead.

In IoT systems the loop stays local, because a round trip to the cloud is too slow and unreliable to steer a motor or a burner. The connected part watches instead: it records set points and outputs, changes targets and flags loops that swing. The industrial IoT guide shows how such plant data is collected.

What are the limits of PID control?

  • Windup. When the actuator is already at its limit, such as a heater at full power, the integral term keeps growing, then takes time to unwind and the process overshoots. Anti-windup logic stops the build-up.
  • Noise. The derivative term amplifies random flicker in the sensor signal, so it needs filtering or is left out.
  • Delay. When the effect shows up long after the action, as with a sensor far down a pipe, PID must be tuned slowly or it starts to swing.
  • Changing or linked processes. Gains that suit one operating point may not suit another, and coupled quantities such as temperature and humidity in HVAC fight each other when each has its own loop. Gain scheduling, which switches gains with conditions, and model-based controllers handle these cases.

PID also cannot fix a bad measurement. Even so, it remains the first thing engineers try, because it needs no model of the process and three numbers are enough to start.

Part of the IoT Glossary. Spotted an error or a missing term? Tell us.