LV Switch Timer

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
In my Lighthouse work we came across this problem. Here's the basic issue: how do you keep people from running a pump too long and flooding the bathroom? The pump is run off a 12V solar charged battery system, and controlled by a simple mechanical timer: turn to 30, then back to 5 so it runs for 5 minutes. But there are some who insist if 5 is good, 30 is better, so they set it for 30 and walk away. Flood ensues, no hilarity.

To replace this I design a simple to use timer (see attachments for schematic). The user interface is a single push button, push to start and it times for a predetermined number of minutes. Press again and it stops. A beeper is also there to alert how long the delay is, and when the delay is over.

If you press and hold the button for 10 seconds, it goes into a program mode where you can set the time delay. This is why there is also a beeper in there.

Let's review the design. The expected load is a pump motor using some 10 to 15 amps when running, more when starting. While isolation was not required an inexpensive 40 amp solid state relay was picked to drive this as a drop in pre-tested good solution. Also, should it fail it is connected by screws to it is field repairable.

Here's the block diagram:



A full schematic is in the attachment; it's a little too large to put on here.


Rich (BB code):
BILL OF MATERIALS

Ref Des    Description
------------------------------------------------------------
C1, C3     0.1uF 0805 SMD cap
C2         10uF Tant SMD cap
D1         CGRM4007-G diode
D2, D3     1N4148W diode    
MOV1       ERZ-V07D330 Surge Absorber
J1         6 pin 0.1" space pins
Q1, Q2     BSS123LT1G MOSFET, SOT-23
R1 - R3    20K Resistor, 0805 SMD
R4         25 ohm Resistor (make from 4x 100 ohm 2512)
SP1        273-092 Speaker (Radio Shack)
SW1        GPB556A05BR Switch
U1         PIC12F629T-E/SN Microcontroller, SOIC-8
U2         MIC5236-5.0YM LDO Regulator with enable, SOIC-8
U3         SSR-40_DD 40A Solid State Relay (EBay)
Timing is done by a PIC micro controller. The requirements (small package, internal oscillator) were easily met by the PIC12F629T-E/SN.

To convert the 12V power to 5V for the PIC the MIC5236-5.0YM low dropout regulator was chosen for it's small size, tolerance for a 60V input, and the enable pin.

Normally the regulator U2 is off, kept off by R2 pulling the enable low. This means there is only a tiny current (less then sleeping the PIC) when not being used. When pressed SW1 kicks on the regulator thru R1 & D2. One of the first things the PIC does on power up is drive GP5 high to drive the enable high thru D3. Thus the PIC can turn the power off when the time is complete. Essentially, D2 and D3 form an OR gate so the button can start the regulator, the PIC can keep it on then turn it off when done.

D1 prevents reverse power polarity from taking out everything. It's cheap protection. The external button SW1 is a sealed switch with gold contacts that should survive a bathroom drenching.

SW1 also drives the PIC GP3 pin so the PIC can read the button to stopping or program the unit.

A MOSFET is used as a driver to chirp the speaker. Another MOSFET pulls the drive side of the SS relay low to turn it on.

The unit was made on a small PCB proto boards with 0.1" plated thru holes. a 16 pin SOIC to DIP adaptor was used to hold the two ICs, and most of the components were added to the thru pin side. 0805 SMD parts fit very well on these boards, as do SOT-23 transistor packages. on board wiring was done with Kynar 30 AWG (i.e., wire wrapping wire) as I find it simple to work with. I keep manual wire wrapping tools to strip the ends. Wires going off board were #22 or #24 (junk box wire, don't really know the size) as they need to be more substantial. The large motor current is contained to the SSR so the wiring I add does not need support this current.



The unit will live in a standard workbox. A blank faceplate was obtained and a central hole drilled to hold the button. The box is from a Radio Shack Project Enclosure #270-1801 (3x2x1") was used to contain the control board, and 4 matching holes were also drilled into the faceplate to attach this. Note the metal top from the box made a great template to get these holes in proper position. The back of the box had 2 holes drilled to hold the SSR.

A very observant reader may note I had no 25 ohm resistors on hand so I used 4x 100 ohm units in parallel.


Programming was done under the (free) Microchip XC8 C compiler. Timer 1 is set to trigger an interrupt about every 32 mS. The ISR keeps track of time passing in the Ticks variable. It also debounces the button by verifying the raw switch input is the
same over 2 reading 23 ms apart.

As the Ticks variable is an integer it may be changed by the ISR while being used by the main routine. To prevent this a simple macro is used:

Rich (BB code):
// "thread safe" macro to read current Ticks
#define GetTicks(Dest) while(Dest != Ticks) Dest = Ticks
This will assign the Ticks value to any other variable and test the value is correct. The process gets repeated if Ticks changes while being copied, ensuring an accurate ("thread safe") time reading.

Main loop first initializes the PIC and turns on the power pin. Then the button is observed. A long press puts us into programming mode, short is timing.

A timing cycle begins with a beep for every minute the timer will time for, then the pump is turned on. Each passing minute is also noted with a count of how long much time has passed. Finally, the pump is turned off, the total time is noted, a long lower note is played. The power pin is set low to disable the regulator, and the PIC loops until
it is off.

