Circuit Bytes · Episode 76 · Electronics · Digital
"Debouncing a button"
Inside a push button, two metal contacts bounce for a few milliseconds when they close, and a chip is fast enough to see every bounce, so one press can count as several. Debouncing fixes it: in code, wait about 20 ms and check again; in hardware, a resistor and capacitor smooth the bounces into one clean change.
Why buttons bounce
Inside a push button, a springy metal strip is pushed onto a fixed metal contact. The strip is springy, so it doesn't land cleanly: like a dropped ball, it bounces. For a short time the contacts make and break contact several times before they settle.
How long? Jack Ganssle tested many real switches and found most stop bouncing within a few milliseconds, though some take much longer. So "a few milliseconds" is typical, not a rule. (Switches can bounce when you let go, too.)
What the chip sees
If you graph the voltage on the chip's input, you don't get one clean step from low to high. You get a jittery line that jumps up and down several times, then settles high.
A microcontroller reads its inputs in microseconds or faster, so it is fast enough to see every bounce. One press looks like on, off, on, off, on. If your code counts each change to "on" as a press, one press can count as two, three, five or more presses. The number changes from press to press.
Fix 1: debouncing in code
Debouncing means making one press give one count. The most common way is in software:
- Notice the first change on the input.
- Wait a short time, often about 10 to 50 ms. Many Arduino examples, including the official "Debounce" tutorial, use about 20 ms.
- Check again. If the button is still pressed, accept it as one press.
The bounces all happen inside the waiting time, so the code never counts them.
Fix 2: debouncing in hardware
You can also smooth the signal before it reaches the chip. A resistor and a capacitor at the chip's input form an RC filter (the RC time constant was episode 31). The capacitor can't charge or discharge instantly, so the fast bounces get smoothed into one slow, clean change.
In real circuits the RC filter is usually followed by a Schmitt-trigger input or buffer, which turns that slow curve into one clean switch from low to high. Dedicated debouncer chips exist too.
Try it yourself
Click a pen or tap a button slowly and listen: you hear one click, but the contacts inside may have touched several times in a few thousandths of a second. If you have a microcontroller board, the Arduino "Debounce" example is a safe 5 V project: try changing its waiting time to 1 ms and then to 50 ms, and see whether one press ever counts twice.
Good to know
- When a mechanical switch closes, its metal contacts bounce for a short time, making and breaking contact several times before they settle (Ganssle; Texas Instruments and Analog Devices application notes).
- Ganssle's tests found most switches stop bouncing within a few milliseconds, with some lasting much longer, so "a few milliseconds" is typical rather than a rule. Switches can also bounce when they are released.
- A microcontroller reads its inputs in microseconds or faster, so without debouncing one press can register as several presses. "Five" in the video is an example; the number varies.
- Software debouncing: after the input changes, wait a set time (often about 10 to 50 ms; 20 ms is used here, as in many Arduino examples) and accept the new state only if it is still the same.
- Hardware debouncing: a resistor-capacitor (RC) filter slows the input so it rises or falls smoothly (the RC time constant, episode 31), usually followed by a Schmitt-trigger input or buffer for one clean switch. Dedicated debouncer chips and SR latches with two-way switches also exist.
- The button also needs a pull-up or pull-down resistor so it reads a clean high or low when not pressed (episode 72).
- Battery and logic voltages only (5 V or 3.3 V).
Answer to the comment question
Without debouncing, each press of a button counts as 4 presses. You press it 3 times. How many presses does the chip count? 12 presses. Each of the 3 presses counts 4 times: 3 × 4 = 12.
Test your knowledge
Quick quiz: "Debouncing a button"
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Jack Ganssle, "A Guide to Debouncing": tests of how long real switch contacts bounce, and software and hardware fixes
- Texas Instruments, "Debounce a Switch": why logic chips see every bounce, and a resistor-capacitor filter with a clean-switching buffer
- Analog Devices, "Interfacing Switches and Relays to the Real World in Real Time": contact bounce and why inputs to a microcontroller need debouncing in hardware or software
- Arduino Documentation, "Debounce on a Pushbutton": waiting a short time in code and checking the button again
Video transcript
You press a button once. Why does the chip count five presses?
Inside a button, two metal contacts snap together. But they don't land cleanly. They bounce.
For a few milliseconds, they touch, spring apart and touch again.
A chip is fast enough to see every bounce. One press looks like on, off, on, off, on.
Fixing this is called debouncing.
In code, the chip notices the first change, waits about twenty milliseconds, then checks again. Still pressed? That's one press.
Or in hardware, a resistor and capacitor smooth the bounces into one slow, clean change.
Without debouncing, each press of a button counts as four presses. You press it three times. How many presses does the chip count? Comment your answer below!
That's your byte.
More from Circuit Bytes
- EP73c"Three-way switches"
- EP74c"Surge protectors"
- EP75c"USB power: why one cable charges everything"
- EP77c"Potentiometers: the volume knob"