AVR Project – Relay Timer with ATmega8 AVR MCU

Timers are widely used in industrial and domestic application for automating tasks. Microcontrollers can be used to design versatile and accurate timers with ease. Here I present a simple timer that can be used to turn on/off a load after user specified time.

The Timer uses a standard 16×2 lcd module for user interface (UI). User can set the time using a 3 button keypad.

After that Timer is started. While count down is in progress, the time left is displayed on screen.

The program use our LCD driver library more details of which can be found in here. Use avr-gcc + AVR Studio to compile.

The prototype was developed using xBoard MINI, a low cost easy to use ATmega8 development board. The program was burned to the MCU’s flash memory using eXtreme Burner – AVR Software and Hardware. A basic knowledge of working with different tools of AVR development is required, so please refer to following articles.


  • Fuse Must be set as follows, HIGH FUSE=C9 LOW FUSE=FF (Very Important)
  • If display is blank please adjust RV1
Part List
01 ATmega8-16 PU U1
02 16×2 LCD Module LCD1
03 16 MHz Crystal X1
04 BC548 Transistor Q1
05 1N4007 Diode D1
06 4.7K Resistor R1,R2
07 10K Variable Resistor VR1
08 22pF Disk Capacitor c1,c2
09 0.1uF Disk Capacitor c3,c4
10 Large Push Buttons s1,s2,s3
11 PCB Mountable Relay RL1

Schematic (Circuit Diagram)

Relay Timer with AVR ATmega8

Fig.: Relay Timer with AVR ATmega8



A Simple Device Timer project designed using ATmega8

AVR MVU. The Timer is usefully for keeping a device
"ON" for a specific period of time. After the set time
elapse the timer automatically turns the load off.

The Timer uses a standard 16x2 lcd module for user interface
UI. User can set the time using a 3 button keypad.

After that Timer is started. While count down is in 
progress, the time left is displayed on screen.

The program use our LCD driver library more details
of which can be found in Web site.

Use avr-gcc + AVR Studio to compile.

Author: Avinash Gupta
E:Mail: me@avinashgupta.com
Web: www.eXtremeElectronics.co.in




Copyright (C) 2008-2009 eXtreme Electronics, India.


#include <avr/io.h>
#include <avr/interrupt.h>

#include "lcd.h"

//Connection of Load
#define LOAD_POS PC0

//Global variable for the clock system
volatile unsigned int   clock_millisecond=0;

volatile char        clock_second=0;
volatile char        clock_minute=0;
volatile char        clock_hour=0;

void Wait(uint8_t n)

   uint8_t i,temp;


void LoadOn()

void LoadOff()



      //Enable Pullups on Keypad


      int8_t hr,min; //Target Time

      //Initialize the LCD Subsystem
      //Clear the display


      //Set up the timer1 as described in the

      //Enable the Output Compare A interrupt


      //Enable interrupts globally

      LCDWriteString("    Welcome     ");
      LCDWriteStringXY(0,1,"   Relay Timer  ");


      LCDWriteString("Set Time - 00:00");
      LCDWriteStringXY(0,1," Start     ^");

      uint8_t selection=1;
      uint8_t old_pinb=PINB;

         while((PINB & 0b00000111) == (old_pinb & 0b00000111));

         //Input received

         if(!(PINB & (1<<PINB2)) && (old_pinb & (1<<PB2)))
            //Selection key Pressed

               selection =0;

         if(!(PINB & (1<<PINB1)) && (old_pinb & (1<<PB1)))
            //Up Key Pressed

            if(selection == 1)

               //Hour is selected so increment it

               if(hr == 100)
                  hr =0;

            if(selection == 2)

               //Min is selected so increment it


               if(min == 60)
                  min =0;

            if(selection == 0)
               //Start Selected


         if(!(PINB & (1<<PINB0)) && (old_pinb & (1<<PB0)))
         //Down Key Pressed

            if(selection == 1)

               //Hour is selected so decrement it

               if(hr == -1)
                  hr =99;

            if(selection == 2)

               //Min is selected so decrement it


               if(min == -1)
                  min =59;

            if(selection == 0)
               //Start Selected



         //Update Display

         LCDWriteString("Set Time - 00:00");
         LCDWriteStringXY(0,1," Start    ");




         if(selection == 0)

         if(selection == 1)

         if(selection == 2)



      //Start the Load


      //Now start the timer
      clock_hour = hr;
      clock_minute = min;
      clock_second =0;

      LCDWriteString("  Power Off In ");


         if((clock_hour == 0) && (clock_minute == 0) && (clock_second == 0))
            //Time Out


            LCDWriteString("Load Turned Off");

               LCDWriteStringXY(0,1,"*Press Any Key*");


               LCDWriteStringXY(0,1,"                ");


               if((~PINB) & 0b00000111)




   //Continue again



