Embedded Systems
What is hysteresis, and how does it stop flicker?
Hysteresis means switching at two different thresholds: on below one line, off only above a higher one, and in between, keep doing whatever you were doing. A reading that wobbles near a single line crosses it again and again, and the output flickers. With a gap wider than the wobble, the noise can't cross back, so the output switches once.
Why one line chatters
A night light reads a light sensor and switches at 512: on below, off above. At dusk the reading sits near 512 and wobbles by a few counts, from noise or a tree swaying in front of the sensor. Each wobble crosses the line, and the light flickers on and off for as long as dusk lasts.
The same thing happens to a thermostat right at its set point, or a fan switched by temperature: a relay that clicks dozens of times a minute, wearing itself out.
Live experiment
Walk through dusk and back
Walk the light from day through a twilight that wobbles, to night and back. One line at 512 flickers with every wobble; 480 and 540 stop the flicker but come on too early at dusk; 300 and 600 stay calm and switch only at night and by day.
“Don't flicker” is lesson 2 of 6 in Signal Shore, Embedded Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.
Two lines and a memory
Give the decision a gap. Turn on below 300, turn off above 600, and between them, change nothing:
bool on = false;
void loop() {
int reading = analogRead(A0);
if (reading < 300) on = true; // dark enough: on
else if (reading > 600) on = false; // bright enough: off
// between 300 and 600: keep whatever it was
digitalWrite(9, on ? HIGH : LOW);
}
The bool is the memory. At a reading of 450, the light could be on or off: it depends on which line it crossed last. That dependence on history is what the word hysteresis describes.
The gap is 600 − 300 = 300 counts. A wobble of a few counts can't carry the reading from one line to the other, so once the light has switched, it stays switched.
Where you'll meet it
Home thermostats switch the heating on a little below the set point and off a little above it. Fridges, water pumps with float switches and battery chargers all do the same. In circuits, a Schmitt trigger builds it in with positive feedback, so a slow or noisy edge comes out clean. In code, it's one bool and two ifs, and it works for robots too: start turning when an obstacle is near, and stop only once it's well clear.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Don't flicker” is lesson 2 of 6 in Signal Shore, Embedded Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Keeps doing what it was doing. 450 is between the two lines, so neither rule fires. The light stays as it was, on if it was on, off if it was off. That's the point of the gap.
- Why not “Turns on”? Turning on needs the reading to drop below 300; 450 isn't there yet.
- Why not “Turns off”? Turning off needs the reading to rise above 600; 450 isn't there either.
- Why not “Flickers”? Flicker is what the gap prevents: between the lines neither rule fires, so the light holds steady.
Show the answer
Answer: Stays off: it hasn't dropped below 300 yet. Between the lines, the light keeps whatever it was doing. It only turns on below 300.
- Why not “Turns on”? On needs a reading below 300, and 400 is still above that line.
- Why not “Flickers”? Between the lines nothing changes, so the light can't flip back and forth on a falling reading.
- Why not “Turns off”? It was already off at 700, above 600, and nothing between the lines changes that.
Show the answer
Answer: Stays on. 22 is between the lines, so neither rule fires and heating keeps its value, true. It stays on until the room passes 24.
- Why not “Flickers”? Flickering is what one threshold does. With a 4 °C gap, 22 °C sits calmly between the lines.
- Why not “Turns off”? Turning off needs the room above 24 °C. At 22 °C the off rule hasn't fired.
- Why not “Turns off then on again”? Neither rule fires at 22 °C, so nothing switches at all, on or off.
“Don't flicker” is lesson 2 of 6 in Signal Shore, Embedded Systems World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.
Questions people ask
What is hysteresis in simple terms?
Using two thresholds instead of one: switch on below one level, switch off only above a higher one, and in between keep doing whatever you were doing. The output then depends on the past as well as the present reading.
How wide should the hysteresis gap be?
Wider than the noise on the reading, so a wobble can't cross from one line to the other, and narrow enough that the switching still happens when you need it. Measure how much the reading wobbles, then leave a good margin.
What is a Schmitt trigger?
Hysteresis built into a circuit. A comparator or logic input with positive feedback switches high above one voltage and low only below a lower one, so a slow or noisy signal gives one clean edge instead of a burst.
Related topics
- Comparator hysteresis and the Schmitt triggerTwo thresholds instead of one: how a feedback resistor stops a comparator chattering on a noisy signal.
- How to use a photoresistor (LDR) with ArduinoLight in, number out: an LDR, a divider and analogRead.
- Bang-bang (on-off) control vs PIDFully on or fully off: the thermostat's controller, its band, and its limits.
- How to debounce a button on ArduinoWhy one press counts as four, and the 50 ms fixes that make it count once.