Programmable digital timer switch using a PIC Microcontroller

Digital timer switches are used to control the operation of electrical devices based on a programmed schedule. This project describes a programmable digital timer based on a PIC16F628A microcontroller that can be programmed to schedule the on and off operation of an electrical appliance. The appliance is controlled through a relay switch. This timer switch allows you to set both on and off time. That means, you can program when do you want to turn the device on and for how long you want it to be remained on. The maximum time interval that you can set for on and off operation is 99 hours and 59 minutes. The project uses a 16×2 character LCD with 4 push buttons to interact with the user.

Circuit Design

The circuit diagram of this project is shown below. A 5 V relay is driven by a PN2222 transistor that is controlled by RB3 pin of PIC16F628A. Digital inputs from the 4 push buttons are read through port pins RA2, RA3, RA4, and RB0. The functions of these push buttons are discussed in the operation section below. A standard 16×2 character LCD is used in the project to display the the device status, program menu and time. The LCD is operated in 4-bit mode, therefore, only 6 I/O pins of PIC16F628A are required to drive it. A piezoelectric buzzer provides audible tone when the timer is started and stopped. It also beeps when the device is turned on or off. The + 5V power supply for the circuit is derived from a LM7805 regulator IC. The input to the regulator is given from a 9V DC wall adapter.

In the circuit diagram, the pins 15 and 16 of the LCD are shown open. These pins are available only in those LCDs that have a back light illumination LED. The pins 15 and 16 are the anode and the cathode of the LED. If your LCD has the back light LED, you can connect these pins to the power supply terminals with a 39 ? resistor in series. The backlight LED enhances the readability of the LCD display in low illumination condition.

The complete circuit soldered on a general purpose prototyping circuit board is shown below.

Operation of the timer

The timer gets inputs from the 4 push buttons. Their functions are described as follows:

  • ON/OFF TIME : This timer device allows you to set both on and off time. When the timer is initially powered on, the device is in off condition and both on and off times are 0. Pressing this button, you can switch between the on and off time on the display.
  • SELECT : This allows you to select between the on and off time settings as well as hour and minute digits. The selected digit is incremented by pressing the ON/OFF TIME button.
  • ENTER : When the appropriate hour and minutes are selected, pressing ENTER finalize the corresponding on or off time.
  • START/STOP is to start or stop the timer. If the timer is already on, you can stop it at anytime during its operation by pressing this button.

Now lets see how it works. Suppose, the device connected to the relay switch is needed to be turned on after 2 minutes. Further, once it is turned on, it is required to be on for next 20 minutes. In this case, the off time is 00:02 and the on time is 00:20, in hh:mm format. Once the timer is started, the device will be turned on after 2 minutes and remained on for 20 minutes. After that it will be turned off again. The following video shows how to do this.


The firmware is developed using the MikroC Pro for PIC compiler.
Download the Source Code

Download the HEX file

Digital timer switch

Update (04/12/2013)
Mark Weir from Australia sent us his revision of our Programmable digital timer switch project. He modified the original code, which was written for PIC16F628A, to incorporate it with PIC18F4620 microcontroller, while hardware and overall functionality remain the same.

Digital timer using PIC18F4620

Here is a link to download his modified version of the firmware written in MikroC compiler.

