Debouncing a Mechanical Switch
← All articles

Debouncing a Mechanical Switch

5 min

A switch doesn't transition cleanly

A real mechanical switch's metal contacts physically bounce against each other on every press or release, producing a burst of rapid, spurious 0/1 transitions lasting a few milliseconds before the signal finally settles — far too fast to see by eye, but easily fast enough to be read as dozens of separate presses by a circuit sampling many times a second. Boolflow's own simulation is idealized and won't reproduce this bounce, but any circuit meant to run on real hardware needs to plan for it.

RawDebouncedbounce

Sampling into a shift register

The standard fix samples the switch signal into a small shift register at a modest, fixed rate — driven by a CLK_DIV-slowed clock, well below the bounce frequency — and only accepts a new value once every bit currently held in the register agrees.

All-agree as the accept condition

Feed a SHIFT4's four output bits into an AND4 and a NOR4 in parallel: the AND4 outputs 1 only once four consecutive samples were all 1, and the NOR4 outputs 1 only once four consecutive samples were all 0. Either one going high is the debounced, stable reading — anything still bouncing has a mix of 0s and 1s in the register and satisfies neither condition.

Try it yourself

Build this filter in the circuit editor driven by a slow CLOCK, then feed its stable output — rather than the raw INPUT switch — into an edge detector before a counter, exactly the combination a real button-driven design needs.

Build a debounce filter in the circuit editor →