Skip to content

Digital Logic

How to debounce a switch in hardware with flip-flops

A push button's contacts bounce: as they close, they spring apart and touch again several times within a few milliseconds. Wired straight to a counter's clock, one press makes three or four rising edges. To debounce it in hardware, sample the button with a D flip-flop on a slow clock, every 10 ms or so. The bounce is over between two samples, so the flip-flop's output steps up once per press and down once per release.

One press, three clicks

Seen on a scope, one press is a burst: 1, 0, 1, 0, 1, all inside about 5 ms. Your finger made one press; the wire says three. Every mechanical switch does this, from keyboard keys to door sensors.

Look less often

Put the button on a flip-flop's D input and clock it slowly. The flip-flop only looks at the button on each clock edge. With a 10 ms clock, at most one look lands inside a 5 ms bounce. That look might see 1 or 0; either way, the next look sees the settled value. Q steps up exactly once per press.

The clock has to sit in a window:

  • Slower than the bounce, or two looks can land inside it and count it twice
  • Faster than the quickest press, about 50 ms, or a quick tap can slip between two looks

Every 10 to 20 ms fits both. The cost is delay: up to one clock period, which nobody notices.

Live experiment

Sample a bouncing button

160 ms of a button: a bouncy press, a quick tap, or a noise spike with no press at all. A flip-flop samples it every 1, 10 or 100 ms and clocks a counter. Straight from the button, every bounce counts; 1 ms counts the bounce too; 10 ms counts each press once; 100 ms misses the tap. Then try two samples in a row.

Try it yourself: free lesson

“Bouncy buttons” is lesson 4 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.

Two samples in a row

Electrical noise can make a single spike on the line, and if it happens to land on a clock edge, one sampler takes it as a press. Add a second flip-flop that copies the first on each tick, so it holds the sample from a tick earlier, and AND the two:

CLEAN = this tick's sample AND the last tick's sample

A real press is still there next tick, so CLEAN lights on its second tick. A one-tick spike is never in both flip-flops at once, so CLEAN stays dark.

Other ways to debounce

  • RC and a Schmitt trigger: about 10 kΩ and 1 µF smooth the bounce into a ramp, and a Schmitt-trigger gate turns it back into one clean edge.
  • SR latch: a changeover switch sets or resets a latch on its first touch, and the bounces only repeat what it's already done.
  • Software: a microcontroller can read the pin every 10 ms, exactly like the sampler; see Arduino button debouncing.

Keyboards, game controllers and lift buttons all debounce one of these ways. Without it, one key press would type three letters.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Bouncy buttons” is lesson 4 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3CLEAN is the AND of two samples in a row. A one-tick spike makes CLEAN…
    Show the answer

    Answer: Stay off: the two samples are never both 1. The spike is in FF1 for one tick and in FF2 for the next, never in both at once, so the AND never sees two 1s.

    • Why not “Stay on”? Once the spike has passed, both samples go back to 0, so CLEAN can't stay on.
    • Why not “Light for one tick”? The spike is in the first flip-flop for one tick and in the second for the next, never in both at once.
    • Why not “Light for two ticks”? Lighting at all needs both samples at 1 on the same tick, and a one-tick spike never manages that.
  2. Question 2 of 3Feed SENSOR itself into the AND, with FF1's Q. A one-tick spike…
    Show the answer

    Answer: Gets through: SENSOR and FF1 are both 1 right after the tick. The tick copies SENSOR's 1 into FF1 while SENSOR is still 1, so both AND inputs are 1. The second sample has to come from a second flip-flop.

    • Why not “Locks CLEAN on”? When the spike ends both inputs drop, so nothing holds CLEAN on.
    • Why not “Lights CLEAN for two ticks”? The spike lasts one tick, so the AND can only see it for about that long, not two ticks.
    • Why not “Is still ignored”? SENSOR itself isn't a stored sample. Right after the tick it still holds the spike, so the AND sees two 1s.
  3. Question 3 of 3A tick should be counted when…
    Show the answer

    Answer: The pin goes from HIGH to LOW. The edge happens once per notch.

    • Why not “Every pass”? Counting every pass counts time, not ticks: the number grows even when nothing moves.
    • Why not “The pin is HIGH”? A HIGH pin is the gap between ticks, and it lasts for many passes too.
    • Why not “The pin is LOW”? The pin stays LOW for many passes of the loop, so counting every LOW reading counts one tick many times.
Keep practising: free lesson

“Bouncy buttons” is lesson 4 of 6 in Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.

Questions people ask

How long does switch bounce last?

For small push buttons, usually a few milliseconds, often under 5 ms. Larger or worn switches can bounce for longer, which is why debouncers usually allow 10 to 20 ms.

What clock rate should a debouncing sampler use?

Slower than the bounce, so at most one sample lands inside it, and faster than the quickest press, about 50 ms, so no tap slips between samples. Sampling every 10 to 20 ms fits both, and that's roughly how fast keyboards scan their keys.

What other hardware debounce circuits are there?

A resistor and capacitor smooth the bounce into a slow ramp, and a Schmitt-trigger gate such as the 74HC14 squares it up again. A changeover switch can drive an SR latch, which sets on the first touch and ignores the bounces. Dedicated debouncer chips also exist.

Every topic