Download Firmware


  • source code not working. need help asap pls

  • Hi,
    Great project. Can I ask if it is possible to have this count down in minutes and seconds intervals instead of hours and minutes.

  • hi i want to ask if i use EEPROM.write(0,off(off_hh); and so on for all four values so does it make sense as if when start again so it read this firstthen go on to do the task by resuming. but while doing this i have a problem that i have to save the status on or off too but the insertion of those commands is really difficult for me could u please help me with this well u dont answer any of questions on ur blog u just tell that where i should tell it or else what variables i should not turn =0 so that all the setting never goes off thanks

  • plesi include the block diagram of this project

  • Source not working lcd refuses to initialite,also no fuses settings for burning to Pic.
    Mikropic c and matlab ide with c compiler complains about errors.

    Please give more info about project.


  • Before i make my statement i like very much this project,althou i am having issues since there isn´t any info relevant to the programmer config such as boden ,lp,xt or hs ,mclre and other options.
    Also i simulate this project on isis proteus and the same happens the project don´t even start and appears to have errors,maybe because of pic config options.

    Thanks in advance

  • Just finished designing PCB for this project in Proteus 8.
    If someone one wants to eliminate the crystal oscillator and load capacitors, you have to type
    PCON.OSCF = 1; // Set internal oscillator frequency to 4 MHz
    inside void main and before compiling in mikroc, select edit project, then change oscillator to INTOSC_CLKIN_CLKOUT.
    But when i did this in pic16f628a, the timing was off about 5 seconds per minute… so i recommend using the xtal 4mhz crystal.
    The Proteus schematic diagram for Simulation is given in link below. I also did the PCB design. I will update the design soon for the lcd backlight enabling and adjustment.

  • Is this timer capable of switching a Relay On/Off for 3.5 minute Intervals ??? I am designing-building a Auxilary Sump pump motor(12VDC 10Ampere) control .

    I need to have my Pump Motor On for 3.5 minutes/ Off for 3.5 minutes – in a Total 24 Hour Loop for say 3 days.

    thank you – Stuart Smith

  • Hi.. can you adding thermostat function in your project? thanks. i need thermostat schematics with more function..

  • Sir, how to write this programmed in MPLAB

  • Hello, sir your project is very nice but here i want to set internal memory of hours and minute when i am fist time set the time after when i am start button then start will be previous set.. please help me

  • When implementing Mark Weir’s program, should I make any changes in R-B’s original circuit other than the PIC

  • any chance someone redo the program to count min and sec instead of hours and min, im not a programer allready built it, it works fine thou would like to use it for UV exposure box that i built

  • Does this continuously cycle? That is XX time on, YY time off, then XX time on, YY time off, etc . I would like to build this to control a hot water heater. Off from midnight, then on at 6 or 7AM everyday. I would use a 45A solid state relay controlled by RB3, Also I will use a photo-resistor voltage divider to drive a 2N2222 to automatically adjust the back-light of the LCD (so I can see it in the dark), Thank you for a very nice project.

  • Sir
    Do you have the layout file for will be a great help for my mini project
    thank you

  • sir… this project can we set time for more device?

  • dear sir, i am interested in ur project…
    mind to send me ur coding ??
    my email

  • dear sir its a great job but i need cyclical timer output pls send me the hex file and my mail id is
    tnks in advance

  • Dear sir,

    i’m interest about your timer project.please be kind enough to send me a HEX

    Thanks with regards,

  • hi sir i want to ask a question i want to make it in loop i mean if i set a timing of on off so when they have performed then those settings dont remove instead they remain untill we update the new settings could u pplease help with this updaton in this code that wat i should do to make it work like this thanks please whenever u r free kindly help me

  • do you have the programmer circuit diagram for the PIC.I already have made the timer circuit but I i have no access to its programmer.kindly help me with the schematics.thanks

  • Hi Jack,
    Thanks for sharing your success.
    It is better if you share updated source file with us or please mention the lines where needed to delete or submit only deleted part. I stuck on this project. Mr. RB posted all wrong source and hex file. But his project design is great.


  • dear sir, what is the type of relay that you used in this circuit. i would like to know bcouse im trying to simulate this cct using proteus.

  • Just finished this project and it works great after I figured out how to change the ext to int oscillator. Actually, I knew how to do it but it would not compile because it added some void main junk at the beginning of the file. I don’t know why it’s there but deleting allowed it to compile.

    I think it should be pointed out that the resonator or xtal is probably overkill for most applications and just running off the internal clock simplifies the construction a bit.

    I didn’t check the accuracy but I built this to turn off a battery charger after 12 hours and I am sure it is well within the one minute resolution of the display.

    It should also be noted that you can get 5v USB chargers for 59 cents on Ebay so building the regulator part of the project can also be eliminated. These probably were not available when the project was designed.

    Thanks for the great design,

    Jack Marengo, Il

Leave a Reply

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