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.
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 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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
- 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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.
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 checkWhat is the main advantage of a Kalman filter over a simple moving average filter?
Return to the chapter’s knowledge checkFor 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