Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 57 ยท Latches and Flip-Flops

SR Latches and Feedback

See how two cross-coupled NOR or NAND gates remember a bit. Learn set, reset, hold, the forbidden state and the gated latch, and simulate feedback in Python.

Every circuit so far has been combinational: the output depends only on the inputs right now. Forget the inputs and the output is gone. A computer needs more. It needs to remember a bit after the signal that created it has disappeared. The smallest circuit that can do this is the SR latch, and it uses nothing but two gates and a loop of wire.

The key idea is feedback: the output of a gate is wired back into the input of another gate. This lesson shows how two cross-coupled NOR (or NAND) gates hold a value, what the Set and Reset inputs do, why one input combination is forbidden, and how to add an enable input to get a gated latch. You will also learn to simulate a feedback loop in Python, which needs a different mindset from evaluating a plain truth table.

Two gates in a loop

Take two NOR gates. Feed the output of each gate into one input of the other. Gate 1 produces Q and gate 2 produces Qn. The remaining inputs are called S (set) and R (reset), and the outputs are Q and Qฬ… (written Qn in code, the complement of Q).

TEXT
Gate 1:  Q  = NOR(R, Qn)     R is the reset input
Gate 2:  Qn = NOR(S, Q)      S is the set input

Each output depends on the other, so the circuit is a loop. If S and R are both 0, each NOR gate simply inverts the other's output. That is a stable arrangement in two different ways: Q=1 with Qn=0, or Q=0 with Qn=1. Both satisfy Q = NOR(0, Qn) and Qn = NOR(0, Q). Which one you get depends on the history. That is memory: the circuit stays in whichever state it was last pushed into.

The four input combinations

S=0, R=0 Hold. Keep the stored bit.
S=1, R=0 Set. Q becomes 1.
S=0, R=1 Reset. Q becomes 0.
S=1, R=1 Forbidden. Both outputs 0.

This is the NOR latch. Inputs are active high.

1 / 2

Set, reset and hold

Walk through the NOR latch step by step:

  • Set (S=1, R=0). Qn = NOR(1, Q) is forced to 0 whatever Q is. Then Q = NOR(0, 0) = 1. The latch now stores a 1.
  • Reset (S=0, R=1). Q = NOR(1, Qn) is forced to 0, so Qn = NOR(0, 0) = 1. The latch stores a 0.
  • Hold (S=0, R=0). Neither gate is forced, and the loop keeps its old state. If you now pulse S for a moment and let it go back to 0, Q stays at 1. The momentary pulse was remembered.
  • Forbidden (S=1, R=1). Both gates are forced to 0, so Q and Qn are both 0. That breaks the promise that Qn is the opposite of Q, and when both inputs return to 0 together, the final state is unpredictable. Designers avoid this input combination.

Simulating a loop

A truth table cannot describe a loop directly, because each output is defined in terms of the other. Software simulation uses a trick: start from a guess for the outputs, compute every gate from the current values, and repeat until nothing changes. The loop is stable when one more pass changes no output. The program below does exactly that for the NOR latch and then walks through a sequence of inputs.

Python A NOR latch settling after each input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each input change takes a pass or two to ripple through the two gates and then the outputs stop moving. The "hold" rows show the memory at work: with S and R both 0, the output is whatever the previous row left behind. The same pair of inputs 0 0 gives Q=1 after a set and Q=0 after a reset.

The forbidden state, in motion

What goes wrong when S=1 and R=1 are released at the same time? In a real circuit, the two gates are never exactly equal in speed, so one of them wins the race and the latch lands in a random state. In a software model, the outcome depends on how you update the gates, and that is a useful picture of the problem.

Python Releasing the forbidden state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With perfectly equal delays the model oscillates between 0 0 and 1 1 forever. Real gates would never be that perfectly matched. Whichever is slightly faster decides the winner, and the two other runs show that the result flips with that tiny difference. A real latch can also hover at an in-between voltage for a while. That effect is called metastability, and a later lesson covers it in detail.

The NAND version

You can build the same memory from two cross-coupled NAND gates. A NAND gate is forced by a 0 (instead of a 1), so the inputs change meaning: they become active low, written Sฬ… and Rฬ… (code: Sn, Rn).

  • Sn=0, Rn=1: set, Q becomes 1.
  • Sn=1, Rn=0: reset, Q becomes 0.
  • Sn=1, Rn=1: hold.
  • Sn=0, Rn=0: forbidden. Both outputs become 1.

The NAND latch is common because NAND gates are the cheapest gates in CMOS. The next program prints the complete next-state table for both versions, trying each previous state, and marks the forbidden rows.

Python Next-state tables for NOR and NAND latches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In each table the hold row shows Q(next) = Q(now). The set and reset rows give the same result for both starting states. In the forbidden rows, both outputs agree with each other (0 and 0 for NOR, 1 and 1 for NAND), which is the sign that the latch is no longer storing a clean bit.

Mechanical switches bounce, so a single push of a button produces several quick make-and-break contacts. A NAND SR latch fed by a changeover switch (one contact for set, one for reset) is the classic fix: the first contact flips the latch, and the later bounces are just more "set" commands. The state machine lesson on debouncing returns to this.

The gated SR latch

A plain SR latch reacts to its inputs at any time. Computers need control over when a value is captured. Adding two AND gates in front of the inputs and a shared enable signal E gives the gated SR latch:

S' = S ยท E and R' = R ยท E

When E=0, both internal inputs are 0 and the latch holds. When E=1, the S and R inputs reach the latch and behave as before. The enable can be a clock or a "write" strobe. A latch that stays open for the whole time E is high is called level-sensitive or transparent: this idea is the starting point of the D latch and the flip-flop.

Python A gated SR latch following an enable signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the rows with E=0: a request to set or reset is ignored, and Q keeps its value. Only the rows with E=1 change the stored bit. The gated latch still has the forbidden state (S=1, R=1 with E=1), which the D latch removes by deriving R from S.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A NOR-based SR latch has S=0 and R=0. What does it do?

    With both inputs inactive, each NOR gate just inverts the other's output, so the loop keeps whichever state it already had.
  2. 2. Which input combination is forbidden for a NOR SR latch?

    With S=R=1 both outputs are forced to 0, so Q and Qn are no longer complements, and releasing both inputs together gives an unpredictable result.
  3. 3. How are the inputs of a cross-coupled NAND latch different from a NOR latch?

    A NAND gate output is forced by a 0 input, so the set and reset inputs are active low, and 0,0 is the forbidden pair.
  4. 4. What does the enable input of a gated SR latch do?

    The two AND gates pass S and R only when E is 1. When E is 0, both internal inputs are 0 and the latch holds.
  5. 5. Why does a Python simulation of a feedback loop repeat the gate evaluation?

    Each output is defined by the other, so you start from the old values and re-evaluate until the circuit is stable.

Summary

  • Feedback turns two gates into memory: a cross-coupled NOR (or NAND) pair has two stable states that store one bit.
  • NOR latch: S=1 sets, R=1 resets, S=R=0 holds, and S=R=1 is forbidden. The NAND latch swaps the meaning and uses active-low inputs.
  • Releasing the forbidden state at the same moment makes the outcome depend on tiny delay differences, and the real circuit can become metastable.
  • A feedback circuit is simulated by re-evaluating its gates until the outputs stop changing.
  • A gated SR latch adds AND gates and an enable E so the stored value changes only while E=1.

Next: D Latches and D Flip-Flops.