Lite / Building Machines That Respond / Week 20
Debouncing: one press, one count
You press a button once. Your program counts seven presses. Is the code wrong, or is the button lying?
Why should I care?
Bounce is a real, physical effect in every mechanical switch. It causes lifts that skip floors in a game, keypads that type '111' and counters that jump. Debouncing is the classic first example of firmware cleaning up a messy physical signal.
- Explain contact bounce and why a fast loop sees one press as many.
- Design a counting debouncer by choosing a reading interval and a number of identical readings.
- Calculate how many readings fall inside a bounce window and the delay a debouncer adds.
Predict
The metal contacts in a button hit each other and spring apart a few times before settling. Your loop reads the pin every 0.5 ms. What will it see?
- One clean change
- Several rapid changes, which look like several presses
- Nothing at all
Guess first, then reveal
For a few milliseconds the pin flickers between 0 and 1. A fast loop catches several of those flickers and counts each one as a press. The fix is to wait until the reading stays the same for a while.
Discover
Start with 1 identical reading needed (no debouncing) and count the 'presses'. Raise the readings needed until you get exactly one press. Then try a 5 ms reading interval: how few readings can you get away with, and how late is the press accepted?
What the interactive shows (text version)
The debounce diagram shows a raw button signal over 40 ms: released (1) until 5 ms, chattering between 0 and 1 until 11 ms, pressed (0) until 30 ms, chattering again until 33 ms, then released. You choose the time between readings and how many identical readings are needed. With 1 reading needed, the chatter counts as several presses. With enough readings, exactly one press is counted, accepted a few milliseconds after the bouncing ends.
Understand
Why buttons bounce
Inside a push button, a springy metal contact slams onto another. It bounces apart and back a few times, like a dropped ball, before staying shut. For a millisecond or more the pin flickers between 0 and 1. Humans never notice; a loop reading thousands of times a second sees every flicker.
A counting debouncer
Keep the accepted state. Each reading, if the pin disagrees with it, count how many readings in a row have disagreed. Only when the count reaches N do you accept the new state. In Python-style code:
if raw == last: run += 1
else: run = 1
last = raw
if raw != state and run >= N:
state = rawThe price is delay
Waiting for N readings every p ms means a press is accepted about N × p ms after the bouncing stops. 4 readings every 0.5 ms adds about 2 ms: fine. 6 readings every 5 ms adds 30 ms, which starts to feel sluggish and can miss very short presses entirely. Engineers pick values from how long their switches bounce.
Explain it another way
Intuitive
Like waiting for a wobbly table to stop rocking before you decide whether it leans left or right.
Technical
Debouncing can be done in hardware (an RC filter plus a Schmitt-trigger input) or in software (counting, timing or shift-register filters). Software debouncing should run from a regular timer tick so its timing is predictable.
Mathematical
Readings in a bounce window = window ÷ interval. Added latency ≈ N × interval. To reject bounces up to b ms, need N × interval > b.
Applied
Keyboards, lift-call buttons and rotary knobs on cookers all debounce their switches, usually in firmware, with a few to a few tens of milliseconds of filtering.
Worked example 1
A switch bounces for up to 8 ms. Your loop reads every 2 ms. How many identical readings should the debouncer require?
8 ms ÷ 2 ms = 4 readings could all fall inside the bounce, so require more than 4, say 5 (10 ms of steady signal). The press is accepted about 10 ms after the bouncing ends.
Common mix-ups
Bouncing means the button is faulty.
✓ Every mechanical switch bounces a little. Firmware or a small filter circuit must allow for it.More debouncing is always safer.
✓ Too much filtering makes buttons feel slow and can miss short presses. Match it to the switch.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write the counting debouncer in Python-style pseudocode as a function update(raw) that returns the accepted state. Trace it by hand on the readings 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 with N = 3, and count the presses it reports.
Reflect
Debouncing trades speed for certainty. Where else have you seen a machine wait to be sure before acting?
Write your answer on paper or in your own notes. Lite never stores what you write.