TactivergeLite

Lite / Building Machines That Respond / Week 20

Debouncing: one press, one count

You press a button once. Your program counts seven presses. Is the code wrong, or is the button lying?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

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.

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?

  1. One clean change
  2. Several rapid changes, which look like several presses
  3. Nothing at all
Guess first, then reveal

For a few milliseconds the pin flickers between 0 and 1. A fast loop catches several of those flickers and counts each one as a press. The fix is to wait until the reading stays the same for a while.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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 = raw

The 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

Intuitive

Like waiting for a wobbly table to stop rocking before you decide whether it leans left or right.

Technical

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.

Mathematical

Readings in a bounce window = window ÷ interval. Added latency ≈ N × interval. To reject bounces up to b ms, need N × interval > b.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

readings

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?

readings

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.

Reflect

Debouncing trades speed for certainty. Where else have you seen a machine wait to be sure before acting?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Pull-ups: giving a button a resting voltageRange, resolution, accuracy, precision →