TollboothState MachineProject
On this page

Activity 4.1.3 — Tollbooth Design Challenge


Learning Objectives

By the end of this lesson, students will be able to:

  1. Apply state machine design methodology to a real-world problem
  2. Design a tollbooth system with defined states and transitions
  3. Create state diagrams and state tables for the tollbooth
  4. Implement the state machine using flip-flops and combinational logic
  5. Consider alternative implementations using microcontrollers

Vocabulary

Vocabulary (click to expand)
TermDefinition
TollboothA barrier system that collects fees from vehicles
Vehicle SensorDetects when a vehicle is present
Coin SensorDetects when coins are inserted
Gate ControllerControls the barrier arm
State TransitionMoving from one state to another based on inputs

Part 1: Problem Overview

Design Challenge: Build a Tollbooth State Machine

A tollbooth is a gate that raises when a vehicle pays a fee. Your task is to design the electronic control system for this tollbooth.

Requirements Analysis

Inputs:

  • Vehicle Sensor (V): HIGH when vehicle is present at booth
  • Coin Sensor (C): HIGH when correct amount of coins is inserted
  • Gate Position Sensors: Upper limit (U) and Lower limit (L)

Outputs:

  • Gate Motor Up: Raise the barrier
  • Gate Motor Down: Lower the barrier
  • Display: Shows amount due or “PAID”

System Behavior:

  1. Wait for vehicle
  2. When vehicle arrives, start counting coins
  3. When toll paid, raise gate
  4. When vehicle passes (sensor clears), lower gate
  5. Return to waiting state

Part 2: State Diagram Design

Define States

Let’s define the possible states of our tollbooth:

StateDescription
S0: WAITINGNo vehicle present, gate closed
S1: COUNTINGVehicle detected, accepting coins
S2: GATE_OPENToll paid, gate opening/open
S3: GATE_CLOSINGVehicle passed, closing gate

State Transitions

         Vehicle arrives            Coin inserted             Vehicle leaves
    ┌───────────────────▶┌───────────────────▶┌──────────────────▶┌──────────────────┐
    │                    │                    │                    │                  │
    │                    │                    │                    │                  │
    │            ┌───────▼───────┐      ┌──────▼───────┐    ┌──────▼───────┐   │
    │            │              │      │              │    │              │   │
    │            │   WAITING   │      │  COUNTING    │    │  GATE_OPEN  │   │
    │            │              │      │              │    │              │   │
    │            └──────┬───────┘      └──────┬───────┘    └──────┬───────┘   │
    │                  │                      │                    │             │
    │                  │                      │                    │             │
    │                  │ Timer/               │                    │             │
    │                  │ Reset               │                    │             │
    │                  │                      │                    │             │
    │                  │                      ▼                    │             │
    │                  │              ┌───────────────┐             │             │
    │                  │              │               │             │             │
    │                  │              │GATE_CLOSING  │◀────────────┘             │
    │                  │              │               │               │
    │                  │              └───────────────┘               │
    │                  │                    │                         │
    └──────────────────┴────────────────────┴─────────────────────────┘
                     Timeout (cancel)

Simplified State Diagram

For implementation, let’s simplify to 4 states:

                              V=1
           ┌─────────────────────────────────────────┐
           │                                         │
           ▼                                         │
    ┌───────────┐                              ┌──────▼──────┐
    │           │                              │             │
    │ WAITING   │────────── C=1 ──────────────▶│  COUNTING   │
    │ (S0)      │                              │    (S1)     │
    │           │◀────────────────────────────│             │
    └─────┬─────┘                              └──────┬──────┘
          │                                            │
          │                                            │
          │                      V=0 (vehicle leaves) │
          │                                            │
          │                                            ▼
          │                                    ┌───────────────┐
          │                                    │               │
          │                                    │  GATE_OPEN    │
          │                                    │    (S2)       │
          │                                    │               │
          │                                    └───────┬───────┘
          │                                            │
          │                                            │
          └────────────────────C=0────────────────────┘
                    (timeout/reset)

Part 3: State Table

State Encoding

Use 2 flip-flops (Q1, Q0) for 4 states:

StateQ1Q0Description
S0 (WAITING)00No vehicle
S1 (COUNTING)01Accepting coins
S2 (GATE_OPEN)10Gate raised
S3 (CLOSING)11Lowering gate

Complete State Table

Current StateQ1Q0Input VInput CNext Q1Next Q0Gate UpGate Down
S0 (WAITING)000X0000
S0 (WAITING)00100100
S0 (WAITING)00111010
S1 (COUNTING)01X00100
S1 (COUNTING)01X11010
S2 (GATE_OPEN)101X1010
S2 (GATE_OPEN)100X1101
S3 (CLOSING)11XX0001

Part 4: Flip-Flop Input Equations

D Flip-Flop Implementation

For D flip-flops, D = next state.

