Tutorial 4 : Get Started “Blinking a LED”

In previous tutorials we have talked about what is a microcontroller and how it works now is the time to write our first program for blinking an LED. So Let’s Go :

Setting up a New Project in AVR Studio. Follow the Steps :

1.) Open your AVR Studio IDE

open avr dude

2. Click on new project


3. Select your compiler as AVR GCC which you have installed as WinAVR earlier

4. Write your project name (Remember DO NOT use spaces and special characters in the project name) or otherwise you would have to do the same process again.

5. Select the folder where you want to save your programs

6. Click on next


7. Select your Debug Platform as AVR Simulator

8. Select your device as ATmega16 from the list (As we are working on ATmega16)

9. Now click on finish and you are done setting up a new project.


After your setup is done you will be prompted to the working window of the AVR Studio and this is where we will write our program and debug it These steps are common throughout the course so remember them as they will be used repetitively whenever we are creating a new program.

Experiencing the AVR Studio IDE :

AVR Studio Window

Now coming through the coding part .. Everybody put your hands on keyboard now..!!

Okay now beginning with the coding, first thing i want to clear here is that the coding of a microcontroller with bare C is somewhat tricky task but it is more awesome as you will be working on the bare codes of every port I mean you have to configure every PORT on your own and it does improves the understanding for how actually things inside work rather then getting a pre-written library as in the case of Arduino Library.

So basic structure of AVR C code will be like as follows :

  1. Including Basic Header Files
  2. Defining CPU Frequency
  3. Inserting a main function
    • Inserting static variables (Defining Control Registers and other variables)
    • After static definitions we will insert a infinite while loop where the main program resides (The actual fun belongs here). This part repeats itself over and over and check the conditions we apply here which would do the required work.

In beginning we will go nice and easy by familiarizing ourselves to the environment. Each line of the code will be commented and explains the function of a particular line of code.

Code for blinking the LED :

#include<avr/io.h>            // Basic Input/Output functions of AVR
#define F_CPU 1000000         // Defining CPU Frequency = 1 MHz
#include<util/delay.h>        // Delay header file for AVR

int main()                    // Initializing the main function 
    DDRB = 0b11111111;          // Defining our PORTB as a output port
    while(1)                    // An infinite loop
         PORTB = 0b00000001;    // PORTB1 == 1
         _delay_ms(1000);       // Delay of 1000 mili-second = 1 sec.
         PORTB = 0b00000000;    // PORTB1 == 0
         _delay_ms(1000);       // Delay of 1 second



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s