LED Blinking Program in Atmega16-AVR

To Blink LED in AVR microcontroller, you need to create a LED blinking program for AVR, I hope all of you guys have already installed Atmel Studio 6.0, Proteus. If you have not installed them please install before you proceed further. The links are given below:

Atmel Studio

Proteus: Although, Labcenter Electronics offers a free demo version of Proteus.

Now that you may have installed the software, we are ready to proceed further. Follow these instructions:

  • Open Atmel Studio 6.0

Atmel Studio

  • Click on New Project.

  • In the new window, click on the name column and give whatever name you want to and select the location as well and hit OK.

  • The device selection tool must appear, in the search column search for Atmega 16, select it and then click ok.

Welcome Screen Atmel Studio

  • There you go, a text editor must have appeared on your screen in which you are going to write a program.

General Structure of LED Blinking Program:

To blink led in AVR, you guys should know about the general structure of the LED Blinking program. AVR microcontroller has a register named DDR(Data Direction Register), which acts as a switch between two registers-PORT and PIN. If the particular bit is 1, it means it is allowing data transfer from PORT to PIN register and if that particular bit is ‘0’it means that data transfer from pin to port register is not possible. Also, if you have to write data to the output port, you have to give that value to the PORT register(Output port), and if you have to read data(Input Port), you have to read it from the PIN register.

For those of you guys who don’t know about registers- a register is a memory used to hold data. There are different types of registers-General purpose, Special Purpose and much more. The register I mentioned here is Control register because it is controlling the Data flow(Just like the switch) between Pin register and Port register used for data transferring.

There are various ways to assign the value to DDR register but I am going with the most basic way to do it currently and rest I will write about it in the later post.

DDRB = 0b00000001;

“B” is the name of the port in which you have connected the  LED. 0b signifies to the compiler that the entered value is in binary form and the value signifies that I have given permission to flow of data in Pin 0 of Port B. As these pins can accept input only in binary form, by binary form I mean you can assign only 5v or 0v. There can’t be any voltage between these two values. So by writing this, I am assigning Pin 0 of Port B to 5v.

PORTB = 0b00000001;

According to the points mentioned above, the program should be-

#include <avr/io.h>
int main(void)
{
 DDRB=0b00000001;
 PORTB=0b00000001;
while(1)
       {                //For infinite execution of Program        
       }
}

It’s simple right. Now your work is almost done here. One thing though, this program will only keep the LED ON but it will not blink it. For LED blinking, we have to introduce delay. For delay, we just have to call delay function and enter the parameter in it.

_delay_ms(1000);

The parameter I passed in delay function is in Mili Second, therefore above command should generate the delay of 1 second. One thing though, delay function is defined in util package, therefore you have to include proper header file in the program which is-

#include <util/delay.h>

I hope you guys get the general idea of the program. Full LED Blinking  Program with comments as follows-

#include <avr/io.h>
#include <util/delay.h>//it has delay function
int main(void)
{
DDRB=0b00000001;
while(1)
       {
     PORTB=0b00000001;
     _delay_ms(1000); //so that LED would remain in ON state for 1 sec and then  turns off  for  1 sec.     
     PORTB=0b00000000;
     _delay_ms(1000);
       }
}

After the program, click on Build and then Build “Project name”. If there is no error, then it means your program is successfully build.

Blink LED

When the program is Build, there is a generation of file-“Project name.hex“. For the execution of Program, click on a link given below:

Execution of the program-simulation

Execution of the program-Hardware

Related Post:

AVR Tutorial for Beginner

9 thoughts on “LED Blinking Program in Atmega16-AVR”

  1. Hello.This post was extremely motivating, especially because I was investigating for thoughts on this issue last Saturday.

  2. Nice post. I used to be checking continuously this blog and I’m impressed! Very helpful information specifically the closing phase 🙂 I handle such information a lot. I used to be seeking this certain information for a long time. Thank you and good luck.

  3. I was examining some of your posts on this internet site and I think this website is really instructive! Keep on posting.

  4. This is the right webpage for anybody who wishes to understand this topic.
    You know so much its almost tough to argue with you (not that I really would
    want to…HaHa). You definitely put a new spin on a topic that has been written about for ages.
    Great stuff, just wonderful!

  5. I am very much new to AVR programming.
    However, I am a Software Engineer developing appllications on VB, VB.Net.
    I know that Microcontroller must be Boot Loaded before it is used.
    I want to know that the ATMega16 Microcontroller you are using in this tutorial/project is pre Boot Loaded?
    Please clear.

    Your web site is definitely awsome to learn and implement.

    Thanks

    1. Hey Pradeep.
      Thanks for taking the time to read the blog. The AVR controller is programmed by the USB ASP programmer by default. If you want to program it with some other interface, you have to write a bootloader for it.
      The controller I have programmed here doesn’t have any modified bootloader.

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This
Scroll to Top