Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: A Traffic Light Controller

Build a complete traffic light controller in VHDL: specification, FSM with per-state timers, pedestrian request, testbench plan and a runnable C model of the design.

This project brings together chapters 4 to 7: a finite state machine, a timer driven by a clock-enable tick, a synchronized input, a testbench plan and a pin-level top module. The design is a two-way intersection with a pedestrian button. You will specify it, draw the FSM, write the VHDL, model it in C and plan the verification.

As everywhere on this track, VHDL listings are read-only (no simulator or board is available on the site). The C model is a runnable stand-in of the exact behavior, written so each piece corresponds to a VHDL process.

Specification

  • Two roads, North-South (NS) and East-West (EW), each with red, yellow and green lights.
  • Cycle: NS green for 8 s, NS yellow for 2 s, all red for 1 s, EW green for 8 s, EW yellow for 2 s, all red for 1 s, repeat.
  • A pedestrian button request shortens a long green: if a request is waiting when a green has run for at least 3 s, the green ends early.
  • The clock is 100 MHz; one tick per second drives all timers.
  • Reset puts the controller into "all red" for 1 s and then starts with NS green.
  • Safety rule: the two roads must never be green or yellow at the same time.

State machine

flowchart LR
  AR1(["ALL_RED_1"]) -->|"timer done"| NSG(["NS_GREEN"])
  NSG -->|"timer done or early request"| NSY(["NS_YELLOW"])
  NSY -->|"timer done"| AR2(["ALL_RED_2"])
  AR2 -->|"timer done"| EWG(["EW_GREEN"])
  EWG -->|"timer done or early request"| EWY(["EW_YELLOW"])
  EWY -->|"timer done"| AR1

Six states, Moore outputs: each state fixes the colors of both roads.

State NS EW Duration
ALL_RED_1 red red 1 s
NS_GREEN green red 8 s (min 3 s if shortened)
NS_YELLOW yellow red 2 s
ALL_RED_2 red red 1 s
EW_GREEN red green 8 s (min 3 s if shortened)
EW_YELLOW red yellow 2 s

The tick generator

VHDL
-- one pulse per second from the 100 MHz clock
process (clk)
begin
  if rising_edge(clk) then
    if rst = '1' or sec_cnt = CLK_HZ - 1 then
      sec_cnt <= 0;
      tick    <= '1' when rst = '0' else '0';
    else
      sec_cnt <= sec_cnt + 1;
      tick    <= '0';
    end if;
  end if;
end process;

(The conditional assignment inside a process requires VHDL-2008, which is fine for modern tools.) In simulation use a small CLK_HZ generic (say 10) so tests finish quickly.

The FSM with a timer

VHDL
type state_t is (ALL_RED_1, NS_GREEN, NS_YELLOW, ALL_RED_2, EW_GREEN, EW_YELLOW);
signal state : state_t := ALL_RED_1;
signal elapsed : natural range 0 to 15 := 0;      -- seconds in this state
signal req : std_logic := '0';                    -- latched pedestrian request

-- helper: how long must this state last?
function duration (s : state_t) return natural is
begin
  case s is
    when NS_GREEN | EW_GREEN   => return 8;
    when NS_YELLOW | EW_YELLOW => return 2;
    when others                => return 1;
  end case;
end function;

process (clk)
  variable done : boolean;
begin
  if rising_edge(clk) then
    if btn_sync = '1' then req <= '1'; end if;      -- remember a request
    if rst = '1' then
      state <= ALL_RED_1; elapsed <= 0; req <= '0';
    elsif tick = '1' then
      elapsed <= elapsed + 1;
      done := (elapsed + 1 = duration(state));
      if (state = NS_GREEN or state = EW_GREEN)
         and req = '1' and elapsed + 1 >= 3 then
        done := true;
      end if;
      if done then
        elapsed <= 0;
        if state = NS_GREEN or state = EW_GREEN then req <= '0'; end if;
        case state is
          when ALL_RED_1 => state <= NS_GREEN;
          when NS_GREEN  => state <= NS_YELLOW;
          when NS_YELLOW => state <= ALL_RED_2;
          when ALL_RED_2 => state <= EW_GREEN;
          when EW_GREEN  => state <= EW_YELLOW;
          when EW_YELLOW => state <= ALL_RED_1;
        end case;
      end if;
    end if;
  end if;
