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

38 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. will the code work on PIC16F628 instead of PIC16F628A?

    ReplyDelete
  13. 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
  14. 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
  15. Great job my friend ! Clean and beautiful project. Sadly, some download links are broken. Can you fix it ? Thanks.

    ReplyDelete
  16. 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
  17. 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
  18. Replies
    1. Hello.
      Can you e-mail to me your code (99 minutes)?
      My e-mail: mr-isaac@bol.com.br.
      Thanks.

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

    ReplyDelete