Digital logic · Part 21 of 36
Flip-flops, counters and clocks
Intermediate 17 min
Latches and D flip-flops, clocks and timing, registers, counters, state machines and the danger of metastability.
What you will learn
- You can explain the difference between combinational and sequential logic.
- You can describe latches, D flip-flops, registers and counters.
- You can design a simple finite-state machine and respect setup/hold times.
Before this: Boolean algebra and minimisation
Logic with memory
The gates of the last two lessons are combinational: their output depends only on the current inputs. A traffic light, a counter or a processor needs memory — the output also depends on what happened before. That's sequential logic.

The latch
Feed two NOR (or NAND) gates' outputs back into each other's inputs and you get an SR latch: a "set" pulse stores a 1, a "reset" pulse stores a 0, and with both inputs inactive the loop holds its state as long as power is on. The forbidden case (set and reset together) is avoided in the improved D latch: while its enable input is high, the output follows input D; when enable goes low, it holds the last value.
The D flip-flop
The workhorse of digital design is the edge-triggered D flip-flop: on each rising edge of the clock, it copies D to its output Q — and holds it until the next edge, no matter how D changes in between. Every bit of state in a synchronous design — registers, counters, state machines, the program counter of a CPU — is a D flip-flop.
Clocks and timing
A clock is a square wave that ticks all flip-flops at once, so the whole circuit moves forward in lock-step. Between two edges, the combinational logic must settle. That gives timing rules:
- Setup time: D must be stable a short time before the edge.
- Hold time: D must stay stable a short time after the edge.
- Maximum clock frequency: f_max = 1 / (clock-to-Q delay + longest logic delay + setup time). If a chain takes 8 ns, you can't clock it faster than about 125 MHz.
Design tools check these constraints automatically ("static timing analysis").
Registers and counters
- A register is n flip-flops sharing a clock: it stores an n-bit value.
- A shift register passes bits from one flip-flop to the next on each clock — converting serial data to parallel and back (the 74HC595 drives LED displays this way).
- A counter adds 1 on each clock. A 4-bit binary counter counts 0–15 and wraps around; its top bit toggles at 1/16 of the clock frequency — a frequency divider.

Finite-state machines
A finite-state machine (FSM) has a register holding the current state and combinational logic that computes the next state (and outputs) from state and inputs. A pedestrian crossing: states GREEN → YELLOW → RED → RED+WALK → back, moving on button presses and timer expiries. You design it as a state diagram, then encode the states in bits. Every protocol decoder, vending machine and CPU control unit is an FSM.
Asynchronous inputs and metastability
A button press or a signal from another clock domain can change at any moment — including during a flip-flop's setup/hold window. The flip-flop may then hang between 0 and 1 for an unpredictable time: metastability. You can't prevent it, only make failure extraordinarily unlikely: pass asynchronous signals through two flip-flops in series (a synchroniser) before using them. Crossing clock domains with multi-bit data needs FIFOs or handshakes.
Debouncing
Mechanical buttons bounce for a few milliseconds, producing several edges per press. Either filter in hardware (RC + Schmitt trigger) or in logic/software: sample slowly and accept a new state only after it stayed stable for, say, 10 ms.
Try it: Logic gate trainer
Toggle the inputs of a gate, watch the output, and fill in its truth table yourself.
Turn on JavaScript to use this lab. Worked example:
Cross-coupled NAND gates form the SR latch. Start with the NAND truth table here: its output is 0 only when both inputs are 1 — the property that lets two of them hold a bit.
Key points
- Sequential logic has memory: output depends on inputs and past state.
- Latches are level-sensitive; D flip-flops copy D to Q on the clock edge.
- Respect setup and hold times; f_max = 1/(t_clk→Q + t_logic + t_setup).
- Registers store words, shift registers convert serial/parallel, counters divide frequencies.
- FSMs = state register + next-state logic; synchronise asynchronous inputs with two flip-flops; debounce buttons.
Flashcards
- Question: When does an edge-triggered D flip-flop copy its input?
- Answer: On the active clock edge (usually rising).
- Question: What is setup time?
- Answer: How long D must be stable before the clock edge.
- Question: How do you reduce metastability risk for an asynchronous input?
- Answer: Pass it through a two-flip-flop synchroniser.
- Question: What frequency does the top bit of a 4-bit counter have?
- Answer: 1/16 of the clock frequency.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Clock-to-Q 1 ns, logic delay 6 ns, setup 1 ns. Maximum clock frequency (in MHz)?
Show the answer
125 MHz (± 0.5 MHz)
1 / 8 ns = 125 MHz.
Source: Harris & Harris, 3.5
Which element copies its input only at a clock edge?
- D flip-flop
- D latch
- NAND gate
- Multiplexer
Show the answer
D flip-flop
Latches are transparent while enabled; flip-flops sample on an edge.
Source: Harris & Harris, 3.2
How many states does a 5-bit binary counter cycle through?
Show the answer
32
2⁵ = 32 states (0–31).
Source: Harris & Harris, 5.4
Which statements about metastability are true?
- It can happen when D changes within the setup/hold window
- A two-flip-flop synchroniser makes failures very unlikely
- It can be eliminated completely by a faster clock
- It matters for signals crossing clock domains
Show the answer
It can happen when D changes within the setup/hold window; A two-flip-flop synchroniser makes failures very unlikely; It matters for signals crossing clock domains
It can only be made improbable, not impossible.
Source: Harris & Harris, 3.5.5
Sources
- Digital Design and Computer Architecture (RISC-V ed.), Harris & Harris, Morgan Kaufmann, 2021
- MIT OpenCourseWare 6.004 Computation Structures, MIT, 2017
- The Art of Electronics (3rd ed.), Horowitz & Hill, Cambridge University Press, 2015
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.