C128 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 8 · Debouncing buttons

One press, many edges, and how to count one

A button's contacts are two springy pieces of metal. When they meet, they bounce apart and together several times over a few milliseconds before they settle, and they do it again on release. A person presses once; a fast program reading the pin sees half a dozen presses. Every product with a button has to debounce it, in hardware with a capacitor or in software by refusing to believe a change until it has lasted long enough. The software way costs nothing but a little logic and a few milliseconds of delay.

  • The button is pressed three times, and its contacts bounce for 4 ms at every press and release. The program counts falling edges two ways and prints both counts at the end.
  • The raw count takes every falling edge: it prints 21. The debounced count accepts a new level only once it has held for 10 ms: it prints 3.
  • The timeline opens zoomed in on the first press, so the chatter shows. Change DEBOUNCE_MS to 0 and run again: with no wait, the debounced count is as wrong as the raw one.

Predict

A button's contacts bounce 3 times on every press and 3 times on every release. How many falling edges does a fast program see for one press and release?

Name it

Contact bounce

Mechanical contacts typically bounce for 1 to 10 ms (cheap or worn ones longer) on both press and release. A program sampling faster than that sees every bounce.

Software debouncing

  • Stable time: accept a new level only after it has stayed unchanged for a time T longer than the bounce (10 to 20 ms). That's what the play's program does.
  • Counting samples: sample every Ts and accept a level after N equal samples in a row; it reacts NTs after the contacts settle.
  • Lockout: act on the first edge immediately, then ignore the input for T. It responds fastest, but a noise spike counts as a press.

T has to be longer than the bounce and shorter than the quickest real presses a person makes (presses come at most about 10 per second).

Hardware debouncing

A resistor and capacitor smooth the contacts' chatter into a slow ramp, τ=RC, and a Schmitt-trigger input, with separate thresholds for rising and falling, turns the ramp into one clean edge. 10 kΩ and 1 µF give τ = 10 ms.

Worked example

A debouncer samples every 2 ms and needs 5 equal samples in a row. How soon after the contacts settle does it react, at most?

Show the solution
  1. Wait for the first settled sample. That takes up to one sample interval.
  2. Then 4 more. Five samples 2 ms apart take at most 5×2ms=10ms
  3. What it ignores. A press shorter than about 10 ms is ignored, which is what a debouncer should do with a 1 ms glitch.

Answer at most 10 ms after the contacts settle.

Worked example

Can a lockout of 50 ms keep up with a person pressing as fast as they can?

Show the solution
  1. How fast a person taps. Fast tapping is about 10 presses per second, 100 ms apart.
  2. How long each part lasts. Each press and release takes at least 30 to 50 ms.
  3. Compare with the lockout. A 50 ms lockout after each edge still leaves room, though it's close.
  4. A safer choice. 20 ms is a safer choice when the bounce is under 10 ms.

Answer yes, though it's close; 20 ms is safer when the bounce is under 10 ms.

Worked example

A switch bounces for up to 8 ms. Why does simply sampling it every 10 ms debounce it?

Show the solution
  1. Compare the spacing with the bounce. Two samples are 10 ms apart, longer than any bounce, so at most one sample lands in a bounce.
  2. What that sample reads. It reads either the old or the new state, both of which are legitimate, and the next sample is stable.
  3. The result. The press is seen once, at most 10 ms late.
  4. The cost. Very short presses, under 10 ms, may be missed.

Answer at most one sample can land in the bounce, and whatever it reads is a legitimate state, so each press is seen once, at most 10 ms late.

Check

A button's contacts bounce 2 times on every press and 2 times on every release. How many falling edges does a fast program see for 3 presses?

Check

A debouncer samples every 2 ms and accepts a level after 3 equal samples in a row. About how long after the contacts settle does it react?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A hardware debouncer uses 10 kΩ and 0.1 µF. What's its time constant?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

The worst bounce measured is 5 ms. With a safety margin of 1.5×, what stable time should the debouncer require?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

Someone presses a button 10 times a second. How far apart are the presses?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

Which debouncing method responds to a press with the least delay?

Check

The button on P4 (to ground, bouncing for 4 ms) is pressed 4 times in the first second. Count the presses, debounced, and print the count once, at 0.95 s.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

Make each press of the button on P4 (bouncing for 5 ms) toggle the LED on P0 exactly once.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

A debouncer samples every 2 ms and needs 3 equal samples in a row.

(a)

At most how long after the contacts settle does it react?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

What is the fastest press-and-release rate it can follow, needing a full count for each?

Check

A debouncer ignores the button for 50 ms after each accepted press.

(a)

At most how many presses a second can it accept?

(b)

Can it keep up with a person pressing about 7 times a second (1 for yes)?

Explain why

Why does a debouncer that waits for the input to stay unchanged for 10 ms ignore bounces but still catch every real press?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.