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.

How to complete this Wokwi lab

Your task: Measure moving-average smoothing and lag

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

Open the simulator · Read the full text instructions

Use this checklist as you work. Checks last only on this page and do not submit or record completion.

  1. Details for step 1
  2. Details for step 2
  3. Details for step 3
  4. Details for step 4
  5. Details for step 5
  6. Details for step 6
  7. Details for step 7
Simulator unavailable? Use the text fallback

Follow the checklist using the recorded results below. Predict each change, compare it with the recorded result, and explain why it matters. These are the lab's recorded results, not measurements you have made. Run the simulator later to test your predictions.

  1. Recorded result for step 1

    Input: inspect diagram.json beside the running Wokwi DHT22. The sensor data pin reaches ESP32 GPIO4. A 10 kOhm resistor pulls that signal to 3.3 V. Serial Monitor reports rawT=24.0C rawRH=40.0% at startup.

    Why it matters: The data path and pull-up define this simulated reading setup. The first serial sample belongs to the visible DHT22 circuit. Power and ground are also present in the diagram. Filtering should be judged only after this input path is known.

  2. Recorded result for step 2

    Input: inspect sketch.ino beside the active DHT22 circuit. The source sets WINDOW_SIZE=5 and declares two sample buffers. It increments sampleCount only to the window size and delays 2000 ms. Serial Monitor shows n=1 through n=4 at a 24.0C, 40.0% baseline.

    Why it matters: Initialized entries alone contribute during startup. The buffer count grows before it remains at five. The two-second interval appears in the serial header. This prevents zero-filled slots from biasing the initial average.

  3. Recorded result for step 3

    Input: start the sketch and open Serial Monitor. Observed: "DHT FILTER window=5 interval=2000ms". Expected: a five-sample window at two-second intervals. The run began a 24.0C and 40.0% raw baseline.

    Why it matters: The header declares the filter configuration. The first raw sample establishes the starting input. One sample cannot prove the average has settled. Later samples are needed to judge smoothing.

  4. Recorded result for step 4

    Input: leave the DHT22 at 24 C and 40% through three sample intervals. Observed: "FILTER rawT=24.0C rawRH=40.0% avgT=24.0C avgRH=40.0% n=3". Expected: the average equals steady raw input. The run printed n=1 and n=2 before n=3.

    Why it matters: The sample count rises as the buffer fills. The three equal inputs produce an equal average. The marked n=3 line matches the real screenshot. This baseline does not yet test a changed input.

  5. Recorded result for step 5

    Input: set the Wokwi DHT22 temperature control to 30.0 C. Serial Monitor: rawT=30.0C and rawRH=40.0%. First changed sample: avgT=25.2C, avgRH=40.0%, n=5. The previous four visible samples were at rawT=24.0C.

    Why it matters: The 25.2 C average includes one 30.0 C and four 24.0 C samples. The raw value changed immediately in the real browser run. The five-sample window therefore lags the input step. The unchanged humidity average confirms a temperature-only input change.

  6. Recorded result for step 6

    Input: leave the Wokwi DHT22 temperature at 30.0 C. Serial Monitor: the next rawT=30.0C sample gives avgT=26.4C. A further rawT=30.0C sample gives avgT=27.6C with n=5. Raw humidity and average humidity remain at 40.0%.

    Why it matters: The visible sequence rises from 25.2 C to 26.4 C to 27.6 C. The buffer still includes earlier 24.0 C samples. The fixed window count stays at five after filling. The lag is a measured response to the 30.0 C step in this run.

  7. Recorded result for step 7

    Input: set the Wokwi DHT22 humidity control to 70.0%. Serial Monitor: rawT=30.0C and rawRH=70.0%. First changed humidity sample: avgT=28.8C, avgRH=46.0%, n=5. The previous visible humidity samples remained at 40.0%.

    Why it matters: The humidity average begins below the new 70.0% input. One 70.0% reading joins four 40.0% readings in the five-sample window. Temperature continues its own rise to 28.8 C. The two channels change independently in this real Wokwi run.

