AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

Interfacing LEDs with the AVR Microcontroller

AVR Seven Segment Display Interfacing

This AVR tutorial will discuss interfacing Light Emitting Diodes with Atmel's AVR series of Microcontrollers. A LED is an acronym for Light Emitting Diode and is basically a electronic device which emits light when an electric current flows through it. The electronic symbol for a LED is : LED Symbol

General purpose LEDs used in digital circuits require around 75mA or less and so we usually use resistors to limit the current flowing through the LED when it is connected within a circuit as shown below. The resistance of the resistor is calculated using the well known formula V=IR.

LED Circuit

Interfacing a LED with a Microcontroller

AVR microcontrollers such as the ATMega8515 only supply a current of about 20mA and so we can drive an LED directly from the microcontroller port eliminating the resistor. In fact if a resistor is added the intensity of the LED will be low.

The figure below shows 8-LEDs connected to an ATMega8515 microcontroller. The code that follows if downloaded to the microcontroller will blink the LEDs continuously. Note that this code could work with other AVR microcontrollers such as the ATMega16, ATmega32, ATTiny2313, etc.

LED ATMega8515 Microcontroller Schematic

Note: In order for the circuit to operate as describe the internal oscillator of the ATMega8515 microcontroller must be program to run at 4MHz.

 *  Written in AVR Studio 5
 *  Compiler: AVR GNU C Compiler (GCC)
 *  Author: AVR Tutorials
 *  Website: www.AVR-Tutorials.com
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
int main()
	DDRC = 0xFF;		// Configure port C as output
		PORTC = 0xFF;	// Turn ON LEDs
		_delay_ms(250); // Wait 250ms
		PORTC = 0x00;	// Turn OFF LEDs
		_delay_ms(250);	// Wail 250ms
	return 0;

Video Demonstrating Code Running on the ATMega8515 Microcontroller

AVR Tutorials hopes this tutorial on interfacing LEDs with the AVR microcontroller was beneficial to you and looks forward to your continued visits for all your microcontroller tutorial needs.