Can't get relay lock on / off to work properly.

I am working on a money bank project. It is a Tardis box with encoded audio and a flashing light to make the Tardis launch sound and flash the Tardis light when activated by a coin.

Wrote the first sketch no problem using the reset input to activate the program when the coin hit reset button. The problem is that leaving it powered up drains the battery too fast.
So I used a relay to turn on the circuit from the coin switch providing power to the relay and the relay latching itself on. The final line of the sketch is an input set to low to sink the power to the relay to ground and shut the relay down. but it doesn’t work! Relay won’t disengage. applying the input at all sets the relay high and it runs the program without the coin switch. How come? initially it works without the input connected OK but how do I get it to shut down. Input set to low should be same as ground right? If I touch the input to the relay coil to a hard ground it shuts the relay off OK. But it won’t work in the sketch.

Anybody have any ideas to resolve this. (Thought about using input to set relay High but I don’t think the coin will hold the circuit on long enough to get the program activated and get the input to keep relay on).

Any ideas would really be appreciated. Thanks. Here is the sketch code:

#include <PCM.h>

int val = 0;

const unsigned char sample PROGMEM = {
124, 123, 129, 131, 131, 130, 128, 124, (a whole lot of data here for encoded Audio)
};

void setup()
{
pinMode(12,OUTPUT);
digitalWrite(10,HIGH);
startPlayback(sample, sizeof(sample));
do {
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(750);
val = val + 1;
} while (val < 3);
digitalWrite(10,LOW);
}
void loop()
{
}

(deleted)

That doesn't help. but yes you're right. While I was wrestling with the code that got dropped, but the result is the same either way.

Your hardware description sounds unusual. Perhaps you should show us a schematic.

The final line of the sketch is an input set to low to sink the power to the relay to ground and shut the relay down.

I take it you mean output, but even an output set to low cannot sink the relay's coil power to ground. Try using an NPN transistor, like the 2N2222A with a base resistor of 1K to 4.7K. Don't forget to use a diode reverse connected across the coil. P.S. Don't try using the transistor to short the relay coil's +'ve power terminal to ground. It might do more than just turn off the relay.

Hi;
How have you got the arduino connected to the relay?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

Thank You Guys so much for your responses. Yes the transistor will work, I’m pretty sure. I should have thought of that but sometimes you get blinded by trying to make an idea work and you forget to look at other approaches. Thanks.

I tried to post the code entirely but it is too loong and exceeds the the maximum 9000 chars.

Please see a schematic drawing here. I have not built it yet but with your input this should be the final project.

#include <PCM.h>

int val = 0;

const unsigned char sample[] PROGMEM = {
  124, 123, 129, 131, 131, 130, 
};

