Worked Example: A Traffic Light Controller
This article applies the eight-step FSM design methodology to a traffic light controller with pedestrian crossing capability.
Functional Specification
An intersection manages North-South (NS) and East-West (EW) traffic flows.
- Input
timer: Pulses high for one clock cycle when the active lighting interval expires. - Input
ped_req: Synchronous flag asserted when a pedestrian presses the crossing button. - Outputs: Six lamp controls (
NS_green,NS_yellow,NS_red,EW_green,EW_yellow,EW_red).
The system cycles through five operating states:
| State | NS Signal | EW Signal | Exit Condition |
|---|---|---|---|
| NS_GO | Green | Red | timer asserted |
| NS_WARN | Yellow | Red | timer asserted |
| EW_GO | Red | Green | timer asserted |
| EW_WARN | Red | Yellow | timer asserted |
| PED_CROSS | Red | Red | timer asserted |
Under standard operation, the controller cycles through NS_GO, NS_WARN, EW_GO, and EW_WARN.
If ped_req is asserted when NS_WARN expires, the controller inserts the PED_CROSS state before EW_GO. From PED_CROSS, the controller transitions directly to EW_GO.
State Diagram
The circuit uses a Moore state machine because lamp outputs depend exclusively on the active state (Figure 1).
When timer = 0, every state maintains its current value.
State Transition Table
The table lists state transitions when timer = 1:
| Present State | timer ($T$) | ped_req ($P$) | Next State |
|---|---|---|---|
| NS_GO | 1 | X | NS_WARN |
| NS_WARN | 1 | 0 | EW_GO |
| NS_WARN | 1 | 1 | PED_CROSS |
| EW_GO | 1 | X | EW_WARN |
| EW_WARN | 1 | X | NS_GO |
| PED_CROSS | 1 | X | EW_GO |
The input ped_req is evaluated only during state NS_WARN.
State Encoding
Five states require three register flip-flops ($Q_2, Q_1, Q_0$):
| State Identifier | State Code ($Q_2 Q_1 Q_0$) |
|---|---|
| NS_GO | 000 |
| NS_WARN | 001 |
| EW_GO | 010 |
| EW_WARN | 011 |
| PED_CROSS | 100 |
Binary codes 101, 110, and 111 are unused don’t-care conditions ($X$).
Next-State Logic Synthesis
Let $T$ represent timer and $P$ represent ped_req. Each next-state equation contains a hold term (when $T = 0$) and a transition term (when $T = 1$).
Bit $D_2$
Bit $D_2$ asserts to enter PED_CROSS (100) from NS_WARN when $T = 1$ and $P = 1$, or to hold PED_CROSS when $T = 0$:
$$D_2 = \overline{Q_2},\overline{Q_1}, Q_0, T, P + Q_2, \overline{T}$$
Bit $D_1$
Bit $D_1$ asserts when transitioning into EW_GO (010) or EW_WARN (011), or when holding in those states:
$$D_1 = T \left( \overline{Q_2},\overline{Q_1}, Q_0, \overline{P} + \overline{Q_2}, Q_1, \overline{Q_0} + Q_2 \right) + \overline{T}, Q_1$$
The term $Q_2$ uses unused codes 101, 110, and 111 to cover PED_CROSS efficiently.
Bit $D_0$
Bit $D_0$ asserts when transitioning into NS_WARN (001) or EW_WARN (011), or when holding in those states:
$$D_0 = \overline{Q_2},\overline{Q_0}, T + \overline{Q_2}, Q_0, \overline{T} = \overline{Q_2},(Q_0 \oplus T)$$
Outside of PED_CROSS, bit $Q_0$ toggles on each active timer pulse.
Output Logic Synthesis
Because the system is a Moore machine, output lamps depend only on state register bits:
| State | $Q_2 Q_1 Q_0$ | NS_G | NS_Y | NS_R | EW_G | EW_Y | EW_R |
|---|---|---|---|---|---|---|---|
| NS_GO | 000 | 1 | 0 | 0 | 0 | 0 | 1 |
| NS_WARN | 001 | 0 | 1 | 0 | 0 | 0 | 1 |
| EW_GO | 010 | 0 | 0 | 1 | 1 | 0 | 0 |
| EW_WARN | 011 | 0 | 0 | 1 | 0 | 1 | 0 |
| PED_CROSS | 100 | 0 | 0 | 1 | 0 | 0 | 1 |
Derive simplified Boolean equations for each lamp output:
$$\text{NS_green} = \overline{Q_2},\overline{Q_1},\overline{Q_0}$$
$$\text{NS_yellow} = \overline{Q_2},\overline{Q_1}, Q_0$$
$$\text{NS_red} = Q_2 + Q_1$$
$$\text{EW_green} = \overline{Q_2}, Q_1, \overline{Q_0}$$
$$\text{EW_yellow} = \overline{Q_2}, Q_1, Q_0$$
$$\text{EW_red} = \overline{Q_1}$$
Timing Simulation
Figure 2 traces an operational cycle containing a pedestrian crossing request asserted during state NS_GO.
When timer pulses high during NS_WARN, the machine evaluates ped_req = 1 and transitions to PED_CROSS. Both red signals illuminate. On the subsequent timer pulse, the controller resumes normal vehicular sequencing at EW_GO.
Practice Exercises
Problem 1
Modify the specification so that a pedestrian request during EW_GO also triggers a pedestrian crossing phase before NS_GO. How many states are required?
Solution
Add state PED_CROSS_2.
State EW_WARN transitions to PED_CROSS_2 when $T = 1$ and $P = 1$. State PED_CROSS_2 transitions to NS_GO when $T = 1$.
Although PED_CROSS_1 and PED_CROSS_2 assert identical outputs, their next-state successors differ (EW_GO versus NS_GO). Therefore, they cannot be merged.
The controller requires six states, which still fit within three flip-flops ($2^3 = 8$).
Problem 2
The circuit schematic contains no hardware reset. If the state register powers up in binary code 111, how does the controller behave?
Solution
Code 111 is an unused state. Evaluating the output equations for 111:
- $\text{NS_red} = Q_2 + Q_1 = 1 + 1 = 1$
- $\text{EW_red} = \overline{Q_1} = 0$
- $\text{EW_green} = \overline{Q_2}, Q_1, \overline{Q_0} = 0$
- $\text{EW_yellow} = \overline{Q_2}, Q_1, Q_0 = 0$
In state 111, all East-West lamps remain dark while North-South is red.
Digital designs must implement a power-on reset that initializes state flip-flops to NS_GO (000). In HDL code, the default branch of a case statement provides safe recovery into a known state.
Problem 3
Verify the simplified output equation $\text{EW_red} = \overline{Q_1}$ across all five valid states and evaluate its behavior in unused codes.
Solution
Evaluate $\overline{Q_1}$ for valid states:
- States
000,001,100have $Q_1 = 0 \implies \text{EW_red} = 1$ (correct). - States
010,011have $Q_1 = 1 \implies \text{EW_red} = 0$ (correct).
Evaluate unused states:
- State
101: $Q_1 = 0 \implies \text{EW_red} = 1$ (safe condition). - States
110,111: $Q_1 = 1 \implies \text{EW_red} = 0$ (unlit lamp).
Defining explicit default transitions ensures that invalid states return to safe states.
The next article presents a serial pattern detector that handles overlapping bit sequences.