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.
Open this circuit in the lab →
What you need
- 2× D flip-flop: the state Q1 Q0
- 3× AND: decode the states 0, 5 and 10 cents
- 5× AND: the product terms of D1 and D0
- 5× OR: N OR D, and the sums that form D1 and D0
- 3× AND: synchronous clear on D1 and D0, and OPEN = Q1 AND Q0
- 4× NOT: NOT Q1, NOT Q0, NOT N and NOT CLR
Step by step
- Draw the state diagram: 0, 5, 10 and 15 cents, with an arrow for each coin and a return from 15 to 0.
- Encode the states as Q1 Q0 = 00, 01, 10, 11 and write the state table.
- Read off the next-state logic:
D1 = Q1 Q0' + Q1' D + Q1' Q0 NandD0 = Q1' Q0' N + Q1' Q0 N' + Q1 Q0' (N + D). - The Moore output depends on the state alone:
OPEN = Q1 Q0. AND each D with NOT CLR for a synchronous clear. - Clear, then feed a nickel, a nickel and a nickel: OPEN lights on the third clock and clears on the next.
State table
| State | Q1 Q0 | No coin | Nickel (N) | Dime (D) | OPEN |
|---|---|---|---|---|---|
| 0 cents | 00 | 0 cents | 5 cents | 10 cents | 0 |
| 5 cents | 01 | 5 cents | 10 cents | 15 cents | 0 |
| 10 cents | 10 | 10 cents | 15 cents | 15 cents | 0 |
| 15 cents | 11 | 0 cents | 0 cents | 0 cents | 1 |
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
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.