Controlling AC devices with AVR using Relays
Everyone of us wants to control a bulb, a fan etc at some point in time. We will look at driving single phase AC devices with a Relay using Atmega32.
Contents
[hide]Basics
The relay is driven using a simple transistor switch circuit. You may look at the schematic of the driver circuit from Ultra AVR Dev Board Schematic. It is a common collector transistor switch circuit. It provides additional current required to drive the relay. The current from the controller port pin is not sufficient, hence the driver is used.
Be careful while driving AC loads. It can be quite dangerous. Do not turn on the mains power while hooking up the circuit. You've been warned!
Hookup
The Ultra AVR Dev board comes with Relays on board. You simply need to hook up the relay inputs with the the port pins as shown in the figure below.
The Code
The code turns ON and OFF the relays, every 5 seconds.
#include <avr/io.h> | |
#include <util/delay.h> | |
#define Relay PA0 | |
int main() | |
{ | |
/* Configure the port A0 as Output */ | |
DDRA = (1 << Relay); | |
while(1) | |
{ | |
PORTA = (1 << Relay);; /* Turn ON the Relay */ | |
_delay_ms(5000); | |
PORTA = (0 << Relay);; /* Turn OFF the Relay */ | |
_delay_ms(5000); | |
} | |
return (0); | |
} |
Demo
Video Tutorial
For those of you, who would like to watch instead of read we have made a video with all the gyan.
Downloads
Download the complete project folder from the below link:
https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip
Have a opinion, suggestion , question or feedback about the article let it out here!

AVR I/O Register Configuration
In this tutorial we are going to discuss the port configuration of AVR/Atmel controllers or in general Atmega family. In this tutorial we will be using Atmega32 as reference, same will be applicable...

Basics of AVR 'C'
Let us look at the basics of 'C' for programming AVR Micrcontrollers in this tutorial. Simple stuff like setting and clearing bits is important to any project you do. It is often required to set...
AVR Hardware and Software Setup
In this tutorial we will look at the basic setup required to get started with AVR series of microcontrollers. There are two aspects to it, the software and the hardware. Fortunately, we for AVR...

AVR Timer programming
Basics Timers come in handy when you want to set some time interval like your alarm. This can be very precise to a few microseconds. Timers/Counters are essential part of any modern...