Comparators & Mixed-Signal Building Blocks

switch debouncing

When you press a mechanical button, you feel one clean click — but electrically the metal contacts do not meet cleanly. They slam together, bounce apart, meet again, bounce again, several times over a few milliseconds before settling, exactly like a dropped ball bouncing before it rests. A fast digital input sees this as not one press but a burst of rapid on-off-on-off transitions. Debouncing is the art of turning that messy burst back into the single, clean event the human intended.

The bounce typically lasts from under a millisecond up to about 10 or 20 ms, depending on the switch. To a microcontroller polling thousands of times a second, that one press can register as five or ten presses. There are two families of cure. Hardware: an RC low-pass filter (a resistor and capacitor) smooths the bouncing edge, often followed by a Schmitt-trigger gate to snap it clean. Software: after the first edge, the program waits a fixed time (say 20 ms) and then re-reads the pin, accepting the press only if the level is still steady — it simply ignores any activity during the bounce window. Either way the trick is to demand stability for longer than the bounce can last.

Debouncing matters anywhere a human presses a key: keypads, volume buttons, menu navigation, counters. Skip it and a single press jumps two menu items or counts a coin twice. The honest points: a Schmitt trigger alone cleans electrical noise but does NOT cure mechanical bounce, because the contact truly is open and closed many times — the levels are real, not noise, so you need a deliberate time delay, not just hysteresis. And the right debounce time is a balance: too short and bounce leaks through; too long and the button feels sluggish or misses fast double-taps. Around 10 to 30 ms suits most human buttons.

A button bounces for about 5 ms. In software, the moment the pin first reads LOW the code starts a 20 ms timer and ignores the pin; when the timer expires it reads once more, and because the contact has long since settled, it registers exactly one press no matter how violently the contact bounced.

Demand steadiness longer than the bounce lasts — one press, one event.

A Schmitt trigger cleans electrical noise but not mechanical bounce: the contacts really do open and close many times, so those edges are real signals, not noise. Debouncing needs a deliberate time delay, in hardware or software.

Also called
debouncecontact debounce去彈跳消除彈跳