Activity 4.1.3 — Tollbooth Design Challenge¶
Learning Objectives¶
By the end of this lesson, students will be able to:
- Apply state machine design methodology to a real-world problem
- Design a tollbooth system with defined states and transitions
- Create state diagrams and state tables for the tollbooth
- Implement the state machine using flip-flops and combinational logic
- Consider alternative implementations using microcontrollers
Vocabulary¶
Vocabulary (click to expand)
| Term | Definition |
|---|---|
| Tollbooth | A barrier system that collects fees from vehicles |
| Vehicle Sensor | Detects when a vehicle is present |
| Coin Sensor | Detects when coins are inserted |
| Gate Controller | Controls the barrier arm |
| State Transition | Moving 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:
| State | Description |
|---|---|
| S0: WAITING | No vehicle present, gate closed |
| S1: COUNTING | Vehicle detected, accepting coins |
| S2: GATE_OPEN | Toll paid, gate opening/open |
| S3: GATE_CLOSING | Vehicle 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:
| State | Q1 | Q0 | Description |
|---|---|---|---|
| S0 (WAITING) | 0 | 0 | No vehicle |
| S1 (COUNTING) | 0 | 1 | Accepting coins |
| S2 (GATE_OPEN) | 1 | 0 | Gate raised |
| S3 (CLOSING) | 1 | 1 | Lowering gate |
Complete State Table¶
| Current State | Q1 | Q0 | Input V | Input C | Next Q1 | Next Q0 | Gate Up | Gate Down |
|---|---|---|---|---|---|---|---|---|
| S0 (WAITING) | 0 | 0 | 0 | X | 0 | 0 | 0 | 0 |
| S0 (WAITING) | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
| S0 (WAITING) | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 |
| S1 (COUNTING) | 0 | 1 | X | 0 | 0 | 1 | 0 | 0 |
| S1 (COUNTING) | 0 | 1 | X | 1 | 1 | 0 | 1 | 0 |
| S2 (GATE_OPEN) | 1 | 0 | 1 | X | 1 | 0 | 1 | 0 |
| S2 (GATE_OPEN) | 1 | 0 | 0 | X | 1 | 1 | 0 | 1 |
| S3 (CLOSING) | 1 | 1 | X | X | 0 | 0 | 0 | 1 |
Part 4: Flip-Flop Input Equations¶
D Flip-Flop Implementation¶
For D flip-flops, D = next state.
Deriving D1 (Q1 next state):
| Q1 | Q0 | V | C | D1 |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | X | 1 | 1 |
| 1 | 0 | 0 | X | 1 |
| 1 | 1 | X | X | 0 |
From the table: D1 = Q1 + Q0·C + Q1'·Q0·V
Simplified: D1 = Q1 + Q0 + C + (Q1'·Q0·V)
Deriving D0 (Q0 next state):
| Q1 | Q0 | V | C | D0 |
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | X | 0 | 1 |
| 1 | 0 | X | X | 1 |
| 1 | 1 | X | X | 0 |
Simplified: D0 = Q1 + Q0 + C + (Q1'·Q0·V')
Part 5: Output Logic¶
Gate Up Output (GO)¶
| Q1 | Q0 | GO |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
GO = Q1 · Q0'
Gate Down Output (GD)¶
| Q1 | Q0 | GD |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
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¶
| Aspect | Discrete Logic | Microcontroller (Arduino) |
|---|---|---|
| Design time | More complex | Faster development |
| Flexibility | Fixed function | Easy to modify |
| Components | Many gates, ICs | Single chip |
| Cost | Higher for complex | Lower |
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¶
- Add a counter to track coins inserted (0, 1, 2, 3)
- Modify COUNTING state to have sub-states
- 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_Counter | Quarters Inserted |
|---|---|
| 00 | 0 quarters |
| 01 | 1 quarter |
| 10 | 2 quarters |
| 11 | 3 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