Deriving D1 (Q1 next state):

Q1Q0VCD1
00111
01X11
100X1
11XX0

From the table:

$$D_1 = Q_1 + Q_0 \cdot C + \overline{Q_1} \cdot Q_0 \cdot V$$

Simplified:

$$D_1 = Q_1 + Q_0 + C + (\overline{Q_1} \cdot Q_0 \cdot V)$$

Deriving D0 (Q0 next state):

Q1Q0VCD0
00101
01X01
10XX1
11XX0

Simplified:

$$D_0 = Q_1 + Q_0 + C + (\overline{Q_1} \cdot Q_0 \cdot \overline{V})$$


Part 5: Output Logic

Gate Up Output (GO)

Q1Q0GO
000
010
101
110

$$GO = Q_1 \cdot \overline{Q_0}$$

Gate Down Output (GD)

Q1Q0GD
000
010
100
111

GD = Q1 · Q0


Part 6: Circuit Implementation

Circuit Diagram

                    ┌─────────────────────┐
         V ────────▶│                     │
                    │   State Machine    │
         C ────────▶│   Logic Circuit    │
                    │                     │
                    └──────────┬──────────┘
                               │
              ┌────────────────┼────────────────┐
              │                │                │
              ▼                ▼                ▼
         ┌─────────┐     ┌─────────┐     ┌─────────┐
         │   D1    │     │   D0    │     │ Outputs │
         │  Flip   │     │  Flip   │     │  Logic  │
         │  Flop   │     │  Flop   │     │         │
         └────┬────┘     └────┬────┘     └────┬────┘
              │               │               │
              ▼               ▼               ▼
            Q1              Q0          Gate Motor

Hardware Implementation

Components needed:

  • 2 D flip-flops (74LS74 or similar)
  • Logic gates for D1, D0 equations
  • Logic gates for outputs
  • Motor driver (L293D) for gate motor
  • Vehicle sensor (IR break beam or limit switch)
  • Coin sensor (can be simulated with pushbutton)

Part 7: Alternative Implementation - Arduino

Why Use a Microcontroller?

The same tollbooth can be implemented much simpler with a microcontroller:

void loop() {
  // Read sensors
  bool vehicle = digitalRead(V_SENSOR);
  bool coin = digitalRead(COIN_SENSOR);
  
  // State machine
  switch(state) {
    case WAITING:
      display.show("INSERT COIN");
      if (vehicle) state = COUNTING;
      break;
      
    case COUNTING:
      display.show("COLLECTING");
      if (coin) {
        raiseGate();
        state = GATE_OPEN;
      }
      break;
      
    case GATE_OPEN:
      if (!vehicle) {
        lowerGate();
        state = WAITING;
      }
      break;
  }
}

Advantages of Microcontroller Implementation

AspectDiscrete LogicMicrocontroller (Arduino)
Design timeMore complexFaster development
FlexibilityFixed functionEasy to modify
ComponentsMany gates, ICsSingle chip
CostHigher for complexLower

Key insight: Both approaches are valid. Discrete logic is “pure” digital electronics learning, while microcontrollers represent real-world engineering practice.


Part 8: Practice Problem

Problem Statement

Extend the tollbooth to include a “quarter counter” that requires exactly 3 quarters before raising the gate.

Modified State Diagram

  1. Add a counter to track coins inserted (0, 1, 2, 3)
  2. Modify COUNTING state to have sub-states
  3. Gate only opens when 3 quarters detected

Hint

You will need to add more states or use additional flip-flops to track the count.

Show Solution

Solution approach:

Instead of simple COUNTING state, use additional flip-flops to count quarters.

Add a 2-bit counter for quarters (00, 01, 10, 11):

Q_CounterQuarters Inserted
000 quarters
011 quarter
102 quarters
113 quarters (ready)

Modified state machine:

  • S0 (WAITING): Vehicle arrives → Go to COUNTING with counter=00
  • S1 (COUNTING): Each coin pulse increments counter
  • When counter = 11 → Go to GATE_OPEN, reset counter
  • GATE_OPEN → Vehicle leaves → GATE_CLOSING → WAITING

Implementation:

  • Add 2 more flip-flops for quarter counter
  • Increment on each coin pulse
  • Detect when counter = 11 to trigger gate

This creates a complete tollbooth system that requires exact payment.


Summary

  • State machines solve real-world control problems
  • Define states, inputs, and outputs first
  • Draw state diagrams before implementing
  • Create state tables to derive equations
  • Use flip-flops to store current state
  • Microcontrollers offer a simpler alternative for complex systems

Key Reminders

  • Start with requirements, then define states
  • Use binary encoding (00, 01, 10, 11) for states
  • Derive input equations using truth tables and simplification
  • Test each state transition carefully
  • Consider both discrete logic and microcontroller approaches

Custom activity — adapted from PLTW Digital Electronics