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 ToolObserve how a five-sample moving average suppresses variation while delaying a genuine DHT22 temperature change.
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.
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
- 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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
- The sketch announces a five-sample window and 2000 ms interval.
- Why it matters
- Recording filter configuration makes each output trace reproducible.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) 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 · 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