Review the detailed text steps and cautions below.

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

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
    In the Wokwi simulation panel, paste diagram.json and sketch.ino into a new Wokwi ESP32 project, then trace power, ground, data, and the pull-up.
    You will see
    Input: inspect diagram.json beside the running Wokwi DHT22. The sensor data pin reaches ESP32 GPIO4. A 10 kOhm resistor pulls that signal to 3.3 V. Serial Monitor reports rawT=24.0C rawRH=40.0% at startup.
    Why it matters
    The data path and pull-up define this simulated reading setup. The first serial sample belongs to the visible DHT22 circuit. Power and ground are also present in the diagram. Filtering should be judged only after this input path is known.
    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
    In the Wokwi simulation panel, inspect the circular buffer, valid-sample count, and two-second delay in sketch.ino.
    You will see
    Input: inspect sketch.ino beside the active DHT22 circuit. The source sets WINDOW_SIZE=5 and declares two sample buffers. It increments sampleCount only to the window size and delays 2000 ms. Serial Monitor shows n=1 through n=4 at a 24.0C, 40.0% baseline.
    Why it matters
    Initialized entries alone contribute during startup. The buffer count grows before it remains at five. The two-second interval appears in the serial header. This prevents zero-filled slots from biasing the initial average.
    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
    Input: start the sketch and open Serial Monitor. Observed: "DHT FILTER window=5 interval=2000ms". Expected: a five-sample window at two-second intervals. The run began a 24.0C and 40.0% raw baseline.
    Why it matters
    The header declares the filter configuration. The first raw sample establishes the starting input. One sample cannot prove the average has settled. Later samples are needed to judge smoothing.
    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
    In Wokwi Serial Monitor, leave the DHT22 at 24 C and 40% until the steady baseline prints.
    You will see
    Input: leave the DHT22 at 24 C and 40% through three sample intervals. Observed: "FILTER rawT=24.0C rawRH=40.0% avgT=24.0C avgRH=40.0% n=3". Expected: the average equals steady raw input. The run printed n=1 and n=2 before n=3.
    Why it matters
    The sample count rises as the buffer fills. The three equal inputs produce an equal average. The marked n=3 line matches the real screenshot. This baseline does not yet test a changed input.
    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
    In the Wokwi simulation panel, select the DHT22 and change its temperature to 30 degrees C; inspect the first changed sample.
    You will see
    Input: set the Wokwi DHT22 temperature control to 30.0 C. Serial Monitor: rawT=30.0C and rawRH=40.0%. First changed sample: avgT=25.2C, avgRH=40.0%, n=5. The previous four visible samples were at rawT=24.0C.
    Why it matters
    The 25.2 C average includes one 30.0 C and four 24.0 C samples. The raw value changed immediately in the real browser run. The five-sample window therefore lags the input step. The unchanged humidity average confirms a temperature-only input change.
    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
    In the Wokwi simulation panel, keep the temperature at 30 degrees C for two more sample intervals.
    You will see
    Input: leave the Wokwi DHT22 temperature at 30.0 C. Serial Monitor: the next rawT=30.0C sample gives avgT=26.4C. A further rawT=30.0C sample gives avgT=27.6C with n=5. Raw humidity and average humidity remain at 40.0%.
    Why it matters
    The visible sequence rises from 25.2 C to 26.4 C to 27.6 C. The buffer still includes earlier 24.0 C samples. The fixed window count stays at five after filling. The lag is a measured response to the 30.0 C step in this run.
    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
    In the Wokwi simulation panel, set relative humidity to 70 percent and inspect the next output.
    You will see
    Input: set the Wokwi DHT22 humidity control to 70.0%. Serial Monitor: rawT=30.0C and rawRH=70.0%. First changed humidity sample: avgT=28.8C, avgRH=46.0%, n=5. The previous visible humidity samples remained at 40.0%.
    Why it matters
    The humidity average begins below the new 70.0% input. One 70.0% reading joins four 40.0% readings in the five-sample window. Temperature continues its own rise to 28.8 C. The two channels change independently in this real Wokwi run.
    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