Confirm sustained PIR motion
Distinguish a sustained digital PIR pulse from a pulse too short to pass a 500 ms confirmation rule.

Physics Phoebe: I want you to connect interface type to observable pin behavior and test the duration rule before trusting a motion alert.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolOpen 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
- In the Wokwi simulation panel, paste the prepared diagram and sketch into Wokwi ESP32. Inspect the PIR connections.
- You will see
- Input: the corrected sketch and supplied PIR diagram were loaded in the public Wokwi editor. Observed: the editor built the sketch and entered a running ESP32 simulation at 00:00.133. Observed: PIR OUT is wired to GPIO 13; its supply and ground reach VIN and GND. Observed: STATUS_LED_PIN is set to GPIO 2 in the visible sketch.
- Why it matters
- The running Stop control and boot text establish that this exact sketch compiled. The PIR is a digital input on GPIO 13, as shown by the wire and PIR_PIN declaration. The GPIO 2 output is named STATUS_LED_PIN so it does not collide with the core macro. The boot screen alone does not establish motion confirmation; later steps test that state.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In the Simulation toolbar, start Wokwi and read the warm-up message in Serial Monitor at 115200 baud.
- You will see
- Input: start the corrected sketch with the PIR idle. Observed: the simulation clock reached 01:00.314. Observed Serial Monitor: "PIR warm-up: wait 60 seconds". Observed: no "CONFIRMED MOTION" line appeared before the first deliberate pulse.
- Why it matters
- The startup line is emitted before the 60-second delay. The 01:00.314 clock confirms this run passed the delay. The absence of "CONFIRMED MOTION" at this point is the idle baseline. The next step applies a PIR pulse after warm-up to test the duration rule.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- In the Wokwi simulation panel, after warm-up, select the PIR sensor and press Simulate Motion.
- You will see
- Input: click Simulate motion after the 60-second warm-up. Observed: at 01:01.431, Serial Monitor shows "CONFIRMED MOTION". Observed: the ESP32 GPIO 2 status LED is blue. Observed: the sketch still displays MIN_TRIGGER_DURATION = 500.
- Why it matters
- The printed confirmation is a direct result of the sustained simulated PIR HIGH. The output appears after the 500 ms threshold, rather than on the first HIGH sample. The blue status LED matches digitalWrite(STATUS_LED_PIN, HIGH). The live clock and Stop control place this output in the active simulation.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the Wokwi simulation panel, leave the sensor alone after triggering it.
- You will see
- Input: leave the PIR alone after the first confirmation. Observed: at 01:07.014, the GPIO 2 blue status LED is dark. Observed Serial Monitor: one earlier "CONFIRMED MOTION" remains visible. Observed: no separate reset line is printed by this sketch.
- Why it matters
- The default simulated pulse ended before this capture. On LOW, the visible sketch clears triggerStartTime and confirmedMotion. The status LED is also written LOW, matching the dark GPIO 2 indicator. The retained serial line is history; it does not mean the output remains HIGH.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- In the Wokwi simulation panel, trigger motion again, and trigger once more while the pulse remains HIGH.
- You will see
- Input: click Simulate motion again after reset, then click during that HIGH pulse. Observed: at 01:09.231, Serial Monitor contains two "CONFIRMED MOTION" lines in total. Observed: the GPIO 2 blue status LED is lit during this pulse. Observed: the second click did not create a third confirmation line.
- Why it matters
- The first line belongs to the earlier completed pulse. The second line proves a LOW interval reset the confirmation state. While HIGH continues, confirmedMotion remains true. That flag suppresses another print until a later LOW resets it.

Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- In the Wokwi simulation panel, stop the simulation, change MIN_TRIGGER_DURATION from 500 to 6000, restart, wait through warm-up, and trigger once.
- You will see
- Input: change MIN_TRIGGER_DURATION from 500 to 6000, restart, wait through warm-up, and click Simulate motion once. Observed: at 01:06.314, the editor shows MIN_TRIGGER_DURATION = 6000. Observed Serial Monitor: only "PIR warm-up: wait 60 seconds" follows the boot text. Observed: no "CONFIRMED MOTION" line and the GPIO 2 blue status LED is dark.
- Why it matters
- The default simulated PIR pulse ended before the 6000 ms threshold elapsed. With no qualifying HIGH interval, the confirmation branch did not print. The status LED remained LOW, matching the missing confirmation. The comparison changes only the threshold; the PIR wiring and sketch logic are otherwise the same.

Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 7
- Do
- In the Wokwi simulation panel, restore MIN_TRIGGER_DURATION to 500, restart, wait through warm-up, and trigger once again.
- You will see
- Input: restore MIN_TRIGGER_DURATION to 500, restart, wait through warm-up, and click Simulate motion once. Observed: the running editor shows MIN_TRIGGER_DURATION = 500. Observed: at 01:01.797, Serial Monitor again shows "CONFIRMED MOTION". Observed: the GPIO 2 blue status LED is lit.
- Why it matters
- Restoring the 500 ms rule makes the same simulated pulse long enough to qualify. The warm-up line precedes the new confirmation in this restarted run. The blue status LED matches the confirmed branch of the visible sketch. The restored source is the committed lab sketch, so learners begin with the demonstrated baseline.

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.
An ESP32 has three I2C sensors: BMP280 (0x76), BH1750 (0x23), and SHT31 (0x44). After adding a second BMP280 for redundancy, both pressure readings return identical values. What is the most likely cause?
Return to the chapter’s knowledge checkAn SD card must transfer at 2 MB/s. A BME280 also needs a connection, and GPIO pins are limited. Which protocol combination meets the throughput while keeping the sensor connection pin-efficient?
Return to the chapter’s knowledge check