Chris Collins

Tools · electronics

PID loop simulator

Simulate a PID controller on a first- or second-order plant with dead time — step response, overshoot, settling time, a load disturbance, output limits and anti-windup — get gains from Ziegler–Nichols, Cohen–Coon or SIMC tuning rules, copy the controller as C code, and play with a toy glucose loop to see what sensor and actuator delays do.

Added · updated 14 October 2026

Works with

How to use it

Describe the plant as K·e^(−θs)/τ1·s + 1)(τ2·s + 1 — gain, one or two time constants and a dead time — and the controller as Kp, Ti and Td. The plant is integrated in small steps; the controller runs only every sample time Ts and holds its output in between, as on a microcontroller.

  • The derivative acts on the measurement (no kick on setpoint changes) through a first-order filter set by N; the output is clamped to its limits, and anti-windup stops the integrator growing while the output is saturated (conditional integration). Turn it off to see why it matters.

  • The plot shows the output, the controller’s output and the setpoint, with rise time, overshoot, 2 % settling time and the integral of absolute error; add a load step to see how the loop rejects disturbances.

  • Tuning rules from the plant model: Ziegler–Nichols reaction-curve PID and PI, Cohen–Coon PID, and Skogestad’s SIMC PI. A second-order plant is reduced to first order plus dead time with Skogestad’s half rule first.

  • The C code implements exactly the simulated controller.

References: Ziegler & Nichols (1942), Cohen & Coon (1953), S. Skogestad, “Simple analytic rules for model reduction and PID controller tuning”, J. Process Control 13 (2003); Åström & Hägglund, Advanced PID Control (2006).

Toy glucose loop

Toy model — not physiology. Simulation only. Educational — not a medical device. Not for diagnosis, treatment, monitoring or dosing.

Pick Toy glucose loop at the top (or open ?preset=glucose). This is the exercise at the end of the hormones chapter, built for you: glucose as a tank with a steady supply, a constant drain and a meal, and an insulin "valve" opened in proportion to the error from a set point. The sensor reports the tank 15 minutes late and the valve’s effect arrives 60 minutes after that — the shape of the problem an automated insulin delivery loop faces (a CGM reads interstitial fluid, and insulin injected under the skin acts slowly).

  • valve = max(0, basal + gain × (sensed level − set point)). It can close but never push the level up — insulin only, no glucagon. At the set point the supply equals drain + basal, so the level holds until the meal.

  • Raise the gain and watch the level overshoot below the set point and ring; set both delays to 0 and the same gain settles cleanly. Delay is the enemy of control.

  • Levels are in made-up units and the defaults are chosen to show the shape, not to resemble a person. Nothing here is a setting, a dose or a target.

Companion to chapter 10, Kidneys, gut, and hormones of Human Biology for the Medical Maker ("Hormonal loops as control systems" and the "toy glucose loop" exercise).

Limits

The PID simulator integrates with small Euler steps and models a linear plant; real plants saturate, drift and change with operating point. The toy glucose loop is a tank, a proportional valve and two pure delays — no insulin action curve, no glucagon, no meal announcement, no sensor noise, nothing a real controller or body does.

Related: ODE solver (a minimal glucose model and a pharmacokinetic model among its presets).

Runs entirely in your browser; nothing you type leaves this page.