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

Vending machine FSM: a 15-cent Moore machine, live

A vending machine controller is a classic finite state machine exercise. This one sells an item for 15 cents, accepts one nickel (N) or one dime (D) per clock, and opens when it has collected at least 15 cents: a Moore machine with four states, 0, 5, 10 and 15 cents, on two D flip-flops.

The state is the money collected so far. From 0 cents a nickel goes to 5 and a dime to 10; from 5 a nickel goes to 10 and a dime to 15; from 10 either coin goes to 15. The 15-cent state raises OPEN, and the next clock returns to 0 for the next customer. Like most textbook versions it gives no change: two dimes still open it, and the extra 5 cents is kept. Giving change needs a second output and a 20-cent state, and a real machine also has to turn each coin sensor's pulse into exactly one clock, the job of an edge detector. For more worked state machines see the traffic light controller and the Mealy vs Moore 1011 detector. The live circuit starts with CLR on: turn it off, then pulse N or D for one clock at a time.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

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. Draw the state diagram: 0, 5, 10 and 15 cents, with an arrow for each coin and a return from 15 to 0.
  2. Encode the states as Q1 Q0 = 00, 01, 10, 11 and write the state table.
  3. Read off the next-state logic: D1 = Q1 Q0' + Q1' D + Q1' Q0 N and D0 = Q1' Q0' N + Q1' Q0 N' + Q1 Q0' (N + D).
  4. The Moore output depends on the state alone: OPEN = Q1 Q0. AND each D with NOT CLR for a synchronous clear.
  5. Clear, then feed a nickel, a nickel and a nickel: OPEN lights on the third clock and clears on the next.

State table

StateQ1 Q0No coinNickel (N)Dime (D)OPEN
0 cents000 cents5 cents10 cents0
5 cents015 cents10 cents15 cents0
10 cents1010 cents15 cents15 cents0
15 cents110 cents0 cents0 cents1

Coins that arrive while the machine is open are ignored in this simple version, and N and D are never 1 in the same clock. Before the page is built a 16-clock run checks OPEN and both state bits at every clock: three nickels, a dime and a nickel, two dimes, a nickel followed by a wait, then a dime.

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 adderMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code converter4-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 muxes

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 vending machine FSM in the lab →

Frequently asked

How do you design a vending machine FSM?

List the amounts of money the machine can hold as states, draw a transition for each coin, mark the state that dispenses, encode the states in binary and derive the flip-flop inputs and the output from the state table, as on this page.

Is a vending machine a Moore or a Mealy machine?

Either can work. This one is Moore: OPEN depends only on the state, so it changes only on clock edges. A Mealy version could open during the clock of the last coin with one state fewer, at the cost of an output that follows the coin inputs directly.

How many flip-flops does a 15-cent vending machine need?

Two, because four states (0, 5, 10 and 15 cents) fit in two bits. A version that gives change or accepts quarters needs more states and usually a third flip-flop.

How would the machine give change?

Add a 20-cent state reached by a dime from 10 cents, and a second output that returns a nickel from that state while dispensing. The state table grows by one row and one output column.