Skip to content
You Start with one site
Amounts in Start with one site
Sensors & Actuators · Concept

Debouncing

One idea Beginner 5 min read
Found this useful?

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.

What the finger doesOne press, lasting a large part of a second.1 press
What the contact doesIt closes, opens and closes again several times before it rests.1 to 10 ms of bounce
What the chip seesIt reads the pin so fast that it sees every one of those changes.5 presses

See it

Predict first

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?

See it: what the pin sees during one press

Wait time
Real presses1
Presses counted

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:

bounce time < wait time < time between real presses

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

The question

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
  1. 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.

  2. 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.

  3. 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

Bounce is not a fault in the switch. Every mechanical contact bounces: a new push button, a relay, a limit switch. Replacing the part does not help. The program, or a small filter, must always deal with it.

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

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.