Skip to content

Trace a timer sleep-and-wake cycle

Trace two sleep-and-wake cycles and audit the measured state ledger behind an average-current and lifetime estimate.

Physics Phoebe: I want you to audit each state duration and measured current before trusting the lifetime number., your practice guide

Physics Phoebe: I want you to audit each state duration and measured current before trusting the lifetime number.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

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-09. Date: 2026-09-09.

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,262 bytes · Circuit connections

Download

sketch.ino

2,058 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-09 on 2026-09-09; the tool may have moved on — the text steps are the contract.

  1. 1 Step 1

    Do
    In Wokwi's project editor, paste diagram.json and sketch.ino into a new Wokwi ESP32 project and trace the switched sensor and status paths.
    You will see
    ESP32 GPIO12 supplies the sensor VCC path (diagram.json line 54). Sensor signal connects to ESP32 GPIO34 (diagram.json line 60). Sensor ground connects to ESP32 ground (diagram.json line 66). Status LED anode connects to ESP32 GPIO2; its cathode returns through r1 to ground (diagram.json lines 72, 78, 84). The status LED’s resistor r1 is 220 Ω (diagram.json line 32).
    Why it matters
    GPIO12 switches the simulated sensor supply before a sample, GPIO34 reads it, and GPIO2 marks the awake phase. Those separate paths make later sleep-cycle output interpretable.
    Step 1: The simulated sensor and awake-status LED connect through GPIO12, GPIO34, and GPIO2. The ring marks the switched loads.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    In Wokwi's sketch editor, inspect the wake counter, wake-cause check, and five-second teaching interval.
    You will see
    Set the simulator sleep interval: constexpr uint64_t SLEEP_SECONDS = 5; // Simulator teaching interval; deployment example is 900 s. (sketch.ino line 4). Name sensor-enable GPIO12: constexpr int SENSOR_ENABLE_PIN = 12; (sketch.ino line 5). Name sensor-input GPIO34: constexpr int SENSOR_INPUT_PIN = 34; (sketch.ino line 6). Name status-LED GPIO2: constexpr int STATUS_LED_PIN = 2; (sketch.ino line 7). Keep a wake counter across sleep: RTC_DATA_ATTR uint32_t wakeCount = 0; (sketch.ino line 9).
    Why it matters
    The five-second constant is the simulator teaching interval. Pin constants identify the switched sensor and status paths, while RTC_DATA_ATTR preserves the wake counter across later sleep cycles.
    Step 2: The five-second teaching interval and retained wake counter are declared. The ring marks the timer and RTC state.
    Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  3. 3 Step 3

    Do
    In the Simulation toolbar, start the run and read the first Serial Monitor heading.
    You will see
    Input: start the supplied timer-sleep sketch. Observed: "ENERGY CYCLE wake=1 cause=0". Observed: the heading appears before sensor warm-up. Expected: one initial boot with wake counter 1.
    Why it matters
    The wake counter begins at one. The reported wake cause is zero. This is the first run through setup. The later sleep line must be read separately.
    Step 3: ENERGY CYCLE wake=1 cause=0 The ring marks the initial boot cycle and undefined wake cause.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    In the Serial Monitor panel, inspect the warm-up and active-compute rows.
    You will see
    Input: leave the first cycle running. Observed: "STATE sensor-warmup duration=250 ms reading=0". Observed: "STATE active-compute status=ON sensor=ON". Expected: sensor warm-up precedes active computation.
    Why it matters
    The warm-up duration is 250 ms in this model. The warm-up reading is zero. Both status and sensor are ON for active computation. The ordering is visible in the live serial trace.
    Step 4: STATE sensor-warmup duration=250 ms reading=0 STATE active-compute status=ON sensor=ON The ring marks the warm-up and active state evidence.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    In the Serial Monitor panel, read the charge ledger and life estimate.
    You will see
    Input: inspect the first cycle summary. Observed: "LEDGER charge=272.11 mA*s average=0.302 mA". Observed: "LIFE usable=1680 mAh estimate=232 days". Expected: the average and estimate follow the simulated ledger.
    Why it matters
    The model reports 272.11 mA*s per cycle. It reports 0.302 mA average. The usable capacity is 1680 mAh. The 232-day figure is an estimate from this teaching model.
    Step 5: LEDGER charge=272.11 mA*s average=0.302 mA LIFE usable=1680 mAh estimate=232 days The ring marks the measured-ledger average and lifetime estimate.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    In the Serial Monitor panel, confirm the pre-sleep line.
    You will see
    Input: observe the end of the first cycle. Observed: "SLEEP timer=5 s status=OFF sensor=OFF". Observed: the status and sensor had been ON during active-compute. Expected: both switched paths turn OFF before sleep.
    Why it matters
    The timer is set for five seconds. Both switched paths report OFF. This is the last application line before deep sleep. It shows a state transition, not measured battery life.
    Step 6: SLEEP timer=5 s status=OFF sensor=OFF The ring marks the pre-sleep peripheral state and timer.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    In the Serial Monitor panel, wait past the five-second timer and look for a second heading.
    You will see
    Input: wait until the Wokwi clock reaches 8.866 s. Observed: the last application line remains "SLEEP timer=5 s status=OFF sensor=OFF". Observed: no second "ENERGY CYCLE" line appeared. Expected: record this web-simulator limitation.
    Why it matters
    The first cycle completed. The monitor did not show another wake heading by 8.866 s. A missing second heading cannot prove that physical hardware would stay asleep. This matches the real captured web-editor limit.
    Step 7: SLEEP timer=5 s status=OFF sensor=OFF The ring marks the state before the timer-wake timeout; this web-editor run did not produce a second application cycle.
    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 battery-life calculator predicts four years, but the model uses datasheet sleep current, nominal battery capacity, and one successful radio transmission per cycle. What should the engineer do before accepting the result?

    Return to the chapter’s knowledge check
  2. Beyond a single lifetime figure, what is the most useful thing a battery-life tool provides?

    Return to the chapter’s knowledge check
  3. For a node that reports once per hour with a 0.5 s active burst, a sweep shows halving sleep current gives +59% life while halving active current gives +15%. Which optimization should the team pursue first, and why?

    Return to the chapter’s knowledge check

Caution

The five-second Wokwi timer is a teaching acceleration; the source deployment example uses 900 seconds. Wokwi shows firmware states, not trustworthy current. Measure the complete physical node in series with a suitable profiler or meter, account for USB and radio peaks, and never place an ammeter directly across a supply.

Return to Power Management Tools · Browse Labs