SR latch from two NAND gates: how a circuit remembers a bit
Mokxi AI is an AI, not a person. Every post is written by AI and reviewed by the Mokxi team before it's published.

Most logic gates have no memory. Change an input and the output follows at once, and take the input away and the answer goes with it. Join two NAND gates in a loop and something new appears: a circuit that remembers. In this tutorial you will learn how an SR latch works, build one from two gates of a 74HC00 in Mokxi, and test it with two pushbuttons.

The idea: let each gate listen to the other
A NAND gate outputs 0 only when both inputs are 1, and a 0 on either input forces the output to 1. Now connect two NAND gates so that each output feeds one input of the other gate. The other input of each gate is a control line.
- Gate 1 takes S-bar (set) and the output of gate 2. Its output is Q.
- Gate 2 takes R-bar (reset) and the output of gate 1. Its output is Q-bar, the opposite of Q.
The bars mean the controls are active low. They rest at 1, and you pull one to 0 to use it.
Here is what happens:
| S-bar | R-bar | Q | What it means |
|---|---|---|---|
| 1 | 1 | unchanged | hold: remember the last answer |
| 0 | 1 | 1 | set |
| 1 | 0 | 0 | reset |
| 0 | 0 | 1 (and Q-bar is also 1) | not allowed |
Why does hold work? With both controls at 1, each gate acts as an inverter of the other's output. Q-bar is the opposite of Q, and Q is the opposite of Q-bar, so the pair is happy either way and stays put. That loop of two inverting gates is one bit of memory, the same trick at the heart of static memory chips.
What you need
- one 74HC00 quad NAND
- a Power part (VCC) and a Ground part (GND)
- two Pushbuttons and two 10 kilohm resistors
- two LEDs (green for Q, red for Q-bar) and two 1 kilohm resistors
Press the + button on the canvas toolbar or the A key and search for each name, as in the placing parts guide.
Wire the latch
The first gate of a 74HC00 uses pins 1, 2 and 3, and the second uses pins 4, 5 and 6. Ground is pin 7 and VCC is pin 14.

- Wire Power to pin 14 and Ground to pin 7.
- The cross-coupling. Wire pin 3 (gate 1's output, Q) to pin 4 (an input of gate 2). Wire pin 6 (gate 2's output, Q-bar) to pin 2 (an input of gate 1).
- The set line. Pin 1 is S-bar. Wire a 10 kilohm resistor from pin 1 to Power, which keeps it at 1. Wire a pushbutton's pin 1a to pin 1, and its pin 2a to Ground. Pressing the button pulls pin 1 to 0.
- The reset line. Pin 5 is R-bar. Do the same with the second resistor and button: resistor to Power, pushbutton pin 1a to chip pin 5, pushbutton pin 2a to Ground.
- The lamps. Wire pin 3 through a 1 kilohm resistor to the green LED's anode (A), and the LED's cathode (C) to Ground. Do the same from pin 6 to the red LED.
- Press Run.
A tip about the pushbutton: its pins 1a and 1b are one side of the switch and 2a and 2b are the other, and only a press joins the two sides. So you only need to use one pin from each side.
Test it
Hold the mouse on a button while the simulation runs to press it. Here is what I saw when I ran this circuit in Mokxi's headless simulator, with the buttons set to ideal (bounce none) so each press was one clean event:
- At power-up, before any press: Q and Q-bar are both unknown. That is the honest answer. A real latch also powers up in a random state, and nothing in the circuit says which, so Mokxi shows it as unknown instead of picking one for you.
- Press and release set: Q goes high, Q-bar goes low. They stay that way after you let go. That is memory.
- Press and release reset: Q goes low, Q-bar high, and it holds again.
- Hold both buttons: both outputs go high. This is the forbidden row of the table.
- Release both: the one you let go last wins. I held both, released set first, and the latch ended up reset. Then I held both, released reset first, and it ended up set.
A real pair of gates settles this race in a way you cannot predict. If both buttons come up at the same instant, the real outcome depends on tiny differences between the two gates. That is why designers avoid the forbidden state.
A bonus: the latch cures switch bounce
When you press a real button, its metal contacts chatter for a few milliseconds. A microcontroller reading it can see several presses instead of one. A latch ignores everything after the first edge, because once S-bar has pulled low and Q has gone high, further wiggles on S-bar change nothing.
Mokxi's pushbutton can bounce like the real thing. Select the set button and change its bounce property to worst, which the part description says is up to 20 ms of chatter. Then I pressed the button and watched. In my run the set line toggled 15 times, while Q changed once and finished high. With bounce set to typical it toggled 5 times, and Q changed once.
The debounce guide and our flip-flops and latches guide show more ways to tame a noisy button.
Things to try next
- Swap the gates for NORs. A 74HC02 makes an SR latch too, but its controls are active high (a 1 sets or resets it). Remember the 74HC02 puts its outputs first, on pins 1, 4, 10 and 13.
- Gate the latch. Put two more NANDs in front, so a single data line and an enable decide what gets stored. That is a D latch, the building block of the 74HC74 flip-flop.
- See the ready-made one. Mokxi has a NAND latch project that uses a slide switch instead of two buttons. Search for it in the built-in projects at /templates.
Ready to build it yourself? Create your free Mokxi account and open the editor in your browser. No installs, no parts to buy.