How to debounce a switch with an SR latch
When a mechanical switch closes, its contact bounces: for a short time, usually a few milliseconds, the signal flips between 0 and 1 before it settles. A double-throw switch and an SR latch made of two NAND gates remove the bounce: the first touch of a contact sets or resets the latch, and every bounce after it only holds it, so the output changes exactly once.
A single-pole double-throw switch moves one contact between two terminals, A and B. Each terminal's line is pulled up to 1 by a resistor and pulled to 0 while the switch touches it, so the lines are active-low, exactly what a NAND SR latch wants: A drives Sbar and B drives Rbar. When the contact first touches A, the latch sets. When it bounces off, A goes back to 1, but B is still 1 because the contact cannot travel all the way across in a bounce, so the latch sees 1 and 1 and holds. In the live circuit the inputs A and B stand for the two contact lines (0 = touching, 1 = not touching): click A on and off to bounce it, and watch RAW, the line itself, flicker while OUT stays put. The trick needs two contacts; for a push button with only one, use an RC filter followed by a Schmitt-trigger input, or debounce in software by waiting until the input has been stable for several milliseconds. Every step of the bounce sequence below is checked before the page is built.
Open this circuit in the lab →
What you need
- 2× NAND: cross-coupled into an SR latch: OUT = NAND(A, Qbar) and Qbar = NAND(B, OUT)
Step by step
- Place inputs
AandBfor the two contact lines, and probesRAW(wired straight to A) andOUT. - Cross-couple two NAND gates: the first takes A and the second gate's output and drives OUT; the second takes B and OUT.
- Start resting on B: A = 1, B = 0, so OUT = 0.
- Throw the switch: set B = 1 (in flight), then set A to 0, 1, 0, 1, 0 to bounce. OUT goes to 1 at the first 0 and stays there while RAW flickers.
- Throw it back: A = 1, then B to 0, 1, 0. OUT drops to 0 at the first touch of B and stays.
A bounce, step by step
| Step | Switch | A | B | RAW | OUT |
|---|---|---|---|---|---|
| 1 | resting on B | 1 | 0 | 1 | 0 |
| 2 | in flight | 1 | 1 | 1 | 0 |
| 3 | first touch of A | 0 | 1 | 0 | 1 |
| 4 | bounces off | 1 | 1 | 1 | 1 |
| 5 | bounces back | 0 | 1 | 0 | 1 |
| 6 | bounces off | 1 | 1 | 1 | 1 |
| 7 | settled on A | 0 | 1 | 0 | 1 |
| 8 | thrown back, in flight | 1 | 1 | 1 | 1 |
| 9 | first touch of B | 1 | 0 | 1 | 0 |
| 10 | bounces off B | 1 | 1 | 1 | 0 |
| 11 | settled on B | 1 | 0 | 1 | 0 |
RAW changes six times, OUT twice: once per throw of the switch. A and B are never both 0, because the switch cannot touch both terminals at once, so the latch never sees its invalid input.
You can also read the full theory in the lesson on the SR latch. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the switch debouncer (SR latch) in the lab →Frequently asked
What is switch bounce?
When the metal contacts of a mechanical switch or button meet, they spring apart and back several times before settling, usually within a few milliseconds. A fast digital input sees each of those as a separate press.
How does an SR latch debounce a switch?
With a double-throw switch, each terminal drives one input of the latch. The first touch of a terminal sets or resets the latch; a bounce only lifts the contact off that terminal, which leaves both inputs inactive, so the latch holds its value until the switch reaches the other terminal.
Why use NAND gates for the debounce latch?
The switch pulls a line to 0 when it touches it, so the inputs are active-low, and a NAND SR latch has active-low set and reset inputs. With NOR gates the inputs would be active-high and the switch would have to pull lines to 1 instead.
How do you debounce a push button with one contact?
Without a second contact the latch trick does not apply. Use an RC low-pass filter followed by a Schmitt-trigger input, or read the button in software and accept a new state only after it has stayed the same for several milliseconds.