Skip to content
UPEEC

Switch Debouncing

Mechanical contacts bounce for a few milliseconds; debounce in hardware or software to count one press.

Easy
Comments 0

When a button closes, its contacts bounce and produce several on/off transitions within roughly 1–20 ms. A microcontroller sees many "presses".

Hardware: an RC filter (e.g. 10 kΩ + 100 nF ≈ 1 ms) followed by a Schmitt-trigger input.

Software: accept a new state only when it has been stable for a while:

if (raw != last_raw) { last_change = millis(); last_raw = raw; }
if (millis() - last_change > 20 && raw != stable) {
    stable = raw;
    if (stable == PRESSED) on_press();
}

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.