Pages

Saturday, April 12, 2014

Simple timer with PIC16F628A

This is a quick project for a timer. Recently I finished my UV light exposure box and thought that it will be convenient to have a build in timer to switch off the light after preset time.
So I had a PIC16F628A lying around and after searching the web I found a Brazilian site (I think?) with tons of interesting projects with microcontrolers. This project is based on one of them.

This is the schematic of the timer:


The schematic uses the internal oscillator of the microcontroller which is enough accurate for my purposes, but as the pins 15 and 16 are left unoccupied, there can be connected external quartz resonator with better accuracy.
As I said, this project is based on an existing project, but actually my schematic is quite different and the code was almost completely rewritten. My programming abilities are little rusty, but I think the final result is quite good.

There are three buttons to operate the timer: "START/STOP", "MIN" and "SEC".
"START/STOP" is for starting and pausing the timer.
"MIN" is for adjusting the minutes. Minutes may go up to 99 and then starts again from 0.
"SEC" is for adjusting the seconds. Seconds goes up to 59 and then starts from 0.
"MIN" and "SEC" have repeat functionality.
Pressing MIN and SEC buttons simultaneously will reset the timer. 
When the timer reach 00:00, the buzzer sounds 3 short and 1 long beeps and the LED lights up.
The buzzer is electromagnetic type. 

After this any of these three buttons will reset the status and LED will switch off.
When timer is counting down - RB7 (pin 13) is high and when the timer is stopped - RB7 is low.

With this pin we can operate some external circuitry. In my case there will be connected a transistor which will switch on and off the UV exposure box.

Jumper J1 is for calibrating of the timer. When shorted, the timer enter in adjusting mode. With MIN and SEC buttons we can increase/decrease the value of an internal parameter thus slowing down or speeding up the timer. This value is stored in the EEPROM.
Pressing START/STOP button when in this mode will reset this parameter to its default value.

I tested the schematic on the breadboard and everything works as described.
The code is written and compiled with mikroC PRO for PIC. The options for the project are:
Oscillator: INTOSC oscillator: I/O function...
Oscillator frequency: 4.000000 MHz
Watchdog Timer: disabled
Power-up Timer: enabled
RA5/MCLR/VPP pin function: disabled
Brown-out detect: enabled
The .hex file was programmed in the microcontroller with my clone of PICkit2 using "PICkit 2 programmer" v.2.61

Link for downloading the archive with schematic, source .c file and .hex file : Timer
Use them on your own responsibility!

Some photos of the finished board:




And the schematic (same as above with added switching transistor and the buttons are different type):



How it works:


Update: June 12, 2014
Because, one of my reader ask me to publish the files of the actual device (second schematic), here the link for downloading them: Timer (PIC16F628A)(2).rar
Inside are the schematic, PCB, partlist, source file and .hex file.
Use them on your own responsibility!
Notes: the LCD display is connected via  male - female pin headers. I prefer to solder the 16-pin female  header on the PCB and the male pin header to the display board, but it can be done otherwise.
If there are used standard pin headers, the distance between display board and PCB is around 10mm, so there must be used low profile capacitors for C1 and C2 or they can be bend to fit under the display.

Update: February 25, 2016
There is update of the software - the changes are:
slightly different interface, the LED will light when the timer is counting. To enter in the adjustment mode it is needed only brief shorting of RA0 to the ground. pressing the START/STOP button will end the adjustment mode.
The link: Timer (PIC16F628A)
In the archive are only .C and .HEX files. The schematic and the PCB are the same and can be found in the previous update.

Update: May 1, 2016 
One of my readers, Alnoor Ratansi send me a modified code for the timer with added ability to count  up to 99 hours 59 min 59 sec. The software uses one additional button attached to pin 16 for adjusting hours. Thank you, Alnoor!
I haven't tried the code, so if anyone try the code, let me know if it work properly. Here you can download it: PicTimer