void setup()
{
  pinMode(12,OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  startPlayback(sample, sizeof(sample));
  do {
    digitalWrite(12, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    delay(750);
    val = val + 1;
  } while (val < 3);
  delay(300);
  digitalWrite(10,HIGH);
}
void loop()
{
}

.
Tardis DWNG.png

If the picture LarryD posted is what you have, the problem is clear to me

Your coin switch energizes the relay, which keeps itself held on and your transistor is trying to short out the relay coils.. that transistor is probably dead now.

The transistor/diode need to move BETWEEN the relay and switch/relay contact, and needs to be "ON" at all times except when you want to release the relay.. I'd call that pin "RelayEnable", which you'd set to LOW to disable it... you'd of course have to set it high after a short time to allow for another coin drop to be detected.

Honestly, I don't see the point of the relay, nothing it controls is high-power... Why not just set the coin switch to an input and do everything programmatically? If the relay really is needed, I'd still do it that way, but control the relay through the transistor driven off another output pin.

The reason for the relay is not for high power (it is a very small relay), But the problem is when you drop a coin in the bank the switch is is on for only about a tenth of a second. Not long enough for the program to be active on the arduino.

Then the whole thing needs to shut down to save the battery from going flat in the in-between times. Relay takes more power than anything else but even without it keeping the arduino powered on flattens the battery in about a day or two.

There are several ways to save power using an Arduino (eg sleep mode) and just wake it up when a coin is inserted via an interrupt.

Unfortunately, the LED on the Arduino board (and the power regulator) also consume quite a bit of power (around 5mA if i remember correctly) so you will either need to desolder these or build a very simple Arduino board of your own when you have the whole thing working (which is much easier than you might think).

chris_1967: The reason for the relay is not for high power (it is a very small relay), But the problem is when you drop a coin in the bank the switch is is on for only about a tenth of a second. Not long enough for the program to be active on the arduino.

So, the purpose of the relay is to hold state while the arduino is asleep.

There might be other bistable circuits that can do this. If you really like the idea of relays, then a pair of relays could be wired up - power the coil of each from the normally-closed contact of the other. A pulse to either coil will flip the state. You could do exactly the same thing with two NAND gates. Or just grab a logic chip with some latches. If you have transistors lying around, I'm sure there are plenty of transistor latches and flip-flop circuits out there.

Having said that - a tenth of a second is a long time. It would normally be more than long enough to wake up an arduino and have it do some counting before going back to sleep. The only reason I can think of for it not being long enough might be if you are switching the board off altogether rather than in a "wake on interrupt" state.

OK so to review some of the suggestions I have so far...

If I use the transistor as a maintain ON transistor I should think I would have to use a power transistor. Ok so then if I use a power transistor I could just use it to shunt the relay coil off like I had planed. But if I use a power transistor I could I potentially just use that without the relay all together.

If I use a 2N222 type to maintain ON then I would need some large resistance to protect the transistor and that would be barely enough to hold the relay on and would starve the rest of the circuit.

As it is a transistor might provide too much resistance.

To leave the Arduino ON in sleep mode and delete the diodes well I haven't thought of trying that but it seems in a circuit like this you might have days and days of total in activity. (no money put in the bank) and then 4 seconds of on time when a coin is inserted. it would seem counter intuitive to maintain ON in this situation. Even if I could make the consumption last for several weeks I think this would be wasteful.

Anyone have any idea what kind of lifespan I would get from my 9v battery in this situation?

I suppose the relay from the beginning is a throw back to my industrial automation training. Master control relays to maintain circuits on and control relays to maintain push button inputs.

A latching type relay would be a good thing to consider. They only need about a 100ms impulse to set or reset the contact state. They draw (and require) absolutely no power to hold their state.

Well that's an intelligent suggestion...

and I looked it up there is a 9v latching relay available from DigiKey for 4 bucks. (plus shipping etc. etc.) It is a Panasonic product. It would take a week or two to get here....

I already have an NPN power transistor here and a signal transistor to trip it...

I think I'll go that route.

But thanks... Good suggestion!

The code is quite simple to operate them, consider the dual-coil versions for ease of connection and operation. The single coil versions are much more difficult as they require an H-bridge style control circuit.

For any type, make sure the coil is never steadily energized, as it will burn out. Panasonic dual coil latching relay, 2A

If you do use a latching relay, don’t buy cheap.
From my experience, the cheaper offerings become intermittent.
.

Also, I would get a 9V zener diode. Use this in series with a diode for coil suppression. A diode only will slow down the contact release time, increase contact arc and decrease the relay's life expectancy. http://jumperone.com/2011/10/using-relays/

Ok so with a little experimentation with resistors I found it worked better with the transistor in the enable ON position but it needed a pull-up resistor to set it high right away.

Anyway long story made short this final drawing I have built on the proto board and it works.

Also the code here:

#include <PCM.h>

int val = 0;

const unsigned char sample[] PROGMEM = {
131, 130, 129, 128, 126, 128, 129, 129, 125, 125, 128, 129, 130, 131, 132, 132, 130, 130, 130, 131, 131, 130, 130, 131, 131, 132, 132, 134, 132, 130, 126, 128, 129, 129, 125, 123, 123, 123, 121, 119, 120, 124, 129, 131, 134, 135, 135, 135, (12 pages of Data here)
};

void setup()
{
  pinMode(12,OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  startPlayback(sample, sizeof(sample));
  do {
    digitalWrite(12, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    delay(750);
    val = val + 1;
  } while (val < 3);
  digitalWrite(10,LOW);
}
void loop()
{
}

Are feeding 18 volts to the Arduino power jack?