Tuesday, 12 June 2012

Code to Make the LED Blink

This is a very simple program, the desired target is to make the LED's on the BASYS2 board blink, and by blinking I mean a very visible turning on and off. This can be very easily achieved in verilog. The code can be found below:

module led_simple(
    input clock,
    input reset,
    output led, led2, led3, led4, led5

reg [26:0] count; //A sizable 27 bit register so that the blink can be seen and is visible, too small a register will make the
      //register stay on as it will blink extremely fast.

always@ (posedge clock or posedge reset)
  if (reset)
   count <= 0;  //if reset button is pressed, initialize or reset the register
   count <= count + 1;  //otherwise increment the register
assign led = count[26];   //MSB connected to output led. and the other outputes conncted as below
assign led2 = count[25];
assign led3 = count[24];
assign led4 = count[23];
assign led5 = count[22];

The concept is very simple. I defined a 27 bit register to count from 27'b0 till 27'b1. Every time the MSB and the MSB-1, MSB-2... become 1 the LED connected to that bit will turn on, otherwise it will stay off.

The register can be of any size, but it should be large enough for it to take some time to activate the MSB thus making the blink nice and visible.

The above code has been tested and can be programed onto the BASYS2 board, using the BASYS2 .ucf file I posted. Ofcourse the file must be edited for it to work, only the required pins must be used. For example the original .ucf file for this code will be:

# Pin assignment for LEDs 
NET "led5" LOC = "n5" ; 
NET "led4" LOC = "p6" ; 
NET "led3" LOC = "p7" ; 
NET "led2" LOC = "m11" ; 
NET "led" LOC = "m5" ; 

# Pin assignment for pushbutton switches
NET "reset" LOC = "a7"; 

# Pin assignment for clock
NET "clock" LOC = "b8"; 

The above code can be seen working in the video here:


  1. these tutorials are great! I am a computer engineering student, and verilog is one of my subjects. Very helpful

    1. Thank you, I'm glad its helping you out :)

  2. Hi, I'm a computer science student and not familiar with engineering courses. I wanted to say thank you! Your articles just saved my life :-)