//The output compate interrupt handler
//We set up the timer in such a way that
//this ISR is called exactly at 1ms interval



My Prototype.

I used the xBoard MINI as a base board for making the timer. The external components attached were the LCD and 3 push buttons.

Fig.: AVR Timer Project Prototype.


Using the Timer

The user interacts with the timer using 3 push buttons and the LCD module. The function of 3 keys are :-

  1. Select Key – Select Different items like hour,minute and start. It moves the arrow on screen to indicate which option is currently selected.
  2. Up/Increase Key – Increase the value. For example, select "minute" using "select" key and press this to increase its value.
  3. Down/Decrease Key – Same as above but it decreases the value.

When the timer is powered up the load is in off state. A welcome message is shown. Then you can set the time using the above buttons. After that move to "start" option by using select key. Then press any key to start the timer. Now the screen shows the count down and the load is powered on. When count down reaches 0 the load is turned off. The pictures and videos below illustrate the process.

timer operation

Fig.: Use SELECT key to move between options.



Fig.: Count down in progress (HH:MM:SS).



Fig.: The Load Turned Off.


JLCPCB Prototype: $2 for 10 pcs PCBs, 48 Hours Quick Turn

China’s Largest PCB Prototype Enterprise, 300,000+ Customers & 10,000+ Online Orders Per Day

Get quote online easily: https://jlcpcb.com/ quote

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.


Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

Hobby Robotics Wheels

