LED blink using ATmega32A

The first thing most people do when you want to check or learn microcontrollers is to check for LED blink from a specific port pin. of the microcontroller In this tutorial, we will show how you can make LED blink using ATmega32A microcontroller. We will be using Atmel studio 7 to write program code and upload to the micrcontroller. Atmel studio IDE software is freely available just google it and download and install it. Here we have used AVR ISP MKII as our programmer tool(you can use other available as well). 

First we will show how to setup the hardware. Below picture shows the ATmega32A microcontroller on a breadboard with stable +5V supply module.Also in the circuit we have put 4MHz external crystal to the pin 12 and 13 of ATmega32A with two 22pF capacitor grounded at the other end of the oscillator. The reset pin 9 is pulled high by connecting it to +5V rail using 10KOhm resistor. The same reset pin 9 is also connected to one end of a push button and the other end of the push button is grounded. Pressing the push button will reset the microcontroller. The ground of the microcontroller which are pins 11 and 31 should be connected to the ground rail. Similarly, the VCC and AVCC pins of microcontroller which are pin 10 and 30 should be connected to the +5V power supply rail. Finally we connect one end of 270Ohm resistor to the pin 21 (which is PORT D pin 7) and the other end of the resistor to the positive end of a LED, the other negative end is connected to the ground rail.


LED blink using ATmega32A

The schematic diagram of the same circuit is shown below.

 schematic of LED blink using ATmega32A


The next step is to connect the AVR ISP MK2 programmer to the ATmega32A microcontroller on the breadboard. For this we need to connect 6 connector, SCK, MISO, MOSI, RST, VCC and GND pins of the AVR ISP MKII to the microcontroller pins 8, 7, 6, 9, +5V rail and the ground rail respectively.

This is as shown in the picture below.

ATmega32A and AVR ISP MK 2

Now we will write LED blinking program in Atmel studio and upload the generated hex file to the ATmega32A microcontroller.


Create a new project in atmel studio using File >  New > Project

atmel studio 7 ATmega32A LED blink

In the next window choose GCC C Executable Project, then enter some name for your project such as LED_Blink in this example, choose/create folder for the project then click on OK.


In the device selection window that comes next, search and choose ATmega32A as your device. Then click OK.


Now your project will be created. Once done, project solution will open. You will see code editor on with bare minimum default code as shown in the picture below.

atmel studio 7 ATmega32A LED blink


Now write the following led blink code in the code editor.

#ifndef F_CPU
#define F_CPU	4000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)
	   DDRD |= (1<<PD7);
    /* Replace with your application code */
    while (1) 
	PORTD |= (1<<PD7);
	PORTD &= ~(1<<PD7);


In the above code, first we use the define pre-processor directive to configure the oscillator frequency of 4MHz(#define F_CPU 4000000UL). Note that the F_CPU line is also required for the function _delay_ms() to function otherwise you will get compile error. In order to use _delay_ms() function later down the code we have to use the #include <util/delay.h> because the function _delay_ms() resides inside the util and it's usage code is within the delay.h file. Inside the main() function first we make the port D pin 7 output using the statement DDRD |= (1<<PD7). Within the while loop we repeatdly turn on and off the led. The led is connected to the port D pin 7 so we send periodic high and low signal to it. To do this we first output high signal using the PORTD |= (1<<PD7) statement, wait for 500 milliseccond using the _delay_ms(500) statement, then we make the same pin go low using the PORTD &= ~(1<<PD7) statement and again wait for 500ms using the _delay_ms(500) statement.

Once done change the Debug to Release mode at the top bar as shown in the picture below.

atmel studio 7 ATmega32A LED blink

After that go to Build and hit on the Build Solution. If everything went alright, then you should see Build Succeeded in the output window at the bottom. This is as shown below.

At this point, a hex file called LED_Blink.hex is created in the Release folder within the project folder.

 This hex file will be uploaded to the ATmega32A microcontroller in the next step.

To upload the hex file using ATmel studio 7, go to Tools > Device Programming.


In the device programming window, select tool as AVRISO mkII, device as ATmega32A, interface as ISP, click on the Read button under the Device Signature section. At this point you should get valid device signature if everything is ok. Also click on the Read button under the Target Voltage section and it should read near 5V.

programming atmel studio 7 ATmega32A LED blink

Since we are using external 4MHz crystal oscillator, we have to make changes to fuses setting. Go to the fuses section and set the fuses are shown in the following picture.


Then go to the memories section and browse and select the hex file in the release folder within your project folder. After selecting the hex file here called LED_Blink.hex click on the the program button. If everything is well then your hex code will be sucessfully uploaded to the ATmega32A memory and you should see message at the output window saying 

Starting operation read registers
Reading register HIGH...OK
Reading register LOW...OK
Read registers...OK

See the picture below.

At this point the LED should be blinking on the breadboard. Watch the following video for this.


Also see other ATmega32A tutorials:

- Gas Sensor MQ-2 with ATmega32 and LCD

- LM35 Temperature Sensor with ATmega32 and LCD

Post a Comment

Previous Post Next Post