1.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.
2.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?
Guess first. Being wrong here is how the surprise works.
3.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?
Loading the interactive…
Can't use the interactive? Read a description instead
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.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A button can bounce for 30 ms after it is pressed. Your loop reads the pin once every 1 ms. How many readings fall inside that bounce window?
5.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
Like waiting for a wobbly table to stop rocking before you decide whether it leans left or right.
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.
Readings in a bounce window = window ÷ interval. Added latency ≈ N × interval. To reject bounces up to b ms, need N × interval > b.
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.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
A debouncer needs 5 identical readings taken every 2 ms. Roughly how long after the bouncing stops is the press accepted, in ms?
Practice 3
Without debouncing, a lift-call button sometimes registers three presses. Why?
Practice 4
You set a debouncer to need 20 readings every 5 ms. A quick 50 ms tap is ignored. Why?
Practice 5 · a fresh version every time
A button can bounce for 40 ms after it is pressed. Your loop reads the pin once every 2 ms. How many readings fall inside that bounce window?
7.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.
8.Reflect
Concepts: Digital input and output · Noise and filtering