firmwareMIT
Traffic light in Verilog
breadboardhub/traffic-light-in-verilog
verilogtestbenchtrafficlight
B
Mike M
v1.0.0 · UpdatedNov 24, 2026 ·0 downloads ·0 forks
Verilog, with a testOpen in the chip playground
// A traffic light controller: green, then yellow, then red, then green.
// Green lasts 4 clock cycles, yellow 2, red 4. rst starts it on green.
module traffic_light (
input wire clk,
input wire rst,
output wire red,
output wire yellow,
output wire green
);
localparam GREEN = 2'd0, YELLOW = 2'd1, RED = 2'd2;
reg [1:0] state;
reg [1:0] timer; // cycles spent in the current state, minus one
always @(posedge clk) begin
if (rst) begin
state <= GREEN;
timer <= 2'd0;
end else begin
case (state)
GREEN: if (timer == 2'd3) begin state <= YELLOW; timer <= 2'd0; end else timer <= timer + 2'd1;
YELLOW: if (timer == 2'd1) begin state <= RED; timer <= 2'd0; end else timer <= timer + 2'd1;
default: if (timer == 2'd3) begin state <= GREEN; timer <= 2'd0; end else timer <= timer + 2'd1;
endcase
end
end
assign green = (state == GREEN);
assign yellow = (state == YELLOW);
assign red = (state == RED);
endmodule
Description
A state machine: green, yellow, red, repeat. It comes with a test that builds with Yosys and runs in the browser. Set rst to 1 and press Step clock to start it on green, set rst back to 0, then press Run and watch the lights cycle.
Cite this design
@design{breadboardhub/traffic-light-in-verilog,
title = {Traffic light in Verilog},
author = {breadboardhub},
url = {https://www.breadboardhub.com/breadboardhub/traffic-light-in-verilog},
version = {1.0.0}
}