Debouncing
On this page
Drop a ball on a hard floor and it does not stop at once. It bounces a few times first. The metal contacts inside a button do the same when they meet. Debouncing is how a program counts one press as one press: it waits until the contact has stopped bouncing.
See it
A program adds 1 to a counter each time the voltage on a button pin falls. You press the button once, firmly. By how much does the counter go up?
The line is the voltage on the pin. The button is pressed at 0 ms, and the contact bounces until 5.9 ms. Each fall that the program counts has a dot and a number. After a counted fall the program ignores the pin for the wait time, shown as a shaded band. A fall inside a band has an open circle and is not counted.
The idea
When the contacts of a switch meet, they bounce: for 1 to 10 ms the signal jumps between high and low before it settles. A chip reads its pin thousands of times in that period, so it sees every jump.
The usual cure is in the program: after the first change that it counts, it ignores the pin for a wait time. The wait must cover the whole bounce and end before the next real press:
for a button pressed by hand, 50 ms works well: longer than any bounce, shorter than a finger can press twice
The cure can also be in the circuit. A resistor and a capacitor at the pin smooth the jumps, and their time constant must be as long as the bounce: 10 kΩ with 1 µF gives 10 ms, while 10 kΩ with 100 nF gives only 1 ms and removes only the shortest bounces. One limit: the bounce time differs from one type of switch to another, and it grows as a switch wears.
The contacts bounce again when the button is let go, perhaps a second after the press. A program that only waits after each counted press can count that release as a second press. The safe rule: count a press only after the pin has been still for the whole wait time.
One worked number
During one press, the voltage on a pin falls at 0 ms, 0.9 ms, 2.1 ms, 3.8 ms and 5.9 ms. The program counts a fall only if it comes more than 3 ms after the last fall it counted. How many presses does it count?
Show the worked solution, 3 steps
- Count the first fall
The fall at 0 ms is counted: press 1. The program now ignores the pin until 3 ms, so the falls at 0.9 ms and 2.1 ms are not counted.
- Find the next fall after the wait
The fall at 3.8 ms comes 3.8 ms after the last counted one, which is more than 3 ms. It is counted: press 2. The program now ignores the pin until 6.8 ms.
- Check the last fall
5.9 ms is inside that wait, so it is not counted. The total is 2 presses for one real press: 3 ms is too short. Check it in the figure, then raise the wait above 5.9 ms.
The mistake to avoid
A second trap: a longer wait is not always safer. A flow meter that gives 20 pulses a second sends them 50 ms apart, so a wait of 50 ms ignores real pulses and the total reads low.
Check yourself
A water meter closes a contact once for each litre, up to 40 times a second. The contact bounces for up to 2 ms. Which wait time suits it?
A door counter adds 3 to 7 for each time the door opens. A technician fits a new switch of the same type, and the count is still wrong. Why?
A lever switch counts boxes on a conveyor. A box passes every 100 ms. To be safe, someone sets the wait time to 200 ms. What does the counter show?
Where you meet it
- Control panelsEvery start and stop button on a machine is read through a wait time.
- Pulse metersWater, gas and energy meters often give one contact closure for each unit. Bounce that is counted becomes units that nobody used.
- Limit switches and door switchesA counter of door openings on a cold room, or of strokes on a press, reads a contact that bounces.
- Mechanical contacts bounce for 1 to 10 ms. A chip is fast enough to see every bounce.
- Debouncing counts one press as one, by ignoring the pin for a wait time after the first change.
- Bounce time < wait time < time between real presses.
- Bounce is normal, not a fault. A wait that is too long loses real events.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.