Bistable relay code

Krolroger

New member
Hello there,

Complete beginner here.

I'm trying to flip a bistable relay with set and reset 5VDC coils from a momentary button push switch. The code I have put together seems to work to the extent that I can alternately turn on a couple of LEDs (instead of a Darlington driver and relay), but instead of briefly turning on, they remain illuminated until the switch is again pressed.

Curiously, when I run the simulator, it seems to function correctly with the outputs going high only when the input goes low.

My momentary switch has a debounce network ( 0,47uF cap, 560R and 10K resistors) series-paralleled across it and brings pin 3 low when pressed. The Picaxe 08-M2 controller is mounted on a suitable proto board.

I'd be grateful for any help.

Regards,
 

Attachments

AllyCat

Senior Member
Hi,

Welcome to the forum. First, it's generally better to post program code directly into the thread, preferably within [code ] .. [/code] tags. So far, it looks as if 17 members have viewed the post, but only 2 have bothered to open the .PDF ! Then it needs quite a lot of work to transfer the .PDF text into the Program Editor. :(

To me, it appears that the Simulation is NOT correct. If the button is NOT down (i.e. Pin 3 is High) then the program loops continuously, generating pulses on either Pin 1 or Pin 2. But the program structure is quite "confusing" (unnecessarily complex, with too many GOTOs) , which may hide what is actually happening. In particular the program is not being "held" when the input pin is High (= 1).

The first IF pin3 ..... could be written as a single line IF pin3 = down then {GOTO} switchdown (there is no need for the ENDIF in this format), but then the BRANCH instruction is doing more than might be expected: not only is it selecting one of the two coils, but IF the counter has a value of 2 then it "falls through" to reset the value to zero. Generally, it is better practice to ensure that a counter generates the "correct" values when it is incremented, not to "patch" it in another part of the program. ;)

Here's a "simpler" version of the program which I think should work. It shouldn't need a debounce circuit because it doesn't (need to) poll the button after its first contact, until after the button has been released. If the button might bounce on release, then a PAUSE could be added at the top of the first DO ... loop. There's absolutely nothing "Wrong" with using the BRANCH instruction (it's actually one of my "favourite" instructions) but the IF THEN... structure is more conventional in this type of application.
Code:
#picaxe 08m2
symbol down = 0
symbol counter = b0

do
    do
    loop while pin3 <> down            ; Wait for press of button
    counter = counter + 1 AND 1            ; Increment and wrap 2 back to 0
    if counter = 1 then
        high 1                          ' switch on output 1
        pause 250                       ' wait 250 ms
        low 1                            ' switch off output 1
    else
        high 2                             ' switch on output 2
        pause 250
        low 2
    endif
     do
    loop while pin3 = down            ; Wait for release of button
loop
Cheers, Alan.
 
Last edited:

Krolroger

New member
Hi Alan,

Thank you very much for this; it's very elegant. I'll go through it and try to thoroughly understand what's going on.

Regards, Simon
 

Krolroger

New member
Hi Alan,

This works very well and flips the signal relay with a 100ms pulse.

Just wondered - because this application ideally would use power from two or three button cells - whether there is anything I can do in programming terms (nap, doze?) to reduce the quiescent current to less than the 600uA the controller draws while leaving it connected to the supply? I'm thinking probably not, but I thought I'd ask.

Regards, Simon
 

AllyCat

Senior Member
Hi,

Yes, probably something around a NAP 4 or 5, inside the "waiting for a button-press" loop. SLEEP is probably too long (minimum ~2.3 seconds) and DOZE is for X2 PICaxes only. The delay is not particularly accurate (perhaps +/- 10%) but much better than the User-Manual indicates. There is also the SETFREQ command, but strangely you might actually increase the frequency, to reduce the execution time (between SLEEPs) by proportionally more than the increase in active current. ;)

That will probably get the average current drain down to below about 100 uA; some further reduction may be possible by switching off some of the chip's internal hardware, but it's probably not worth the trouble. Another power-saving measure could be to reduce the supply rail to, say, 2.5 volts which can still run the PICaxe perfectly well. The relay would then need a separate "higher" voltage supply rail, or you might use a "boosting" hardware arrangement with an Electrolytic capacitor, such as a "bootstrap" or "H-bridge" arrangement. Or even an inductive boost converter driven by a PWM output pin.

Are you happy with the Program Code and Simulation? For amusement, I actually got the main loop down to about 6 lines of code, but didn't want to include too many "unnecessary" changes. :)

Cheers, Alan.
 

Krolroger

New member
Well, that's quite a result! NAP 4 reduces the drain to less than 20uA . NAP 5 gets it down a little further but makes the button response a bit iffy. A couple of CR 2032 lithium cells should last well over a year.

Next step is to miniaturise this with a SMD picaxe.

Many thanks,

Simon
 

AllyCat

Senior Member
Hi,

Yes, that's rather lower than expected. But I don't think much is running by default during Sleep/Nap, except for the Brownout Detector and the Sleep timer itself (the PIC's internal Watchdog timer).

Strictly, 2 x Lithium cells are a little over the base PIC's maximum supply voltage rating, so perhaps connect a forward diode or LED in series with its supply. Note that the PICaxe is able to measure it's own supply rail voltage, using the CALIBADC10 command, with no additional hardware or pins required. So it could even increase its own current consumption a little, if it found the series drop was inadequate, due to the very low current drain. ;)

Cheers, Alan.
 
Top