Skip to content

Measure moving-average smoothing and lag

Observe how a five-sample moving average suppresses variation while delaying a genuine DHT22 temperature change.

Physics Phoebe: I want you to compare every raw step with its delayed average before choosing a window., your practice guide

Physics Phoebe: I want you to compare every raw step with its delayed average before choosing a window.
Predict the reading, then compare it with the measurement.

Wokwi ESP32

Third party Tool

Observe how a five-sample moving average suppresses variation while delaying a genuine DHT22 temperature change.

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

793 bytes · Circuit connections

Download

sketch.ino

1,220 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
    Paste diagram.json and sketch.ino into a new Wokwi ESP32 project, then trace power, ground, data, and the pull-up.
    You will see
    The DHT22 data pin reaches GPIO4 and a 10 kOhm resistor pulls that signal to 3.3 V.
    Why it matters
    The wiring reproduces the source lab's electrical precondition before software filtering is evaluated.
    Step 1: The DHT22 data line connects to GPIO4 with a 10 kilohm pull-up to 3.3 volts. The ring marks the sensor and pull-up.
    Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  2. 2 Step 2

    Do
    Inspect the circular buffer, valid-sample count, and two-second delay in sketch.ino.
    You will see
    The code stores five readings, averages only initialized entries, and waits for the DHT22's required interval.
    Why it matters
    Counting only valid entries avoids startup bias from zero-filled slots, while the interval prevents oversampling.
    Step 2: The circular buffer averages only collected samples and enforces a two-second interval. The ring marks the filtering code.
    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
    The sketch announces a five-sample window and 2000 ms interval.
    Why it matters
    Recording filter configuration makes each output trace reproducible.
    Step 3: DHT FILTER window=5 interval=2000ms The ring marks the declared filter configuration.
    Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  4. 4 Step 4

    Do
    Leave the DHT22 at 24 degrees C and 40 percent relative humidity until the baseline prints.
    You will see
    Raw and average values both settle at 24.0 degrees C and 40.0 percent.
    Why it matters
    A steady baseline confirms the filter is not inventing an offset.
    Step 4: FILTER rawT=24.0C rawRH=40.0% avgT=24.0C avgRH=40.0% n=3 The ring marks the steady baseline.
    Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  5. 5 Step 5

    Do
    Select the DHT22 and change its temperature to 30 degrees C; inspect the first changed sample.
    You will see
    The raw temperature reaches 30.0 degrees C while the five-sample average remains between 24 and 30 degrees C.
    Why it matters
    The gap is the filter's response lag, not measurement error.
    Step 5: The raw temperature is 30.0C while the moving average is still lower. The ring marks the first changed sample.
    Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  6. 6 Step 6

    Do
    Keep the temperature at 30 degrees C for two more sample intervals.
    You will see
    Successive 30.0 degree C inputs pull the average upward toward the new steady value.
    Why it matters
    A window reduces random variation by combining samples, but necessarily spreads a real step across time.
    Step 6: Repeated 30.0C raw samples move the five-sample average toward 30.0C. The ring marks the rising average.
    Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
  7. 7 Step 7

    Do
    Set relative humidity to 70 percent and inspect the next output.
    You will see
    Raw humidity changes independently to 70.0 percent while its average begins below 70 percent.
    Why it matters
    Independent channels need independent buffers, and each exhibits the same smoothing-versus-lag tradeoff.
    Step 7: Raw humidity changes to 70.0% while its moving average remains lower. The ring marks the independent humidity response.
    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. You have a noisy temperature sensor that occasionally produces spike values (e.g., 22C, 23C, 55C, 22C). Which filter is better for removing these spikes?

    Return to the chapter’s knowledge check
  2. What is the main advantage of a Kalman filter over a simple moving average filter?

    Return to the chapter’s knowledge check
  3. For a moving average filter with window size N=10 sampling at 100Hz, what is the approximate latency introduced?

    Return to the chapter’s knowledge check

Caution

Wokwi provides controlled digital sensor values, not evidence of field accuracy or random-noise rejection. On hardware, use 3.3 V-compatible wiring, preserve the pull-up and two-second interval, compare against a traceable reference, and reject invalid readings before updating the buffer.

Return to Sensor Filtering and Validation · Browse Labs