A program cycle starts with a long high note followed by one short high beep to note it has 1 minute ready to program. Subsequent short presses increment the new time up to the max, then it resets back to 1. A long press will save the value, beep high long to signal success, then beep the new setting counts. A long no-press will abort the programming function, make a long low note. Both methods will then set the power off.

The finished unit is still on my desk till the next tour date when I have access to install this. Later I will report how the unit is received and how it works in the field.

Other views...

Top View:



Side View:




Bottom View showing the SSR:

 

Attachments

MMcLaren

Joined Feb 14, 2010
861
Ernie, I modified your program for a 12F683 and it works great. Nice job. There are just a few typos in the source file that you might want to correct;

() line 12 says "PIC12F519" instead of "PIC12F629"
() line 31 defines your "long press" as 5 seconds, not 10 seconds mentioned in the article
() line 81 comments and article say you're using TMR1 when you're actually using TMR0
() line 83 "pull-ups disabled" comment doesn't match actual bit setting
() line 116, 165, 193, and 194 comments all refer to "seconds" instead of "minutes"

Also, is the assignment in line 50 a typo too? I don't see where 'ButtonIs' is used anywhere in the program.

Rich (BB code):
PRESS_TYPE ButtonIs;
Commenting the line out doesn't seem to have any effect on the program.

The excellent comments make the program very easy to follow, even with those few typos.

Cheerful regards, Mike
 
Last edited:

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
Mike: Thanks for your corrections and kind words. I hope you got some good use from this.

Wow, you looked long and hard at that code to find all my typos!

The project "history" is I used several devices while writing this code. I started from a freebie dev board someone send me off the Microchip boards while my target chip was in the mail... so I had several variations of the source and the chip comment didn't keep track with the code build as the source file had the processor in the name during development.

I got bored waiting 10 seconds to test the programming functions, and 5 seconds is a long enough "eternity" to arbitrate between a short and a long press. A late change and the comment never caught up.

Same with the timer number, think I was originally using Timer1 but it wasn't available at some point so I switched over to Timer0, an awful device but serviceable enough for this usage.

Also, towards the end I started to rewrite the READ_BUTTON() routine (why did I use all caps? Ugh!) to use that ButtonIs variable, but it went backwards too much so I reverted to the original (and working) scheme I already had.
 

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
UPDATE: The device did not work when initially tested. Hooked it up, pressed the button, the LED on the solid state switch lit, but no pump operation. While reaching for my voltmeter the pump turned on... and stayed on while the solid state switch proceed to get very hot.

I removed the device and took it back home (remember, I'm using this in the bottom of a lighthouse). I had no good reason for the failure so I replaced the solid state switch with a good old relay (Radio Shack), and changed the FET to a transistor as the relay current was just about the max Idd of the fet. Second try was a champ, and it worked to rave reviews for a few parties.

In retrospect, I believe when hooking up the pump, which is correctly color coded to NEC wiring standards (black hot, red switched load) I reversed the connections thinking "electronics" and not "electricity" color coding. However, the relay is probably the better choice anyway as these cheapie solid state relays have a rather large voltage drop.

Also in retrospect I realized I need configure the device differently, as now it is part of the "house" wiring, and thus should be NEC compliant. As I'm not UL 508 certified (anyone got a copy of that spec?) it should not be a permanent part of the wiring. I believe I am compliant if I add a compliant outlet and make my switch a stand alone unit that plugs into the system. At least that way if an inspector down checks it we just unplug it until he goes away <grin>.

Hurricane Sandy did a number on our little lighthouse flooding the lower areas with about 3 feet of sale water for a week till it could be pumped out. This coming Saturday is the dreaded work crew day when we clean out the muck and I will be able to see, but I think the box was under water. I have heard the toilet itself is now toast, with flooring floated away and the marine toilet (with several pumps and timers inside it) also soaked.

Our floating dock also floated away, but it washed ashore and can be repaired. The walkway stayed behind high on the rip rap.

However, our damage was nothing compared to some.

Remains of Old Orchard Shoal, NY lighthouse in 2012 after Hurricane Sandy.
Photograph courtesy U.S. Coast Guard Northeast.
(NOT MY LIGHTHOUSE thank you!)

 

MMcLaren

Joined Feb 14, 2010
861
Mike: Thanks for your corrections and kind words. I hope you got some good use from this.
You're welcome, and thank you for posting a project with such a nice unique user interface. I couldn't quite imagine how well it would work after looking at your code and that's why I decided to give it a try. Once I experienced the interface first hand, I was amazed that it had such a natural and intuitive feel. Very nicely done!

May I ask if you're using the "free" version of the XC8 compiler for this program? That's what I just installed in my MPLAB 8.84 environment in order to test your program, but, the compiler output is just awful. It's using 928 out of 1024 words of 12F629 program memory for your modest little program. The same program, modified for the "free" version of the BoostC compiler, is about 45% smaller (~500 words).

Cheerful regards, Mike
 
Last edited:

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
Mike: Yep, I'm using the freebie crippled XC8 compiler that tells me on every build:
Running this compiler in PRO mode, with Omniscient Code Generation enabled,
produces code which is typically 40% smaller than in Free mode.
The MPLAB XC8 PRO compiler output for this code could be 399 words smaller.
I'm only slightly curious as to where that memory gets used... but as in low quantities PIC ROM is by far cheaper then PIC Compilers so it's an acceptable trade off for me.
 
Top