end process;

The outputs are a separate concurrent block: a pure function of state (Moore), so they are glitch-free if you also register them:

VHDL
ns_green  <= '1' when state = NS_GREEN  else '0';
ns_yellow <= '1' when state = NS_YELLOW else '0';
ns_red    <= '1' when state = ALL_RED_1 or state = ALL_RED_2
                      or state = EW_GREEN or state = EW_YELLOW else '0';
-- ew_* similarly

Note the design choices: a single process for state and timer (a one-process FSM, see the FSM styles); the button is synchronized first (btn_sync, two flip-flops, as in the FPGA flow lesson); and a sticky req flag remembers a press that arrives while the light is not green yet.

Running the specification in C

This model follows the VHDL structure line by line, with a tick per loop step, and prints each second of one full cycle plus a pedestrian press during the EW green.

C C stand-in: traffic light controller with pedestrian request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In the trace, NS runs its 8 s, then yellow and all-red; the EW green begins at t=12. The press at t=14 is handled when EW green has run 3 s: the green ends after 3 s, at t=15, and EW_YELLOW begins; the safety check never fires.

Testbench plan

A good plan is a list of properties, each with a directed test:

  1. After reset the lights are all red, then NS green appears after 1 s.
  2. Each state lasts exactly its duration (count ticks).
  3. Safety: at every clock edge, NS and EW are never both non-red. Put this in a concurrent assert, so it holds during every test:
VHDL
assert not ((ns_green = '1' or ns_yellow = '1') and (ew_green = '1' or ew_yellow = '1'))
  report "SAFETY VIOLATION" severity failure;
  1. A request before the 3 s minimum is held until 3 s.
  2. A request during a yellow or red is remembered for the next green.
  3. Reset in the middle of EW_YELLOW returns to ALL_RED_1 immediately.

Use CLK_HZ := 10 in the bench so one "second" is 10 clocks, and follow the self-checking structure from Self-Checking Testbenches.

The top level

The top module connects the clock, a reset button, the pedestrian button, and six LEDs (two sets of red, yellow, green). Pin constraints go in the constraints file. Buttons are usually active-high on FPGA boards but check yours.

Extensions

  • A blinking yellow night mode selected by a switch.
  • Configurable durations through generics.
  • A seven-segment countdown that shows seconds remaining.
  • A sensor input that holds green while a car waits on the other road.

Safety properties first, performance second. A controller that is occasionally slow is acceptable; one that is occasionally unsafe is not. Express the safety rule as an assertion that runs in every test.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the controller use a one-second tick instead of counting 100 MHz cycles in every state?

    A single divider produces an enable pulse; per-state timers count only a few ticks and stay in one clock domain.
  2. 2. What type of FSM outputs does the traffic light have?

    The light colors depend only on the state, which is the definition of a Moore output.
  3. 3. Why is the pedestrian button synchronized and latched?

    Synchronizing avoids metastability; the sticky request flag remembers a press until a green can react.
  4. 4. The best way to check that both roads are never green together is

    A concurrent assert checks the safety property continuously, in all tests.

Summary

  • Combine a one-second tick, a state register and per-state durations to build timed FSMs.
  • Moore outputs from the state give clean, glitch-free light signals.
  • Synchronize the button and latch the request so the FSM can act on it later.
  • Express safety as an always-on assertion and keep simulation fast with a small CLK_HZ.
  • The C model validates the behavior before any VHDL is written.

Next: Project: A UART Transmitter