Finite state machine examples: a turnstile, live, and more
The simplest useful finite state machine is a coin-operated turnstile: it is either LOCKED or UNLOCKED. A coin unlocks a locked turnstile, a push through locks an unlocked one, and anything else leaves it as it is. This live circuit is that machine, one D flip-flop for the state and three gates for the next-state logic, and below it is a table of the larger state machines built live on this site.
Every finite state machine has the same three parts: a register that holds the current state, logic that computes the next state from the state and the inputs, and outputs. Here the state is one flip-flop, Q = 0 for LOCKED and Q = 1 for UNLOCKED, and the next state follows straight from the rules: from LOCKED, go to UNLOCKED if COIN is 1; from UNLOCKED, stay unless PUSH is 1. As one equation, D = COIN Q' + PUSH' Q. The output UNLOCKED depends only on the state, which makes this a Moore machine (compare the Mealy machine). Notice what the design says about odd inputs: a push while locked does nothing, an extra coin while unlocked is simply kept, and a coin and a push in the same clock while locked unlock it. Writing the state table first is what forces those decisions. Here CLK is a plain switch, so each clock is a click. The circuit opens with CLR = 1 (locked): set CLR to 0, put a 1 on COIN, and pulse CLK. Before the page is built, a 23-step sequence covering every transition is checked.
Open this circuit in the lab →
What you need
- 1× D flip-flop with clear: the state Q (0 = LOCKED, 1 = UNLOCKED), cleared to LOCKED by CLR
- 2× AND + 1 OR: the next state D = (COIN AND Q') OR (NOT PUSH AND Q)
- 1× NOT: NOT PUSH
Step by step
- Place inputs
COIN,PUSH,CLK,CLRand a probeUNLOCKED. - Write the state table: LOCKED + coin goes to UNLOCKED, UNLOCKED + push goes to LOCKED, every other case stays put.
- Encode LOCKED = 0 and UNLOCKED = 1 and read the next state off the table:
D = COIN Q' + PUSH' Q. - Wire D into a D flip-flop with clear, and probe its Q as the output.
- Clear it, then clock in a coin (UNLOCKED goes on) and then a push (it goes off).
Turnstile state table
| State | COIN | PUSH | Next state | UNLOCKED (output) |
|---|---|---|---|---|
| LOCKED (0) | 0 | X | LOCKED | 0 |
| LOCKED (0) | 1 | X | UNLOCKED | 0 |
| UNLOCKED (1) | X | 0 | UNLOCKED | 1 |
| UNLOCKED (1) | X | 1 | LOCKED | 1 |
More finite state machine examples, live
| Machine | States | What it shows |
|---|---|---|
| Traffic light controller | Green, yellow, red phases | A Moore machine whose outputs drive lights |
| Vending machine | Money inserted so far | Inputs that add up, and an output when enough has arrived |
| Sequence detector (Moore) | How much of 1011 has arrived | Spotting a bit pattern, overlaps included |
| Sequence detector (Mealy) | Four states for 1011 | Outputs that depend on the input too |
| Edge detector | This bit and the last one | A one-clock pulse on every change |
| Gray code counter | Eight codes | Next-state logic from K-maps |
| Mod-5 counter | 0 to 4 | Unused states and self-starting |
| Serial adder | The carry | One bit of arithmetic per clock |
X means the input does not matter. Every machine above is designed the same way: list the states, write the table of next states and outputs, choose a binary code for each state, and turn each next-state bit into logic. The finite state machine lesson walks through the method.
You can also read the full theory in the lesson on finite state machines. 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 Turnstile state machine in the lab →Frequently asked
What is a simple example of a finite state machine?
A coin-operated turnstile: two states, LOCKED and UNLOCKED, two inputs, COIN and PUSH. A coin unlocks it, a push locks it again, and every other input leaves the state alone. It fits in one flip-flop and three gates.
What are some real-world examples of finite state machines?
Traffic lights, vending machines, elevators, washing machines, turnstiles, and inside computers, the control unit of a CPU, serial communication receivers and pattern detectors. Several are built live on this page's list.
What is the difference between a Moore and a Mealy machine?
A Moore machine's outputs depend only on the current state, like the turnstile's UNLOCKED output. A Mealy machine's outputs depend on the state and the current inputs, which often saves states but lets an output change as soon as an input does.
How do you design a finite state machine?
List the states, draw the transitions for every input, write the state table, assign each state a binary code, then derive each flip-flop's next-state equation and each output from the table, for example with K-maps.