View Full Version : TOGGLE question

17-11-2005, 23:30

I have built a very simple programmable switch.
The device uses an 08A, and a relay.
The circuit employs a push button to set off the timer, or to initiate a change in state.

In this case, I programmed the 08 using the TOGGLE command, so that with every press of the trigger button, the relay would change state.

This worked wonderfully, but after being in the "ON" state for around 6 hours or so, the relay drops out, and I can't see how the program could be letting it do this.

Here is my code:

'Relay controller/Picaxe switch.
'Version 1.0

'This code simply switches a relay on then off with one momentary push-button switch.
'The chip is programmed for the type of switch you want: timer, momentary, toggle, etc.

low 0
low 1
low 2
low 4

if pin3=0 then deb 'Wait for pwr switch to be pressed
goto start

pause 500 'Debounce the switch contacts
if pin3 = 0 then relay 'Once contacts settle, if still pressed, then goto relay
goto start

toggle 2 'Toggle state of pin2
wait 2 'Allow relay to operate, and switch to be released
goto start


I have used on-board 78L05 regulation, with decoupling and filtering. I have used darlington trasnsistor to switch the relay, with back-EMF protecting diode.

The DC input is 12v, and there is a 2200uF cap across this 12v input for the purposes of acting as a resevior cap and preventing any slight drop in the 12v supply rail causing the relay to drop out. Perhaps I should put a suitable cap across the relay coil aswell?

For the moment, I have bypassed this module, with a simply mechanical toggle switch, but I am curious as to how this could happen.

The input pin used to look for the button, is connected as per the diagram in the manual, with the 10k and 1k resistors.

Any questions?
Please ask.


Edited by - Grogster on 11/17/2005 11:37:20 PM

Edited by - Grogster on 11/17/2005 11:40:57 PM

18-11-2005, 01:16
As you say, it should be working, so the question has to be; what could cause the relay to drop out unexpectedly ?

Loss of current through the relay through power sagging or the transistor failing to keep a low enough resistance ought to recover as soon as voltage/current is restored. That leads to the possibility that what drives the transistor is switching.

The output pin is only altered when the PICAXE is reset or a button push causes the toggle. Is some transient causing the reset, or an imaginary button push to be seen which also gets through the debouncing ?

If it drops out again, the first thing to do is check all voltages, and particularly what the output pin voltage is which drives the transistor. The code may well be working, but the hardware has somehow failed.

What you really need to do is to monitor what state the PICAXE is in when it fails and how it got there when it does. The easiest way to do this is to put SERTXD or SEROUT commands in the code and monitor what gets sent out using the Terminal window of the Programming Editor. Rewritten to make your code easier to add such code (untested) ...

- low 0
- low 1
- low 2
- low 4
- Start:
- WaitingForButton:
- IF pin3 <> 0 THEN WaitingForButton
- PAUSE 500
- IF pin3 <> 0 THEN Start
- WAIT 2
- GOTO Start

Set the program going, activate a few tests to check the code works and displayed output is as expected, turn the relay on, clear what's on the display and wait for another failure.

There are other tricks you can try using LED's to monitor status and buzzers which can be used which sound at reset and so on, but using serial to display messages has the advantage that it builds a history of what's happened even when you aren't there or watching it. The downside is that it ties up a PC ( or similar ) while waiting for a failure.

If, having left it alone to soak, anything is displayed on the display that indicates external interference or pick-up. If the relay drops out then the sequence of messages should explain how it got there. If there's nothing shown then it will require a little more thought.

If that situation does occur. Press the button to check that serial is still being sent, that the PICAXE is executing correctly and observe what the relay does. Knowing what is happening ( or isn't ) after a failure helps in analysing what that failure may have been caused by.

Note that adding the 'trace code' does affect the original operation ( for example that 500mS debounce period will be stretched ) so the failure may not materialise, but a reset or interferenc should be reported. It is possible to create more complex trace code which has almost no impact on original operation but that takes considerably more effort to write.

Edited by - hippy on 11/18/2005 1:27:35 AM

18-11-2005, 01:35
Thanks hippy!

I will re-program and test this.

I will have to use SEROUT, as this relay module uses the 08A, not the 08M, and the manual says that SERTXD does not work on the 08A...

I will setup an old DOS laptop, with the relay module, and set it going on test, with a log-file open in Procomm...

What are the byte-values for a CR and LF so I can program these bytes to be sent?


Edited by - Grogster on 11/18/2005 1:40:10 AM

18-11-2005, 02:15
Yes you'll have to use SEROUT ( 08A is actually just plain "08" ), but use Pin 0 and the data goes out the Download Serial Out line which can be handy.

An old PC with some terminal sotware is often invaluable for things like this. CR and LF are pre-defined within the Programming Editor.

18-11-2005, 03:14
FYI, CR is 13 ($0D) and LF is 10 ($0A), although, as hippy says, they're predefined in this environment.


24-11-2005, 12:46
I had a bizare random reset on one of my projects using an 08M. Couldnt figure out why it seemed to occur every time i went into the room to see the box - turned out to be the flourescent light in the room next to the box - replaced the starter and it works fine now :)


24-11-2005, 20:36
Just letting everyone know, that I have not had a chance to try the suggestions yet, but I will soon - i've been too busy, and the mecahnical switch does a better job then my relay/PICAXE attempt anyway, so the module has been put on low-priorit!!!! :P

I will post, when I have tested, just so everyone knows what is happening...