Electronics - Verilog - Blinking a LED

Blinking a LED, a basic step.

But without this first step, there won't be a second.

And without second, no third and so on.

This simple tutorial will explain basics in order to program a blinking system.

Thus this timer example in Verilog could be seen as an Hello World exercise.

Let's get started.

Explanation

This tutorial has been made with the Altera DE1 board.

In Verilog, every program starts by a module.

This module will declare ports as:

  • input;
  • output;
  • inout.

A clock is an input because the pin where the clock is connected has to receive data from this clock.

An output will send data from the program to the pin.

So it's normal to set a LED as an output.

Our board has four green LEDs.

For our example we're going to use only two of them.

LEDG[1] will be always lit and the LEDG[0] will blink.

The reg element is there to hold a value.

1'b means that it'll be only 1 binary digit in this reg element.

So we declare three reg elements, one for a data holding a value of 1 in binary, the second to create a counter and the third to have a state.

Of course this state will change during the process.

Indeed, in the always@ block, we tell the process that for each clock's edge, the counter will be incremented by 1.

For the first code, called Quick blinking, we also tell the process that the state value will change  everytime the counter reach 2^20.

Why 2^20?

Because in binary this value is: 100000000000000000000.

We've twenty zeros, so we've 2^20 in binary.

In a decimal base it will be 1,048,576.

Our clock is at 50 MHz, meaning fifty million oscillations per second (it's huge, yes).

If we would like to know how long the LEDG[0] will stay high (switched on) or low (switched off) we've to do:

1,048,576 / 50,000,000 = 0.02097152

So approximately 0.02 second.

This time represents the moment when the LED is high (1) or when it's low (0).

So to retrieve the period (a cycle) we've to multiply this result by 2.

Then:

 0.02097152* 2 = 0.04194304

Approximately 0.04 second.

Now to know how many times the LEDG[0] will blink per second, we've just to do:

1 / 0.04194304 = 23.8418579102

So approximately 24 times per second, that is 24 Hz.

This frequency is a bit too quick.

Let's reduce it with the second code, called Slow blinking.

For that we've to change the counter-27th's bit (so 2^26).

Same as earlier:

2^26 = 67,108,864

Consequently:

67,108,864 / 50,000,000 = 1.34217728

And:

1.34217728 * 2 = 2.68435456

Our period is approximately 2.7.

1 / 2.68435456 = 0.37252902984.

Then the frequency is approximately 0.37 Hz.

And if you check the LED after sending the HDL on the board, you should see the LEDG[0] blinking once every 1.3 second (from low to high or high to low) and 2.7 seconds for a complete cycle (from low to low or high to high).

Code

Quick blinking

// Quick blinking a LED

/* module */
module blinking (

    input CLOCK_50,
    output [1:0] LEDG
    
);

    /* reg */
    reg data1 = 1'b1;
    reg [32:0] counter;
    reg state;
    
    /* assign */
    assign LEDG[0] = state;
    assign LEDG[1] = data1;
    
    /* always */
    always @ (posedge CLOCK_50) begin
        counter <= counter + 1;
        state <= counter[20]; // <------ data to change
    end

endmodule

Slow blinking

// Slow blinking a LED

/* module */
module blinking (

    input CLOCK_50,
    output [1:0] LEDG
    
);

    /* reg */
    reg data1 = 1'b1;
    reg [32:0] counter;
    reg state;
    
    /* assign */
    assign LEDG[0] = state;
    assign LEDG[1] = data1;
    
    /* always */
    always @ (posedge CLOCK_50) begin
        counter <= counter + 1;
        state <= counter[26]; // <------ data to change
    end

endmodule

Conclusion

A simple timer is a good way to start learning Verilog.

In this tiny code there were a lot of things to know and you are now aware about that.

Well done, you've made it. laugh

Comments

Comment: 

Great tutorial I must say. If I would like to blink the LEDG alternatively. What should have modify from your code?

Comment: 

Hello KentMax,

There are several green LEDs.

Which one do you want to blink?

The code already blinks LEDG[0]. laugh

Comment: 

Thank you Mi-K, it worked :D
You are a great person :D, I want to contact you for asking some questions regarding to my project, can you give me your email or anywhere I can contact you?
Thanks, really thanks, you made me believe in humanity again :D

Comment: 

Hello MCubbed,

Thank you for your compliments.

Glad to see that you're hope is still there. cool

Unfortunately I'm a bit busy at the moment and I couldn't help much as I do in my tutorials.

I'm sure you can understand. wink

Comment: 

Hello,
If i want to do the mini project on blinking LED by using the odd and even form,
what should i modify from your coding above.
I hope you can help me.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.