One-sentence summary
Raw sensor readings contain small fluctuations; to calm this jitter we average a few readings together and add a small buffer around our threshold values.
Why does it matter?
In the previous lesson we learned that a relay lets a small signal control a larger circuit. But how much can we trust the sensor that produces that small signal?
If you place a light sensor on a still table and print its value to the screen, you will see the number never stops shivering: 512, 514, 511, 515, 513... The light is not actually changing, yet the number moves. This is called noise.
Noise looks small, but it causes trouble. When a value sits close to a threshold, a robot's decision can flip on and off. For example, if you say "turn on the LED when the light is below 512," and the value bounces between 511 and 513, the LED will flicker many times per second. This lesson is about how to calm that flicker.
Why does raw data shiver?
A sensor reading is not produced by a single cause. It is the sum of many tiny effects, and that is why it does not stay still.
Where noise comes from
Noise usually comes from:
- Electrical interference: Small electrical fluctuations in the wires and circuit shift the reading by a few units.
- Analog-to-digital conversion: When the sensor's continuous value is turned into a number, the last digit is constantly rounded.
- The environment itself: Tiny air movements, a passing shadow from your hand, a change in temperature — real but unimportant changes.
Everyday example: Bathroom scale
When you step on a scale, the display shows 61.2, then 61.4, then 61.1 for a moment, and only after a few seconds settles on one number. The scale gathers the first readings and waits for them to calm down. Your weight is not changing; the scale is simply ignoring the noise.
Everyday example: The thermometer's waiting beep
An ear thermometer does not show the measurement instantly. It "beeps" only after taking several readings and combining them. Because one instant reading could be misleading, the device collects data for a short moment.
Simple filtering: taking an average
The easiest way to reduce noise is to stop trusting a single reading. Instead, we add up the last few readings and take their average. The high and low fluctuations cancel each other out, and a smoother number is left behind.
The idea
Let's keep the last 5 readings in a list. With each new reading we drop the oldest, add the newest, and use the average of the list. This is called a moving average.
Start
Create a list with 5 slots
Repeat forever:
read the sensor
remove the oldest reading from the list
add the new reading to the list
total = sum of the 5 numbers in the list
average = total / 5
use the average value
Even if a single reading jumps to 515, as long as the other four readings are near 512, the average comes out around 512–513. This way the sudden spike is softened.
micro:bit-style example
The example below computes the average of the last 5 light readings. The logic is the same as the pseudocode:
readings = [0, 0, 0, 0, 0]
def loop():
# drop the oldest, shift the list one step left
for i in range(4):
readings[i] = readings[i + 1]
readings[4] = read_light_level() # new reading
average = sum(readings) / 5
show_on_screen(average)
How many readings should we average? The more readings you use, the smoother the result — but the sensor also reacts more slowly to real changes. Five readings is a good balance for most beginner projects.
Threshold and hysteresis
Averaging reduces jitter, but a value that hovers right at a threshold can still cause trouble. Here we need a second idea: adding a small buffer around the threshold.
The problem with a single threshold
With the rule "turn on the LED when the value is below 512," if the value swings between 511 and 513 the LED will flicker constantly. Because the boundary is too sharp, the decision becomes unstable.
The fix: a two-threshold buffer
We use two different values, one for switching on and one for switching off. This is called hysteresis. For example, turn the LED on below 500, but only turn it off once the value climbs above 540:
If the LED is off and average < 500
turn the LED on
If the LED is on and average > 540
turn the LED off
The region between 500 and 540 is a "comfort zone." Even if the value jitters inside this range, the state does not change. The decision only flips with a clear difference.
Everyday example: Home thermostat
If an air conditioner is set to 24 degrees, it does not switch on and off exactly at 24. It usually starts cooling at 25 degrees and stops at 23. This small gap prevents the device from clicking many times per second — this is exactly the idea of hysteresis.
Mini practice
Build a small "night light" with a light sensor and an LED. Your goal is for the LED to turn on when the light dims, without flickering.
- Read the sensor and print the value. In steady light, note how many units the number moves.
- Compute the moving average of the last 5 readings. Watch the jitter on the screen shrink.
- Try a single threshold:
if average < 500, turn LED on. Slowly shade the sensor with your hand and watch for flicker at the boundary. - Add hysteresis: turn on at 500, turn off at 540. See the flicker disappear.
- Raise the number of averaged readings from 5 to 10 and compare how the response slows down.
Write in your notebook: Which setting gave the most stable and fastest balance?
Common mistakes
Trusting a single reading
Deciding based on one instant value leaves the door open for noise to mislead you. Always combine several readings.
Averaging too many readings
If you push the average up to 50 readings, the result becomes very smooth but the sensor reacts far too late to real changes. Even after you cover the light, the lamp responds slowly. Balance matters.
Forgetting to update the list
If you add the new reading but never remove the oldest, the list stays full of old values and the average does not reflect reality.
Making the hysteresis gap too narrow
If you set the on and off thresholds too close together (say 511 and 513), the buffer does nothing and the flicker returns. The gap must be clearly wider than the size of the noise.
Safety note
- Do this work only with low-voltage educational sources: a battery, USB, micro:bit or Arduino. Never connect to mains electricity (a wall socket).
- Do not change the circuit while power is connected; cut the power first, make the connection, then power up again.
- Use the correct resistor with your LED; without a resistor the LED and the pin can be damaged.
- If a sensor's decision drives a moving part such as a motor, power the motor from a separate driver and proper power source, not from the board's pins, and keep fingers and hair away from moving parts.
- Always build and test a circuit together with an adult.
Lesson summary
- Raw sensor data contains noise; it shivers with small fluctuations even in a steady environment.
- Noise comes from electrical interference, analog-to-digital conversion, and the environment itself.
- A moving average smooths the jitter by combining the last few readings.
- Averaging more readings improves the result but slows the response; balance is needed.
- Hysteresis uses two different thresholds for switching on and off to prevent flicker at the boundary.
Check questions
- Why does a sensor value still move in steady light?
- How does a moving average reduce jitter?
- What is the downside of averaging too many readings?
- What is hysteresis, and why is it more stable than a single threshold?
- If a sensor's decision will drive a motor, which safety rule must you follow?
Answers
- Electrical interference, the last digit of analog-to-digital conversion, and tiny changes in the environment keep shifting the value by a few units.
- The high and low fluctuations of the last few readings cancel each other out in the average, leaving a smoother number.
- The result becomes very smooth, but the sensor reacts late to real changes, so the system becomes slow.
- Hysteresis means using two different thresholds for switching on and off. The buffer zone between them keeps the state from changing even when the value jitters at the boundary.
- The motor must be powered from a separate driver and proper power source, not from the board's pins; keep away from moving parts and do the work with an adult.
Source and verification note
For “Filtering Sensor Data”, verification focuses on whether the relationship between Why does raw data shiver? and Everyday example: Bathroom scale remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.
Next lesson
Calibration: How do we map a sensor's raw values to meaningful units in the real world?