0
0
Verilogprogramming~10 mins

Traffic light controller FSM in Verilog - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Traffic light controller FSM
Start: Reset
State: Green Light
Timer expires?
NoStay Green
Yes
State: Yellow Light
Timer expires?
NoStay Yellow
Yes
State: Red Light
Timer expires?
NoStay Red
Back to Green Light
The traffic light FSM cycles through Green, Yellow, and Red states, each lasting a set time before moving to the next.
Execution Sample
Verilog
module traffic_light_fsm(
  input clk, reset,
  output reg [2:0] light
);
  typedef enum {GREEN, YELLOW, RED} state_t;
  state_t state = GREEN;
  // Timer logic omitted for brevity
endmodule
This Verilog FSM cycles traffic lights through Green, Yellow, and Red states on a clock with reset.
Execution Table
StepCurrent StateTimerConditionNext StateOutput Light
1RESET0reset=1GREENGreen=1, Yellow=0, Red=0
2GREEN0timer < green_timeGREENGreen=1, Yellow=0, Red=0
3GREENgreen_timetimer == green_timeYELLOWGreen=0, Yellow=1, Red=0
4YELLOW0timer < yellow_timeYELLOWGreen=0, Yellow=1, Red=0
5YELLOWyellow_timetimer == yellow_timeREDGreen=0, Yellow=0, Red=1
6RED0timer < red_timeREDGreen=0, Yellow=0, Red=1
7REDred_timetimer == red_timeGREENGreen=1, Yellow=0, Red=0
💡 FSM cycles continuously through GREEN -> YELLOW -> RED states based on timer values.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 7
stateRESETGREENYELLOWREDGREEN
timer0incrementingreset to 0reset to 0reset to 0
light000100010001100
Key Moments - 3 Insights
Why does the FSM reset the timer when changing states?
The timer resets at each state change to count the duration of the new state separately, as shown in steps 3, 5, and 7 in the execution_table.
What happens if reset is active during operation?
When reset=1, the FSM immediately goes to GREEN state with timer=0 and light=Green on (step 1), overriding other states.
Why does the FSM cycle back to GREEN after RED?
The FSM is designed to cycle through states in order: GREEN -> YELLOW -> RED -> GREEN, as shown by the next state transitions in the execution_table rows 7 back to 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output light when the FSM is in YELLOW state at step 4?
AGreen=1, Yellow=0, Red=0
BGreen=0, Yellow=0, Red=1
CGreen=0, Yellow=1, Red=0
DAll lights off
💡 Hint
Check the Output Light column for step 4 in the execution_table.
At which step does the FSM transition from RED back to GREEN?
AStep 6
BStep 7
CStep 5
DStep 3
💡 Hint
Look at the Next State column in the execution_table for when state changes from RED to GREEN.
If the timer did not reset after changing from GREEN to YELLOW, what would happen?
AThe FSM timer would count incorrectly causing wrong state durations
BThe FSM would stay in YELLOW indefinitely
CThe FSM would skip YELLOW and go to RED immediately
DThe FSM would reset to GREEN immediately
💡 Hint
Refer to the key_moments about timer reset and the variable_tracker showing timer resets at state changes.
Concept Snapshot
Traffic Light FSM in Verilog:
- States: GREEN, YELLOW, RED
- Timer counts duration per state
- On timer expiry, move to next state
- Reset sets state to GREEN
- Output controls lights accordingly
Full Transcript
This traffic light controller FSM cycles through three states: GREEN, YELLOW, and RED. It starts at GREEN after reset. Each state lasts a set time controlled by a timer. When the timer reaches the state's duration, the FSM moves to the next state and resets the timer. The output lights correspond to the current state. The FSM loops continuously, ensuring safe traffic light operation.