74 comments:

  1. I found it interesting is the project for use at my home,
    I like to know that sotware was used to be Compiled?
    and thank you very much for sharing...!

    ReplyDelete
    Replies
    1. It was compiled with mikroC pro, but I suppose the code can be modified easily for MPLAB and Microchip XC8 compiler.
      In mikroC pro there is a build-in library for LCD displays and I am not sure is there a similar library in MPLAB compilers.

      Delete
  2. I need to modify the code so that the red LED goes off when the counter reaches zero, rather than this on
    and i do not know how to do.
    please help me..! thanks

    Greetings from Bolivia.

    ReplyDelete
    Replies
    1. You can do this without modifying the code. Just connect a second LED in parallel with the output. Thats way the LED will light up when the output transistor is switched on. You must add a current limiting resistor in series with the LED - 1k or 1.8k will be fine.

      Delete
  3. Thank you sir for this helpful and wonderful proJect

    ReplyDelete
  4. Gracias lo Construi y funciona de maravilla

    ReplyDelete
  5. I would like change de messages to spanish lenguage, but when I compile, give me many errors. Can you help me?

    Thanks

    ReplyDelete
    Replies
    1. Which program do you use to compile the code?

      Delete
    2. The code is designed and compiled with Mikroelektronika MicroC Pro for PIC.
      You should use the same or you have to change the code to be compatible with your program of choice.

      Delete
  6. A question what method you use to did your pcb?
    Its a nice project man, thanks for sharing!

    ReplyDelete
    Replies
    1. I use photoresist method. There is a ton of videos on Youtube with instructions. Here's one excellent: https://www.youtube.com/watch?v=p2kFazl-aEE

      Delete
  7. For me,simulation in Proteus works perfectly but in real world, code never runs. What are configuration bits 0 and 1 in Pickit? Thank you!

    ReplyDelete
  8. I found the error! Pin 5 of LCD was disconnected from GND. Thank you for sharing this project.

    ReplyDelete
  9. Has anybody modified the code to add hours ?

    ReplyDelete
    Replies
    1. I have modified the code to add hours. how do I upload the code

      Delete
    2. Hi, you can send me the source code and the .HEX file to my email and I will upload them here. The email is shown at my "About" page.

      Delete
  10. Dear sir,

    shall we on this timer for 10 minutes and off repeating the process is available ?

    ReplyDelete
    Replies
    1. You mean the timer to switch ON for 10 min, then switch OFF for 10 min and repeat this?
      Of course it is possible if you find someone to write the code.

      Delete
    2. Bonjour,
      Oui, j'ai modifié le programme pour cela.
      mickvarenne@free.fr

      Delete
  11. Is it possible to modify code so that last time period is stored in eeprom, for example I need to use the timer for multiple time periods of the same time eg 25 seconds and to save time re entering the 25 secs just depress the start button again.If a different time period is required hold one of the buttons in for a preset time to erase the last time period then enter new time period and then this new period is written to eeprom.

    Many thanks.

    ReplyDelete
    Replies
    1. Bonjour,
      J'ai modifié le programme pour garder en mémoire le dernier temps utilsé.
      mickvarenne@free.fr pour le post

      Delete
  12. Hello
    Modify the project by adding a button to decrease the minutes, also add a button to "reset" enabling the pin RA5 and applying in this "power on reset", I think that with this always reset the memory to the assigned value (977) Jumper and the function of pressing min-seg simultaneously was disabled, the problem is that when compil, sends many errors, as like I am new with C, could someone revise the code? Could also send the file in proteus for simulation, compile from micro c pro...... Regards

    ReplyDelete
  13. Hi, great job. Can you please modify the code to write to EEPROM the last selected time?

    ReplyDelete
    Replies
    1. Bonjour,
      Vous voulez dire la dernière valeur programmée?

      Delete
    2. Bonjour,
      J'ai modifié le programme, je peux vous le poster
      mickvarenne@free.fr

      Delete
    3. Hello!
      See my blog "electroniquePIC.blogspot.fr"
      Thank you
      Greetings

      Delete
  14. Great job my friend ! Clean and beautiful project. Sadly, some download links are broken. Can you fix it ? Thanks.

    ReplyDelete
  15. The 99 hours model doesn't have the downloand link avaible anymore.Please do something.Thank you

    ReplyDelete
    Replies
    1. Hello!
      See my blog "electroniquePIC.blogspot.fr"
      Thank you
      Greetings

      Delete
  16. Seems like none of the download links are working?What's wrong with them?

    ReplyDelete
    Replies
    1. Hi,i would like very much hours version
      My email is barga_2003@yahoo.com and specify that you with this matter because i receive a lot of spams.Thanks

      Delete
  17. Replies
    1. Hello.
      Can you e-mail to me your code (99 minutes)?
      My e-mail: mr-isaac@bol.com.br.
      Thanks.

      Delete
  18. Hi, Love the project. Could you please mail it for me to za.gene at gmail dot com

    ReplyDelete
  19. hi

    good works please send sch and hex file ubimes@gmail.com thanks

    ReplyDelete
  20. Hello i have the follow problem
    after programing the pic i See only finished on the display and 00:00
    set time min and seconds is no problem
    only start stop does noting i have check the 5v on pin18
    and it is there also i have checked the button and it is ok
    please have you any id what is wrong

    ReplyDelete
  21. Hi, Could you please repost your drop box links, In particular the code with ability to set/adjust hrs.
    Many thanks Jesse

    ReplyDelete
  22. HI, Not sure if appropriate but I've changed this project to add hrs and backlight output and capability to to turn timer off if lamp(or other device) overheats. Pinout is different from posted diagram but info is in timer.c file. Thanks Jesse

    https://drive.google.com/file/d/15EEKHrzqEp3KjhXEJfiWoxHeulakHLkN/view?usp=sharing

    ReplyDelete
    Replies
    1. if you can i need the first one or V1 source code if you can pls send me i can not get from the first one link yemane188@gmail.com

      Delete
  23. Great job my friend ! Clean and beautiful project. Sadly, some download links are broken. Can you fix it ? i need source to learn...i'm newbie....Thanks.

    want try this :
    https://www.se.com/id/id/product-range-presentation/529-zelio-timer-relays/

    ReplyDelete
  24. The hex does not seem to work. I programmed using both winic800 and willem. programmer is a willem pcb50b

    ReplyDelete
    Replies
    1. In the schematic pin4 (RA5/~MCLR/VPP) is not connected so in the software used to burn the program you must set this pin to be IO pin and disable MCLR functionality or else the microcontroller will contiguously reset. Another solution is to solder a small 10k resistor between this pin and VDD (+5V).

      Delete
  25. Paddy you can try my version that does work, https://drive.google.com/file/d/15EEKHrzqEp3KjhXEJfiWoxHeulakHLkN/view?usp=drivesdk

    ReplyDelete
    Replies
    1. Seems that your links work. However, please add a diagram. Thanks.

      Delete
    2. Hi,
      In the updates at the bottom of the article there are three links. the first of them have the schematic and the PCB.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Христо (August 14, 2020 at 2:28 AM) << Thank you for your response. I got your diagram and photos. May I get your C source and HEX file? I shall program with MikroC for PIC or MPLAB X. I want to use your timer for the Geyser (water heater). I need only 99 minutes and seconds. Prof. A.H.M. Asadul Huq, Dhaka University, Bangladesh.

      Delete
  26. Your timer project is very interesting. But currently your Timer related links are not working. Please give me pointer to a link, where i can get the timer source and Hex file. Thanks. Prof. A.H.M. Asadul Huq, Dhaka University, Bangladesh.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. @Goahead << Seems that your 99 hour timer works now in my Proteus simulation (I had to change the push button active time to 500 ms in simulation). I want to start the timer once it gets power, automatically, without applying any trigger on the start button. Would you please tell me where should I modify the code or the circuit? Thanks in advance.

    ReplyDelete
    Replies
    1. Hi,
      You can add these lines just before the "while" loop:

      start = 1;
      LED = 0; // to light the LED
      OUT = 1; // to switch the output high
      seconds = 0;
      minutes = 0;
      hours = 0;
      cnt = 0;

      You can remove the duplicating lines from the beginning of the "main" procedure.
      Hope this will work for you. This program was written very long time ago and it shows :(
      I wouldn't write it like that now.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. So the timer starts and then stops because the hours, minutes and seconds are 0. So in order to work as you want you should read from EEPROM these values before the main loop. Also you should store these values in the EEPROM after changing them.
      Now in the EEPROM, the first byte is flag that there was written the OneSecond value. The next 2 bytes are the value of OneSecond. You can make the same arrangement for the time values - the forth byte to be flag that there was written "time" and next 3 bytes will hold the time values. And then you can read the flag and if the flag is 1 you read time values and start the timer. If the flag is different then you don't start the timer.
      I don't have time to write the code, so you should do it yourself.

      Delete
    5. Thanks a lot for your idea. I shall try to implement your idea.

      Delete
  29. Sir can I add hour with this program

    ReplyDelete
    Replies
    1. Of course you can. There are unused pins, so you can add a button for the hours. And you should modify the code accordingly.

      Delete
    2. If you want to change the code only to display the hour:min:sec you can do that too.

      Delete
    3. This program gives output on LCD like

      One second; 0972
      RES - +

      I don't know why it is come

      Delete
  30. This program gives output on LCD like

    One second; 0972
    RES - +

    I don't know why it is come

    ReplyDelete
    Replies
    1. Your timer is in adjustment mode. It enter this mode when jumper J1 is connected. To run in normal mode you should disconnect the jumper (remove the jumper cap).

      Delete
  31. Sir I use hour showing .hex file on my simulation . Successfull it runs but
    Hour is automatically increase where I add button please guide me sir

    ReplyDelete
  32. Thanks for the circuit. I've started doing the PCB layout. Just one question regarding the internal oscillator, if using an external crystal would I have to modify this line 'Oscillator: INTOSC oscillator: I/O function...'? Thanks, Dave

    ReplyDelete
    Replies
    1. Yes, you should modify this line, so the microcontroller can switch to external crystal clock source. If you put 4MHz crystal, the timer should work without additional changes, but if you use higher frequency crystal, then you have to recalculate some variables in the code.

      Delete
  33. Do you happen to have the gerbers for you pc board for your count down timer and are they available to us?

    ReplyDelete
  34. Does this timer go down to 1 second? I gather that the lowest time is 59 seconds. I am looking for one that will go down to say 10 seconds (or 1 second) and also drive a relay to turn on/off another device.

    ReplyDelete
    Replies
    1. Yes, it can be set with 1 second accuracy. The lowest time is 1 second, not 59 seconds. And about the gerber files - the project was created with Eagle many, many ears ago and since then I move on to other platforms - KiCad and EasyEDA, so I don't know if I can unearth this old project and convert it and then produce gerber files. The PCB is very simple, so it is not difficult to create it from scratch.

      Delete
  35. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I asked for gerbers because that is usually what is offered and needed to make the boards. If you have the Eagle project files that is even better. Assuming you are willing to share them. Whether it is Eagle, KiCad, or EasyEda does not matter.

      You might consider sharing the gerbers on PCBWay in their "shared projects" area as I understand the author (you) get a royalty if someone orders the boards from them. Certainly not the same as selling boards yourself. One thing I found disappointing with their shared projects is that most do not link back to where build information may be found (this web page for your project).

      Thank you.

      Delete