62 thoughts on “AVR Project – Relay Timer with ATmega8 AVR MCU

  • By Zagg - Reply

    Hi !
    Thanks again for your very great tuto !


  • By Zagg - Reply

    Hi again!
    An idea proposal for your next project/tutorial :

    Adding output to ATMega with an 74HC595 :–)

    Have a nice day!


  • By Uttam Dutta - Reply

    Mr. Avinash
    Thank You again for developing such a nice stuff. I am waiting long for such kind of tutorial, one request-how can I marge real time clock with it.
    Uttam Dutta

  • By kapil - Reply

    Please describe the fuse….
    How can set the fuse bits for the external crystal and for other purpose.

    • By Puiu - Reply


      Fuse Must be set as follows, HIGH FUSE=C9 LOW FUSE=FF (Very Important)
      If display is blank please adjust RV1

  • By Businge elly - Reply

    this is a very nice tutorial and i have liked it.Please am begging you could you send me a pdf of the tutorial.my email is ell86bu@gmail.com

  • By Lee - Reply

    Thanks for your great tuto.

  • By Gandoulf - Reply


    You can set the fuse bits by this way (with avrdude):

    avrdude -c %your programing device% -p m8 -P %programing device emplacment% -U hfuse:w:0xC9:m -U lfuse:w:0xFF:m

    Have a nice day !

  • By kapil - Reply

    @ Gandoulf
    thanx. But how you decide about the fuse bit and how they are calculated. one more point is that this is for mega8 how fuse bit is set for the mega16/32/8535 or other?

  • By HYNIX - Reply


    Man ever herd of a thing called DATASHEET ???????????

  • By Marian Neagu - Reply

    Hello!First of all I want to thank you for this tutorial.
    Now I have a question: can I use an ATTINY2313 instead Atmega8 to build this timer?
    I have to say I’m newbie in microcontrollers and when I try to use attiny, it let me set the clock, but when I start the timer, instead of “:” between hours and minutes and seconds it display all kind of characters changing.
    Thank you again.

    P.S. Please excuse my english.

  • By andy - Reply


    Nice tuto, could you show me how to setting about clock source for AVR…?
    I used extrnal crystal 4MHz with Atmega8535, what are registers to be setting to make avr work in proper clock…?

    Cause default setting is CKSEL = 0001 and SUT = 10.

    Thank you

  • By Antons - Reply

    Can you please give me some clue for which compiler is the code written for?
    Cannot just manage to get it parsed by my CodeVisionAVR 2.04

    Thanks in advance!

  • By Avinash - Reply


    I think you have not read the comments given on the code !!!

  • By Antons - Reply

    Sorry my bad!
    As it usually goes: you skip a line == you skip everything…
    Pity that those C notations are so different.
    Nice tutorial of a nice project, by the way!

  • By Dreggie - Reply


    that’s a nice project ^^

    I would need the same but with a second relay, which turns off at last 10 minutes. It’s for turning off the heat-resistors.

    Need it for my clothes dryer, but it’s an older one, so I don’t get a new timer for it.

    Couldmaybe someone help me out for a chanced code?

  • By seema - Reply

    Hello avinash,

    i have some crazy problem …can you please help me ?

    I am using AVR Studio4 as compiler and JTAGICE mkII and ATmega16 controller .
    Problem is like when I build my program then building is sucess but it showing following things:

    AVR Memory Usage
    Device: atmega16

    Program: 17148 bytes (104.7% Full)
    (.text + .data + .bootloader)

    Data: 151 bytes (14.7% Full)
    (.data + .bss + .noinit)

    Build succeeded with 5 Warnings…

    and I am not able to put it in my controller its tell hex file does not fit in selected device

    can you help me ? Is it like I have to change my controller?

    Program: 17148 bytes (104.7% Full) is this mean my controller is full

  • By seema - Reply

    hello Avinash

    plz reply I am in problem …with this error

  • By Ritesh - Reply

    Hi avinash,

    what is use of

    why you put this out of main() function ?

  • By sanjeev pandey - Reply

    hi, i m final yr student of EE. i m making a project by using
    atmega853 which is used for the protection of dc motor(12v) with the help of over voltage, undervoltage and over current relay and the value of voltage and current is also being displayed on the lcd . plz can u send me the proper ckt. diagram as well as the programming.

    • By Avinash - Reply

      Don’t spoil the name of Indian Students in front of the world. This is no homework site! Why would anyone do the project work for you ??? Its your headache to handle it your self.

  • By Felix Khan - Reply

    Hello Mr. Gupta:

    I have recently started learning AVR and C language programming. I found your website through search engine as I was looking for learning material that would get me started at a beginners level. You are doing a excellent job in explaining this rough and tough material in a very simplistic way. I love your approach of being repetitive and understanding of the beginners needs.

    I am wondering if you write code for others who wish to compensate you for your time? I have a project that I would like to have coded by someone who has the experience to keep the code tidy. If you are interested in helping me out then please let me know. I would love to call you if you wouldn’t mide sharing your phone number with me.

    I hope you keepup the good work.

    Best regards.

    Felix Khan

    Tucson, Az

  • By murali - Reply

    Hi Avinash
    how to set high fuse =c9 low fuse=FF. I am using ponyprog2000 for writing my atmega8 chip.
    My timer is taking 16secs to skip 1 sec to turn off the load.

    • By Avinash - Reply


      See the forum there is a topic on Ponyprog

  • By murali - Reply

    Hi Avinash,
    Thank u for the response. Could not locate the topic ponyprog on the forum, please help.

  • By Neil Ablang - Reply

    any reason why the variables below are set as char instead of integer int?

    volatile char clock_second=0;
    volatile char clock_minute=0;
    volatile char clock_hour=0;

    • By Avinash - Reply


      Because we are running on 8bit CPU and it can handle 8bit variable much more efficiently. It we know value that a variable will hold is in range of -127 tp +128 then we use char only. Another typedef for it is int8_t which stands for 8 bit integers.

  • By neil ablang - Reply


    thanks for the clarification. uint8_t can be used too for those global variables? I am trying use part of your codes into my programs, being newbie on AVRs but not on C.

    anyway, I found your tutorials so easy to follow . It helps a lot, along with reading the Datasheets.

  • By shohaib - Reply

    thanks for this project…..
    I remained searching for hardware requirements for a ATMEGA8l for proper operation for the whole last week,
    basically i was trying to shift to AVR’s from 89c51’s,
    so was trying to find, what are the necessities….
    but was unable to find one,,,,
    but your project clearly shows , what hardware is necessary,
    once again thanks

  • By paul - Reply


    je vien de r√©aliser ce montage,j’ai juste un petit probl√™me a la fin du d√©compte l’affichage reviens a welcome.ont a juste le temps de voir la ligne pour relancer le programme.



  • By paul - Reply


    Problème résolu par la mise en place de poussoirs liniature a la place de modeles plus gros !

  • By masoud - Reply

    hi i am iranian
    thanks for it circuit

  • By Pepe Gonzaez - Reply

    Dear Sir:

    I just build this project, but your C code counts HH:MM, what I have to do to change it to count MM:SS


  • By Raj - Reply

    Dear all,
    I want to make an event detection timer using 16 MHZ external crytal and atmega32 microcontroller which will be able to calculate the event difference between two port in nano second range. Please help me.

  • By Emil - Reply

    I have the same problem like “pepe gonzaes”: i want to use this countdown timer for a UV exposure unit PCB and I need Minute:Seconds.
    If you can put a HEX for this it will be great (and appreciated!)

  • Pingback: universal auto matic relay circuit to disconnect charger from supply in ful charging

  • By kostas - Reply

    Dear avinash,
    Thank you very much for sharing such working projects!!! i really love your tutorials … always helping me to learn manipulating avrs!! I would like to ask you what the function of the myutils.h file and if the lcd.c and lcd.h files are written by you or are libraries for driving lcd’s?
    thank you very much in regards!

  • By Riey - Reply

    This relay timer only could work with 16Mhz External Crystal, cannot lower or higher, or will not exactly match with the clock.
    Attention for the fusebit, Can set with AVRIOSP Software.
    I suggest you better make it with CMOS IC Such as 74HC595 or 74LS164.
    Keep Posting for the next an amazing project…

  • By handrianto - Reply

    thank for you aplication

    mr i have problem with this project because when i compile this program in code vision evaluation there is error in this part
    void Wait(uint8_t n)

    uint8_t i,temp;


    in this part VOID WAIT ( UINT8_T N )
    is declration syntax error

    tanks for u kindness

  • By handrianto - Reply

    thank for you aplication

    mr i have problem with this project because when i compile this program in code vision evaluation there is error in this part

    in this part VOID WAIT ( UINT8_T N )
    is declration syntax error

    tanks for u kindness

    • By Avinash - Reply


      because you forget to read this instruction

      “Use avr-gcc + AVR Studio to compile.”\

      Its there in the program itself.

      Please I don’t like careless peoples making mistakes and eating my time !

  • By handrianto - Reply

    i very apriciate to your project
    i will try it for my student

  • By Rajeev - Reply

    Can you plese publish a clock with a light control to On/Off Porch light at every evening (eg. On at 6.30 pm and Off at 10.30 Pm unattended by settin On off time)

  • By Vito - Reply

    Circuit on the breadboard tested at the moment and with two types of relay 5V, works very well.
    Of course, I had to set the fuse as indicated on the site, otherwise it will not work well, he was going too slow.
    I think it should also work with a relay 230V, right?

  • By nishad - Reply

    plz give pcb layout of this pro

  • By Jatin - Reply

    Problem solved Guys !!!
    Was able to recover those IC’s by providing an external crystal support .
    The Project now works PERFECT !!!
    Just as word of precaution
    ALWAYS ..flash the AVR first and then set the Fuse to HIGH:C9 and LOW:FF ….Dont do it the other way round .
    This was the mistake I did …Now i learnt !!
    Thanks for a great project .

    • By Avinash - Reply


      You are wrong and confused, you can do it either way. But I don’t have time telling you what wrong you are doing coz it’s just lack of your common sense.

  • By Abdul majeed - Reply

    Hey Avinash I have a problem.
    I am driving a relay from attiny2313 microcontroller through BC546 transistor. When i connect ac appliance eg a bulb and drive the relay then it becomes on then suddely becomes off.But if no ac load is apllied then relya works fine what could be the problem. Should i saprate the ground of mcu from relay driving transistor ground?

  • By omid - Reply

    Hi tanks for you
    very very good
    omid from iran from iran
    goodbay engineering

  • Pingback: IR d√°lkov√© ovl√°d√°n√≠ a ?asova? k DSLR Sony | elektro.akceasouteze.cz

  • By Andr√© Souza - Reply

    Hello, could you help me set a schedule for a timer cascade that controls 15 ports or more? After that triggers the first, the other will then switch on 3 for 3 seconds. Are you doing this?
    With atmega?

    • By Sandeep - Reply

      Have you found solution for Sequential timer ?

      If yes, please send me details

      Thanking you in advance

  • By manoj - Reply

    sir: i do my final year project ( automatic synchronization)

    can you help me?
    i need program for frequency comparator , counter ,timer program

    in my idea?

    first select timer counter
    set timer t1,

    next timer mode
    count port
    reg move
    port a t1,t2
    compare reg timer
    if less than port c1 on
    if greater than port c2 on
    if equal port c3 on

  • By Nic - Reply

    Can this controller programmed in such way to set the countdown time from a week up to 2 months?

  • By nima - Reply

    Hi thanks for your projects;
    I have a quuestion.
    i want to create a timer that set and get number from keypad.
    i.e set the hour and minute with numbers on keypad.
    please help me!

  • By Ritabrata - Reply

    Hello Avinash,

    The Circuit design and your explanations have been very helpful. However, I, myself not being a programmer would require some additional help if its not of too much trouble to you.
    I require the system to ask the user for a particular time period for which it will stay on and also a time period for which it will stay off. Having entered the time period the system should enter an infinite loop of operation. It will keep being ON for the SET period and remain OFF for the SET period. the time set should not get wiped off at power down.

    I wish to employ this as an airconditioner controller at my place!!

    I would be grateful to you if you could help me out!

  • By andreas ioannides - Reply

    Hello Avinash,how are you , i am andreas ioannides from cyprus , nice to meet you ,,

    i do a project that i find it on the web with dmx and stepper motor ,,

    as i see your project is atmel avr gcc ..

    i need very much your help .. for my project ,,

    can you please find little time to share this project together ,,

    please email me

    best recards


  • By Surjit - Reply

    Sir how can give me example on eeprom based timer where the interval valve can be store in eeprom.

  • By Dyra - Reply

    Hello sir, nice to meet you !!!
    is a good project, i’ve tried it and succeeded. only what if the program listing to control with the second, please ……………., thanks a lot

  • By nithin - Reply

    very nice project sir.
    but i have a dought, when the supply is stoped to micro controller then what about the timer it will stop .
    but i want to stop the timer when supply is stoped and continue after supply is given with the time where it stoped so what i should do..
    plz rply me sir..

  • By Vitaliy - Reply

    Executed your project on Atmega16.
    Used the internal generator on 8 MHz
    The select button does not work.
    up and down work fine.
    Please tell me what could be the problem!

  • By Humayun Kobir - Reply

    temp=n*28; can you pls clarify this line?

Leave a Reply

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

− three = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>