# Timer Control Circuit using Micro-Controller

#### soneill2

Joined May 3, 2016
6
Hi All,

New to this forum but am looking for some help on a project i am working on. Must say there are some great threads on here. I will be an expert yet
I am not new to electronics but can no means say I am a professional.

Basically I want to be able to do some life cycle testing on motors. So direct connection to motors, also i want to be able to connect the same circuit to a relay to trigger an electric actuator. Involved in this would be different cycle times so for example say:

Example 1.
On 10 secs, off 60 secs,
On 10 secs, off 60 secs,
On 10 secs, off 60 secs,
On 10 secs, off 60 secs,
On 10 secs, off 3600 secs, During this I would also like to be able to count the cycles that the motor runs for

Example 2.
On 75 secs, Off 30 secs, On 15 secs, off 1800 secs, Again like to be able to count cycles.

As you can see it has to be programmable depending on what is require.

Some Equipment I already have at hand are a power supply, Pic K150 programmer, Various chips (PIC12F683-I/P, PIC16F72-I/SP, PIC18F4550-I/P).
I am not limited on budget as i want a fairly robust system that Is user friendly after being initially set up, What would be the best method to achieve this?

Thanks.

#### R!f@@

Joined Apr 2, 2009
9,861
Welcome to AAC.

I believe a PIC can be programmed fairly easily for that.

Not sure about what you mean by counting cycles.
An LCD to display count cycles will be easy to implement though.
You need a PIC with 6 port for LCD and some extra for the cycle triggering.

#### dannyf

Joined Sep 13, 2015
2,197
it has to be programmable
It is fairly easy to do, but heavily dependent on what you meant by "programmable".

In the simplest (also the most complex, depending on your perspective) way, you can code some delay routines and write a piece of code specific to your task and get a mcu programmed for that task;

In the most complex (also the simplest) way, you can get a plc and code it either on a user interface or through a computer;

Then I'm sure that there are tailored programs somewhere in between. Again, it all depends on what you meant by "programmable".

#### jpanhalt

Joined Jan 18, 2008
11,087
Yes, it can be done with a PIC or any other microcontroller. Just considering a PIC, there are three (at least) design options that need to be considered up front:

1) How are you going to get a time of >3600 seconds? If you consider TMR1, which is 16-bit, and clock it at say 32.768 kHz (a common clock frequency) that will only give you about 16 seconds per rollover. Of course, you could count rollovers to extend that. You could also clock it at another frequency, but from where? You have 50 or 60 Hz available. You might also consider a separate module called a "real time clock" (RTC). RTC modules are cheap (often <$2) and will give various time outputs from 1 Hz up to 32.768 kHz. You might also read the module, but that might be more complicated than you want. 2) As you set the various test conditions, do you want them set by the user without reprogramming the chip, or do you want to reprogram the chip for each different test. The latter may be less complicated in terms of the program, but could be inconvenient. 3) (edit) Considering that you will probably have a keypad or other user entry device and an LCD display, you may want to consider a chip with more pins to make life easier. John Last edited: Thread Starter #### soneill2 Joined May 3, 2016 6 Welcome to AAC. I believe a PIC can be programmed fairly easily for that. Not sure about what you mean by counting cycles. An LCD to display count cycles will be easy to implement though. You need a PIC with 6 port for LCD and some extra for the cycle triggering. Thanks for the quick reply, My idea of counting cycles is say for example when it completes one cycle, as in on for 10sec, Off 30secs, that counts it as one cycle. You say easy to implement, as in connect to the circuit, but programming it how easy would it be? At the moment iv 3 PICs (PIC12F683-I/P, PIC16F72-I/SP, PIC18F4550-I/P). One has 40 pins, i presume this would be the easily to program? Thanks Thread Starter #### soneill2 Joined May 3, 2016 6 It is fairly easy to do, but heavily dependent on what you meant by "programmable". In the simplest (also the most complex, depending on your perspective) way, you can code some delay routines and write a piece of code specific to your task and get a mcu programmed for that task; In the most complex (also the simplest) way, you can get a plc and code it either on a user interface or through a computer; Then I'm sure that there are tailored programs somewhere in between. Again, it all depends on what you meant by "programmable". Hi Thanks for the reply. Honestly I would like the most simple way to do it, PLC at the moment is out of the question, unfortunately as i would be pretty good with ladder logic, i could have different chips for different programs? #### dannyf Joined Sep 13, 2015 2,197 unfortunately as i would be pretty good with ladder logic, i could have different chips for different programs? Check out Ladder logic for pic/avr, a free ladder logic implementation on a few pic/avr chips. #### elec_mech Joined Nov 12, 2008 1,500 Welcome to AAC. Have you programmed a microcontroller before? If yes, what language are you most comfortable with: Assembly, BASIC, or C? If you've never programmed before, I'd strongly suggest using BASIC. This is only my opinion, but it is much easier to learn and use than the other languages. If you're set on using a PIC, you can use a free version of PicBasicPro from melabs (student version). Of the PICs you have, only the 12F683 will work with the free version. You'll probably want something with more pins though. You'll also need to set up MPLAB to work with PicBasicPro and the K150. In the minute I researched your programmer, the reviews were poor at best. I hate to tell you to toss it, but based on the reviews, you'll have a lot less headaches if you go with something else. Consider a PIC Kit 3 or similar. Alternately, if you really want to keep things simple, I'd suggest using a PICAXE. These are PIC microcontrollers with a bootloader on them that work with a free BASIC compiler. You might spend a buck more on a PICAXE chip than its equivalent PIC, but you'll save lots of setup time and there is another forum dedicated to programming these chips - though I can help you if you go this route. Alright, next is user interface. Below is a couple of ways to approach it, but by no means the only ways. The top half uses BCD rotary thumbwheel switches to clearly set the ON and OFF times. The four digit seven-segment display counts the cycles. You have a start and stop button and a cycle reset button. The only caveat to all this is dealing with things like the user changing the ON and OFF times while the system is running - do you allow the change during operation or ignore it? If you ignore it, how do you know what the actual ON and OFF times are? Just things to think about. The bottom half is minimalist. An LCD display and a rotary encoder wheel. The wheel can spin 360 degrees and has a button. You can then control and prevent the ON and OFF times from being changed during operation and still count and display cycles. I show a 2 line LCD, but you'll probably want a 4 line. This way you can show ON & OFF time and cycle count (more) easily. You can of course combine elements of the two, for instance, add a start and stop button to the LCD and wheel option or make up something completely different. This is all of the five minutes of thought I put into this. Then we move onto electronics. You could program a PIC to do the timing for you and with enough effort and math you'll get the timing just right. However, I'm lazy and if you go with a PICAXE the last thing you want to rely on is accurate timing (from the microcontroller). Instead, as suggested previously, use a real time clock (RTC). You can use these to provide a fairly accurate 1 Hz clock pulse which is all you need here. However, these also provide time and date, so if you're feeling ambitious later, you could add this to the display when not in use. Or go the next step and add memory to store the ON/OFF times, cycle counts, and times and dates of each test and store it to an SD card or USB flash drive. As you might infer, I'll add more and more features until it breaks, so start small then work your way up. If you opt for the PICAXE route, I can put a design and some software together for you. Thread Starter #### soneill2 Joined May 3, 2016 6 Welcome to AAC. Have you programmed a microcontroller before? If yes, what language are you most comfortable with: Assembly, BASIC, or C? If you've never programmed before, I'd strongly suggest using BASIC. This is only my opinion, but it is much easier to learn and use than the other languages. If you're set on using a PIC, you can use a free version of PicBasicPro from melabs (student version). Of the PICs you have, only the 12F683 will work with the free version. You'll probably want something with more pins though. You'll also need to set up MPLAB to work with PicBasicPro and the K150. In the minute I researched your programmer, the reviews were poor at best. I hate to tell you to toss it, but based on the reviews, you'll have a lot less headaches if you go with something else. Consider a PIC Kit 3 or similar. Alternately, if you really want to keep things simple, I'd suggest using a PICAXE. These are PIC microcontrollers with a bootloader on them that work with a free BASIC compiler. You might spend a buck more on a PICAXE chip than its equivalent PIC, but you'll save lots of setup time and there is another forum dedicated to programming these chips - though I can help you if you go this route. Alright, next is user interface. Below is a couple of ways to approach it, but by no means the only ways. View attachment 105403 The top half uses BCD rotary thumbwheel switches to clearly set the ON and OFF times. The four digit seven-segment display counts the cycles. You have a start and stop button and a cycle reset button. The only caveat to all this is dealing with things like the user changing the ON and OFF times while the system is running - do you allow the change during operation or ignore it? If you ignore it, how do you know what the actual ON and OFF times are? Just things to think about. The bottom half is minimalist. An LCD display and a rotary encoder wheel. The wheel can spin 360 degrees and has a button. You can then control and prevent the ON and OFF times from being changed during operation and still count and display cycles. I show a 2 line LCD, but you'll probably want a 4 line. This way you can show ON & OFF time and cycle count (more) easily. You can of course combine elements of the two, for instance, add a start and stop button to the LCD and wheel option or make up something completely different. This is all of the five minutes of thought I put into this. Then we move onto electronics. You could program a PIC to do the timing for you and with enough effort and math you'll get the timing just right. However, I'm lazy and if you go with a PICAXE the last thing you want to rely on is accurate timing (from the microcontroller). Instead, as suggested previously, use a real time clock (RTC). You can use these to provide a fairly accurate 1 Hz clock pulse which is all you need here. However, these also provide time and date, so if you're feeling ambitious later, you could add this to the display when not in use. Or go the next step and add memory to store the ON/OFF times, cycle counts, and times and dates of each test and store it to an SD card or USB flash drive. As you might infer, I'll add more and more features until it breaks, so start small then work your way up. If you opt for the PICAXE route, I can put a design and some software together for you. Thanks for the reply, This is great, its very detailed description of all the different possibilities. I have however decided to test out the route of Ladder logic for pic/avr. the reason is i am quite familiar with this type of programming on PLC's. I have not used it on PICs yet tho.. But surely it cant be that much different, just set the pins correct and should be good to go? I know the K150 is a pretty cheap piece of equipment but surely its worth a go seen as i already have it here. Im waiting at the moment for a few more parts to arrive and will build a little circuit to see can i get a Led to operate on off for different lenghts of time. then move onto the bigger prokect i have linned up. A side note, what would a PLC on average cost? Possible siemens or is there any cheaper unbranded versions? #### elec_mech Joined Nov 12, 2008 1,500 I have however decided to test out the route of Ladder logic for pic/avr. Did you find software to do this? If so, can you post a link? But surely it cant be that much different, just set the pins correct and should be good to go? I'm vaguely familiar with ladder logic, but I've never used nor programmed a PLC myself. Conceptually it's very similar to a microcontroller (uC), but there are some differences in how they execute. This . . . I know the K150 is a pretty cheap piece of equipment but surely its worth a go seen as i already have it here. . . . is in contradiction with this. I am not limited on budget Certainly there's no harm in trying, BUT, if you've never programmed a microcontroller before, you're going to run into plenty of gotcha's - the absolute LAST thing you want to do is use a questionable programmer. You could attempt to load a program 20 times and see nothing happen which will have you question your ability, then your sanity. It's one thing if the code is off, you learn - it's quite another if the programmer is in question. As you haven't programmed a uC before, you won't know whether it's you or the programmer. Hence, I strongly suggest getting something more reliable. A PIC Kit 3 is under$50, a knockoff on eBay is under $12. However, if you go with a PICAXE, you can get away with an inverted serial to USB converter. Im waiting at the moment for a few more parts to arrive and will build a little circuit to see can i get a Led to operate on off for different lenghts of time. then move onto the bigger prokect i have linned up. Excellent approach, start small, one task at a time, then work your way up. If you post a schematic, we can critique it and help you avoid problems. A side note, what would a PLC on average cost? Possible siemens or is there any cheaper unbranded versions? Again, I'm not overly familiar with PLCs. I know they're excellent in factory environments and extremely robust. Compared to a uC, they are expensive though. If you were to do this with PLCs (including buying PLC peripherals like displays) I imagine you'll spend close to a grand. Using a uC and bare parts, you'll probably be somewhere between$150-$300 ballpark. #### neonstrobe Joined May 15, 2009 182 Did you find software to do this? If so, can you post a link? I'm vaguely familiar with ladder logic, but I've never used nor programmed a PLC myself. Conceptually it's very similar to a microcontroller (uC), but there are some differences in how they execute. This . . . . . . is in contradiction with this. Certainly there's no harm in trying, BUT, if you've never programmed a microcontroller before, you're going to run into plenty of gotcha's - the absolute LAST thing you want to do is use a questionable programmer. You could attempt to load a program 20 times and see nothing happen which will have you question your ability, then your sanity. It's one thing if the code is off, you learn - it's quite another if the programmer is in question. As you haven't programmed a uC before, you won't know whether it's you or the programmer. Hence, I strongly suggest getting something more reliable. A PIC Kit 3 is under$50, a knockoff on eBay is under $12. However, if you go with a PICAXE, you can get away with an inverted serial to USB converter. Excellent approach, start small, one task at a time, then work your way up. If you post a schematic, we can critique it and help you avoid problems. Again, I'm not overly familiar with PLCs. I know they're excellent in factory environments and extremely robust. Compared to a uC, they are expensive though. If you were to do this with PLCs (including buying PLC peripherals like displays) I imagine you'll spend close to a grand. Using a uC and bare parts, you'll probably be somewhere between$150-$300 ballpark. #### neonstrobe Joined May 15, 2009 182 Hi I'd recommend an Arduino processor. These come on a PCB with a USB connection to plug into your PC. Programming language is a basic-like C, not too difficult, there are many example programs available. TO control a motor you would need to add a motor switch to the unit. You could do this using a breadbooard or even a plug-in type, but most plug-in boards are expecting 0.6mm pins for components, not the usual 1mm pins on the Arduino boards. BUt they may fit with a firm but careful push, assuming you don't want to make a dedicated PCB to hold the extras. The motor switch may need to use an opto coupler if the motor is on the mains, so this would need an isolated bit to amplify the photosignal as well, which would have to be operated from the mains supply e.g. a separate 5V supply or something like that which can have one side connected to whatever actually turns the motor on, which I suspect would be a triac. Thread Starter #### soneill2 Joined May 3, 2016 6 Did you find software to do this? If so, can you post a link? I went with using the LDmicro software, free to download, again tho i works perfect on the pc but might be a different story when I try to run it on the PIC I'm vaguely familiar with ladder logic, but I've never used nor programmed a PLC myself. Conceptually it's very similar to a microcontroller (uC), but there are some differences in how they execute. This . . . . . . is in contradiction with this. Certainly there's no harm in trying, BUT, if you've never programmed a microcontroller before, you're going to run into plenty of gotcha's - the absolute LAST thing you want to do is use a questionable programmer. You could attempt to load a program 20 times and see nothing happen which will have you question your ability, then your sanity. It's one thing if the code is off, you learn - it's quite another if the programmer is in question. As you haven't programmed a uC before, you won't know whether it's you or the programmer. Hence, I strongly suggest getting something more reliable. A PIC Kit 3 is under$50, a knockoff on eBay is under $12. However, if you go with a PICAXE, you can get away with an inverted serial to USB converter. Very true, but sure iv noting to lose at the moment so worth a good see can i get some results and get more familiar with the process of programming PIC's. I may yet buy some of these pieces of equipment Excellent approach, start small, one task at a time, then work your way up. If you post a schematic, we can critique it and help you avoid problems. Iv taken this from another project someone else has completed, its more or less just a trial again to see can i get the PIC running. Again, I'm not overly familiar with PLCs. I know they're excellent in factory environments and extremely robust. Compared to a uC, they are expensive though. If you were to do this with PLCs (including buying PLC peripherals like displays) I imagine you'll spend close to a grand. Using a uC and bare parts, you'll probably be somewhere between$150-\$300 ballpark.
PLC's are truly amazing pieces of equipment, but for my objectives I wouldnt need all the displays etc,m Just if i can track on PC that would do just fine

#### soneill2

Joined May 3, 2016
6
Hi
I'd recommend an Arduino processor. These come on a PCB with a USB connection to plug into your PC. Programming language is a basic-like C, not too difficult, there are many example programs available.