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.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolObserve a 100 ms PID controller drive an LED PWM while a first-order plant model responds in firmware to setpoint and disturbance changes.
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.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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.
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 checkA 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