State machine vending model
Build it step by step
0 of 8 doneDraw the states
Sketch IDLE, CREDIT, DISPENSE and CHANGE with arrows for each button.
Wire the buttons and display
Connect as in the table.
Test the coin
Each press adds 5 to the credit shown.
Buy item A
Press coin twice and then item A.
Test not enough credit
Press item B with only 10 and hear the beeps.
Test change
Add 20 and buy item A, then count the change beeps.
Test cancel
Insert coins and press cancel to get the refund.
Extend it
Add a third item or a sold-out state.
How do you build a state machine vending model?
A coin button, item buttons and a servo dispenser model a vending machine with states. You need Arduino Uno R3, Tactile push button 6 mm × 4, SG90 Micro Servo, 16x2 LCD with I2C backpack (PCF8574), Passive buzzer, Electrolytic Capacitor 100 µF, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 90 minutes.
- Level
- Intermediate
- Time
- About 90 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- SwitchesBuy
- Motors & ActuatorsBuySG90 Micro Servo × 1
- DisplaysBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- PassivesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Coin button | Uno D2 and GND | Adds 5 to the credit |
| Item A button | Uno D3 and GND | Price 10 |
| Item B button | Uno D4 and GND | Price 15 |
| Cancel button | Uno D5 and GND | Refund |
| Passive buzzer + | Uno D6 | Beeps |
| Passive buzzer - | Uno GND | Ground |
| Servo signal | Uno D9 | Dispenser arm |
| Servo red and brown | Uno 5V and GND | Power, with the 100 µF capacitor across them |
| LCD I2C SDA and SCL | Uno A4 and A5 | Display |
| LCD I2C VCC and GND | Uno 5V and GND | Power |
Code
A state machine keeps one clear current state and decides what each input does in that state, which avoids tangled if statements. The four states here are idle (no money), credit (money inserted), dispense (servo arm moving) and change (give money back). The model uses a servo arm as the dispenser and one beep for each 5 unit coin of change, so no real money or mechanism is involved. A small servo can run from the Uno 5 V pin for this light load with a 100 µF capacitor across the supply. Draw the state diagram on paper first and compare it with the code. Arduino IDE, board "Arduino Uno" (or "Arduino Nano" with processor ATmega328P).
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// States: IDLE, CREDIT, DISPENSE, CHANGE. Libraries: Servo, LiquidCrystal_I2C.
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
enum State { IDLE, CREDIT, DISPENSE, GIVE_CHANGE };
State state = IDLE;
const byte COIN = 2, ITEM_A = 3, ITEM_B = 4, CANCEL = 5, BUZ = 6;
const int PRICE_A = 10, PRICE_B = 15, COIN_VALUE = 5;
int credit = 0, price = 0;
unsigned long stateStart = 0;
Servo arm;
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool pressed(byte pin) { // returns true once per press, with debounce
static unsigned long last[8];
if (digitalRead(pin) == LOW && millis() - last[pin] > 250) { last[pin] = millis(); return true; }
return false;
}
void show(const char *a, const char *b = "") {
lcd.clear(); lcd.setCursor(0, 0); lcd.print(a); lcd.setCursor(0, 1); lcd.print(b);
}
void beeps(int n) { for (int i = 0; i < n; i++) { tone(BUZ, 1500, 80); delay(160); } }
void go(State s) { state = s; stateStart = millis(); }
void setup() {
for (byte p = 2; p <= 5; p++) pinMode(p, INPUT_PULLUP);
arm.attach(9); arm.write(0);
lcd.init(); lcd.backlight();
show("Insert coin", "A=10 B=15");
}
void loop() {
switch (state) {
case IDLE:
if (pressed(COIN)) { credit += COIN_VALUE; go(CREDIT); char b[17]; sprintf(b, "Credit %d", credit); show(b, "A=10 B=15"); }
break;
case CREDIT:
if (pressed(COIN)) { credit += COIN_VALUE; char b[17]; sprintf(b, "Credit %d", credit); show(b, "A=10 B=15"); }
if (pressed(ITEM_A) || pressed(ITEM_B)) {
price = digitalRead(ITEM_A) == LOW ? PRICE_A : PRICE_B;
if (credit >= price) { credit -= price; show("Dispensing...", ""); arm.write(90); go(DISPENSE); }
else { show("Need more coins", ""); beeps(2); }
}
if (pressed(CANCEL)) { go(GIVE_CHANGE); show("Refund", ""); }
break;
case DISPENSE:
if (millis() - stateStart > 1000) { arm.write(0); go(GIVE_CHANGE); show("Enjoy! Change:", ""); }
break;
case GIVE_CHANGE: {
int coins = credit / COIN_VALUE;
beeps(coins); // one beep for each coin of change
credit = 0;
delay(500);
go(IDLE);
show("Insert coin", "A=10 B=15");
break;
}
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Coin pressed | Credit rises by 5 |
| Item A with 10 or more | Servo moves and credit falls by 10 |
| Not enough credit | Message and two beeps |
| Cancel | One beep per coin of refund |
If it does not work
Presses repeat
Increase the debounce time.
Servo jitters
Add the capacitor or power it separately.
LCD blank
Try address 0x3F and adjust the contrast.
Wrong item bought
Check D3 and D4 wiring.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the state machine vending model?
Arduino Uno R3, Tactile push button 6 mm × 4, SG90 Micro Servo, 16x2 LCD with I2C backpack (PCF8574), Passive buzzer, Electrolytic Capacitor 100 µF, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the state machine vending model take to build?
About 90 minutes for a intermediate maker, in 8 steps.
How do I know the state machine vending model works?
Coin pressed: Credit rises by 5. Item A with 10 or more: Servo moves and credit falls by 10. Not enough credit: Message and two beeps.
Presses repeat: what should I check?
Increase the debounce time.
Servo jitters: what should I check?
Add the capacitor or power it separately.
LCD blank: what should I check?
Try address 0x3F and adjust the contrast.
Wrong item bought: what should I check?
Check D3 and D4 wiring.
