Skip to content

Run a sampled PID loop against a simulated plant

Observe a 100 ms PID controller drive an LED PWM while a first-order plant model responds in firmware to setpoint and disturbance changes.

Gateway Gus: I want you to keep the simulated-plant boundary visible and explain every output change from setpoint, measurement, and stored controller state., your practice guide

Gateway Gus: I want you to keep the simulated-plant boundary visible and explain every output change from setpoint, measurement, and stored controller state.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Observe a 100 ms PID controller drive an LED PWM while a first-order plant model responds in firmware to setpoint and disturbance changes.

Tier 1 · Web · No account

Version tested: Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-10. Date: 2026-09-10.

Open the ESP32 editor, paste diagram.json, then paste sketch.ino.

Open Wokwi to paste in the files (new tab)

Get the files

Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.

diagram.json

1,115 bytes · Circuit connections

Download

sketch.ino

1,607 bytes · ESP32 program

Download

  1. Use the launch button above to open the ESP32 editor in Wokwi.
  2. Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
  3. Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.

Steps

Screens captured against Wokwi ESP32 Compiled and booted by the Wokwi gate (ESP32 Arduino core 3.3.11); captured in the Wokwi web editor on 2026-09-10 on 2026-09-10; the tool may have moved on — the text steps are the contract.

  1. 1 Step 1

    Do
    Paste the supplied diagram and sketch into Wokwi; inspect the potentiometer, disturbance button, and LED PWM output.
    You will see
    GPIO34 reads the setpoint, GPIO13 applies a modeled disturbance, and GPIO2 drives the LED through a 220 ohm resistor.
    Why it matters
    The wiring separates the learner input and visible command from the plant model implemented in code.
    Step 1: A setpoint potentiometer, disturbance button, and PWM LED connect to the ESP32. The ring marks the loop's physical inputs and output.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    Inspect the 100 ms schedule, anti-windup guard, and first-order plant update in the sketch.
    You will see
    SAMPLE_S is 0.1, integral growth pauses when saturation pushes farther, and measured moves by dt/tau toward output.
    Why it matters
    A sampled controller needs timing and state protection, while the simple plant equation makes the software-in-the-loop boundary explicit.
    Step 2: The sample period, saturation guard, and first-order plant update are visible. The ring marks the software-in-the-loop boundary.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    Start the simulation and open Serial Monitor at 115200 baud.
    You will see
    PID LOOP ESP32 READY appears with MODEL=first_order_in_firmware, sample_ms=100, tau_s=1.2, and output=LED_PWM.
    Why it matters
    The run record must state that the plant is simulated in code rather than measured from physical hardware.
    Step 3: The startup record identifies the in-firmware first-order plant and 100 ms loop. The ring marks the model boundary.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    Watch the first six controller samples after startup.
    You will see
    Each row prints setpoint, measured, output, saturation, and model=SIMULATED at 100 ms intervals.
    Why it matters
    A trend record exposes the feedback variables needed to explain the next control command.
    Step 4: Serial rows report setpoint, measured, output, saturation, and model=SIMULATED. The ring marks one complete sample record.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    Let the model run until measured approaches the potentiometer setpoint.
    You will see
    The measured value rises toward setpoint while output reduces from its initial saturated command.
    Why it matters
    Closed-loop behavior is visible when measured error changes the next output.
    Step 5: The simulated measurement approaches the setpoint as output leaves saturation. The ring marks the settled trend.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    Press Disturb plant once.
    You will see
    DISTURBANCE applied=-25.0 to simulated plant appears, followed by a lower measured value and a larger output.
    Why it matters
    A controlled disturbance checks whether feedback corrects a change that was not commanded by the setpoint.
    Step 6: A minus 25 disturbance lowers the simulated plant and increases controller output. The ring marks the disturbance record.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    Continue for at least twelve samples after the disturbance.
    You will see
    Measured climbs back toward setpoint and output falls as the error shrinks.
    Why it matters
    Recovery evidence is more useful than a single bright LED because it shows the loop's direction and bounded command over time.
    Step 7: A dozen post-disturbance samples show measured recovering toward setpoint. The ring marks the recovery evidence.
    Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)

Chapter checks

These questions refer to the chapter’s examples. Use the return links to review their answers.

  1. A fan must hold cabinet temperature near a target while heat load changes when processors wake and sleep. Which control shape is the best first loop choice?

    Return to the chapter’s knowledge check
  2. A refrigerated cabinet runs a fixed fan schedule, but door openings now vary by hour. The cloud service can suggest setpoints, yet network delays are sometimes several seconds. Which design record best fits the risk?

    Return to the chapter’s knowledge check

Caution

The first-order plant is simulated in firmware; the potentiometer is only a setpoint and the LED is only a PWM command indicator. This does not prove process physics, sensor quality, stability, actuator limits, fault response, or safe tuning. Use conservative limits and representative hardware evidence before controlling a real plant.

Return to Open-Loop and Closed-Loop Systems · Browse Labs