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

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.

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 COIN, PUSH, CLK, CLR and a probe UNLOCKED.
  2. Write the state table: LOCKED + coin goes to UNLOCKED, UNLOCKED + push goes to LOCKED, every other case stays put.
  3. Encode LOCKED = 0 and UNLOCKED = 1 and read the next state off the table: D = COIN Q' + PUSH' Q.
  4. Wire D into a D flip-flop with clear, and probe its Q as the output.
  5. Clear it, then clock in a coin (UNLOCKED goes on) and then a push (it goes off).

Turnstile state table

StateCOINPUSHNext stateUNLOCKED (output)
LOCKED (0)0XLOCKED0
LOCKED (0)1XUNLOCKED0
UNLOCKED (1)X0UNLOCKED1
UNLOCKED (1)X1LOCKED1

More finite state machine examples, live

MachineStatesWhat it shows
Traffic light controllerGreen, yellow, red phasesA Moore machine whose outputs drive lights
Vending machineMoney inserted so farInputs that add up, and an output when enough has arrived
Sequence detector (Moore)How much of 1011 has arrivedSpotting a bit pattern, overlaps included
Sequence detector (Mealy)Four states for 1011Outputs that depend on the input too
Edge detectorThis bit and the last oneA one-clock pulse on every change
Gray code counterEight codesNext-state logic from K-maps
Mod-5 counter0 to 4Unused states and self-starting
Serial adderThe carryOne 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

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)switch debouncer (SR latch)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 subtractor74181 ALUCMOS AOI gate (AOI21)D latch vs D flip-flop74LS47 BCD to 7-segment decoder74HC595 shift register7400 quad NAND gate chip7402 quad NOR gate chip7404 hex inverter chip7408 quad AND gate chip7432 quad OR gate chip7486 quad XOR gate chip7474 dual D flip-flop74HC165 shift registerCD4017 decade counter74LS283 4-bit adder74LS90 decade counter74HC161 4-bit counter74HC151 8-to-1 multiplexer74HC157 quad 2-to-1 multiplexer74HC139 dual 2-to-4 decoderCD4511 BCD to 7-segment decoder7492 divide-by-12 counter7493 4-bit binary counter74HC244 octal bufferRace-around condition (JK latch)74HC373 octal latch74HC374 octal D flip-flop1-to-2 demultiplexerhalf adder using 2:1 multiplexersregister file (4 x 4-bit)digital PWM generatorCD4013 dual D flip-flop74LS76 dual JK flip-flop

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.