Write Your First Verilog Module: A Blinking LED Counter
Write a Verilog blinking LED counter, simulate it with a testbench in Icarus Verilog, then see the Yosys report for an iCE40 FPGA build.

The blinking LED is the "hello world" of programmable hardware, and it is a better first project than it sounds. It uses a clock, a register, an adder, a pin constraint and a way to see the result. This post writes it in Verilog, checks it in simulation with Icarus Verilog 12.0, and shows the Yosys 0.40 synthesis report for a Lattice iCE40 part. It assumes you can read basic Verilog syntax, and defines the terms it uses along the way. The same module drives the next posts in this cluster, where it gets built and flashed.
What the design must do
The board has a clock oscillator that runs at 12 MHz, which is 12,000,000 rising edges per second. A human eye cannot see an LED change that fast, so the design must count clock edges and change the LED only every so often. A counter that adds one on every edge counts up and wraps around. A high bit of that counter changes slowly, and if the LED shows that bit, it blinks.
The relationship is simple. Bit n of a free-running binary counter toggles every 2^n clock cycles. At 12 MHz, bit 23 toggles every 8,388,608 cycles, which is about 0.70 seconds, so the LED changes state about every 0.7 s and a full on-off period is near 1.4 s. If you want a faster blink, use a lower bit, such as bit 22.
The module
module blink (
input wire clk,
output wire led
);
reg [23:0] counter = 24'd0;
always @(posedge clk)
counter <= counter + 24'd1;
assign led = counter[23];
endmoduleWalk through it line by line.
module blink (...) declares the design and its two ports. clk is an input wire carrying the oscillator. led is an output wire driving a pin.
reg [23:0] counter = 24'd0; declares a 24-bit register with an initial value. The [23:0] range means bit 23 is the most significant. The initial value is honoured by FPGA flows, because the starting contents of each flip-flop are written into the configuration. For a chip that cannot do that, you would add a reset input instead.
always @(posedge clk) counter <= counter + 24'd1; is the whole counter. On every rising edge, the register loads its own value plus one. The non-blocking <= is the right operator for clocked state. The literal is written with an explicit width, 24 bits, so the addition has no surprise about size. When the counter reaches 24'hFFFFFF the next addition wraps to zero.
assign led = counter[23]; connects the top bit to the output. There is no logic here, only a wire from a flip-flop to a pin.
Simulating it first
You cannot watch an LED in a simulator, but you can watch the signal. A full 0.7 second toggle is far too many clock cycles to simulate quickly, so the testbench overrides the width with a smaller counter. The blink module above has a fixed width, so use a parameterised version for testing:
module blink_p #(
parameter BIT = 23
) (
input wire clk,
output wire led
);
reg [BIT:0] counter = {(BIT+1){1'b0}};
always @(posedge clk)
counter <= counter + 1'b1;
assign led = counter[BIT];
endmoduleThe counter now spans bits BIT down to 0, and the output shows the top one. A testbench can pick BIT = 2 so the LED toggles every four clock cycles:
`timescale 1ns/1ps
module blink_tb;
reg clk = 1'b0;
wire led;
integer i;
blink_p #(.BIT(2)) dut (.clk(clk), .led(led));
always #5 clk = ~clk;
initial begin
for (i = 0; i < 12; i = i + 1) begin
@(posedge clk);
#1 $write("%b", led);
end
$write("\n");
$finish;
end
endmoduleThe counter starts at 0. After the first rising edge it is 1, after the second 2, and so on. The output is bit 2 of the counter value after each edge. The values after edges 1 to 12 are 1,2,3,4,5,6,7,8,9,10,11,12 and bit 2 of each is 0,0,0,1,1,1,1,0,0,0,0,1. Compile and run:
iverilog -g2005 -o blink_tb.vvp blink_p.v blink_tb.v
vvp blink_tb.vvp000111100001The pattern is four ones, four zeros, repeating, which is what a toggle every four cycles looks like when it started low and was sampled just after each edge. Seeing it behave on a small width gives you confidence that the 24-bit version does the same thing, only slower.
Constraining it to pins
A simulator does not know which pin carries the clock. The build does need to know, and the open iCE40 toolchain reads a PCF file with lines of the form set_io <port> <pin>. The pin numbers depend on the board's schematic. For an iCEstick, which has a 12 MHz oscillator and several LEDs, the file would look like this:
set_io clk 21
set_io led 99Treat those numbers as an illustration. Pins are specific to a package and a board, and the schematic or the vendor's constraint file is the source of truth. A pin assignment that does not exist in the package fails at place and route, and one that exists but is wired to something else fails silently on the board.
Synthesising it
Yosys reads the Verilog and maps it to iCE40 primitives:
yosys -p "read_verilog blink.v; synth_ice40 -top blink; stat"The statistics block, trimmed to the cell lines, lists a flip-flop per counter bit, lookup tables for the adder inputs and carry cells for the fast carry chain:
Number of cells: 72
SB_CARRY 23
SB_DFF 24
SB_LUT4 25The exact numbers vary with Yosys version and options, but the structure does not. A 24-bit counter costs 24 flip-flops, and the adder rides the dedicated carry chain, which is why it uses so few lookup tables. The led output costs nothing extra, because it is a direct connection to the register's output.
The remaining steps are place and route with nextpnr, packing with icepack, and flashing with iceprog. The next cluster in this series walks through them for an iCE40 board.
Variations that teach something
Once the first blink works, change it in small ways, and predict the result before you rebuild.
Faster blink. Use
counter[21]instead ofcounter[23]. Each lower bit doubles the frequency.Two LEDs. Declare
output wire [1:0] ledand assign{counter[23], counter[22]}. Each LED then blinks at a different rate.Duty cycle. Replace
counter[23]withcounter[23] & counter[22]. The LED is on for a quarter of the period, because both bits must be high.A reset button. Add an input
rst, make the counter clear when it is high and remove the initial value. This is the portable version.
Each change is one line, and each shows a different part of the model: a lower bit is a faster signal, a gate is logic after a register, and a reset is a mux in front of the flip-flop input.
Common first-time problems
If the LED stays dark, check the clock first. A wrong pin for clk gives a counter that never runs. If the LED blinks too fast to see, you have taken a bit that is too low, and it looks continuously dim. If the build complains about a missing pin, a port name in the PCF file does not match the module. If simulation shows x on the LED, the counter had no initial value in a testbench that does not honour it, or the module was instantiated without a clock.


