Menu
Coddy logo textTech

Defining The States

Part of the Fundamentals section of Coddy's Verilog journey — lesson 79 of 90.

challenge icon

Challenge

A traffic light controller is a finite state machine that cycles through different light states (Red, Yellow, Green) with specific timing intervals. A finite state machine (FSM) is a circuit that can be in one of a limited number of states. It changes from one state to another based on inputs or timing.

In this project, you will build a traffic light controller for a simple intersection. The traffic light has three outputs:

LightOutputMeaning
Redred = 1Stop
Yellowyellow = 1Caution
Greengreen = 1Go

The lights cycle in this order:

Green → Yellow → Red → Green → …

Timing Sequence

StateDurationNext State
Green30 secondsYellow
Yellow10 secondsRed
Red40 secondsGreen

In this lesson, you will define the states for the traffic light controller.

A traffic light has three possible states:

StateLightCode
GreenGreen light ON2'b00
YellowYellow light ON2'b01
RedRed light ON2'b10

Complete the module by adding the missing parts.

What to do:

Define state encoding: 0=Green, 1=Yellow, 2=Red

  1. Declare a 2-bit register called state
  2. Declare a 6-bit register called counter (for timing up to 40 seconds)
  3. Add output assignments:
    • When state is 0: green = 1, yellow = 0, red = 0
    • When state is 1: green = 0, yellow = 1, red = 0
    • When state is 2: green = 0, yellow = 0, red = 1

Try it yourself

module traffic_light (
  input clk,
  input reset,
  output reg red,
  output reg yellow,
  output reg green
);
  // TODO: Task 1 - Declare state register (2 bits)
  
  // TODO: Task 2 - Declare counter register (6 bits)
  

  // TODO: Task 3 - Output assignments using case (state)
  // state 0: green=1, yellow=0, red=0
  // state 1: green=0, yellow=1, red=0
  // state 2: green=0, yellow=0, red=1

endmodule

All lessons in Fundamentals