Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

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.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place inputs A and B for the two contact lines, and probes RAW (wired straight to A) and OUT.
  2. Cross-couple two NAND gates: the first takes A and the second gate's output and drives OUT; the second takes B and OUT.
  3. Start resting on B: A = 1, B = 0, so OUT = 0.
  4. 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.
  5. 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

StepSwitchABRAWOUT
1resting on B1010
2in flight1110
3first touch of A0101
4bounces off1111
5bounces back0101
6bounces off1111
7settled on A0101
8thrown back, in flight1111
9first touch of B1010
10bounces off B1110
11settled on B1010

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

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates4-bit LFSR74LS138 decoderBCD subtractor

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.