It seems like a simple thing but I not sure what Im doing wrong. I building a controller for a traffic light that I want to hang in the garage. Im using a standard atmega 168 programmed with an arduino board. Im using 3 digital pins to control a relay board. The problem that Im having is that the lights don't always turn off when the pin goes low. Here is the program that im using:
/* YourDuino Example: Relay Control 1.10
Handles "Relay is active-low" to assure
no relay activation from reset until
application is ready. terry@yourduino.com */
void setup() /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//---( Turn all 4 relays ON in sequence)---
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(60000); // wait for a second
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
delay(1);
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(10000); // wait for a second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(1);
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(60000); // wait for a second
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
delay(1);
The wiring looks OK but prototype boards are fairly notorious for bad connections. I would try a simpler sketch that turns all the relays on. Then you can wiggle wires to see if a connection is loose.
Yeah... Are you sure the pin is going low? If the pins are switching as you expect, it's not a software problem.... Are you checking the output pins with a meter, or an LED or something?
delay(60000); // wait for a second
Just my opinion, but I'd change that comment, since "a" second implies "one" second.
It's obviously a delay, even if you don't understand the Arduino programming language. So, if you don't want to put the actual time in the comment, you can delete the comment. Or, maybe change it to: "//delay(milliseconds)". Or, indicate the purpose for the delay: "//Leave green light on for 60 seconds"
John
Think Im going to go ahead and solde up a stand alone Arduino to eliminate the chances of a bad connection.
Doug,
That was some canned code from the site that sells the board. I was playing around with the delays to get the timing down for the light. It seems that I can lightly tap the relay after it the Arduino goes low and the contacts will go open. I have tried a meter and an led with a battery.
Im starting to think that these Songle relays are junk....It seems that I cap tap them after they are de-energized and all goes good.
I think you are right. I think it's a "sticky" relay.
As a possible "quick-and-dirty" fix - If it works sometimes, try "exercising" the relays by turning them on & off rapidly (maybe 100mS) with no load attached. Let it run for 1000 cycles or so. If there's some corrosion on the contacts, that may clean it off. Of course if it's not working at all, then it cannot be exercised.
Quite often, this will work. If this works, it might be a permanent fix, or it might be a short-term fix... In any case I wouldn't use that relay in a medical device.
When the relay is off, it should always be off, but it's possible to partially energize a relay if there's something wrong with the electronics or if the output switches on & off at a very fast rate, like how PWM works.
I have to give credit where credit is due. Thanks Johnwasser! It was indeed loose connections at the perf board. It seems that if I have a loose connection the board never gets the signal to go low and turn relay off. Repaired connections and works like a charm! Thanks for all the help guys!
That look like a nice board that I need to build. Did the board include a resistor at the base of the transistor ? I saw LED's on this board...one green <-- power indicator ? and 4 red one <-- relay indicator ? On - relay active Off - relay off. I saw 4 transistors and 4 diode , limiting resistors for the leds, and the relay are rated at 10 A. And big headers... Interesting
Back to square one! Soldered up the circuit and have basically the same issue I had before. The relays will each energize in turn and stay energized until power is removed from the circuit. I had it working ok last night but after soldering it up it does just as described. I added the decoupling cap between the power and ground but it did not help. Im running out of ideas here Any help would be much appreciated. All pins are connected correctly and there are no solder bridges.
Can you please try this test code. You have to change the pins numbers to fit your hardware setup/connection.
The code compile, but not tested...
const boolean RELAY_ON = 0;
const boolean RELAY_OFF = 1;
const int Relay_1 = 2; // Arduino Digital I/O pin number
const int Relay_2 = 3;
const int Relay_3 = 4;
const int Relay_4 = 5;
void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
delay(4000); //Check that all relays are inactive at Reset
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//---( Turn all 4 relays ON in sequence)---
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(1000); // wait for a second
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(1000); // wait for a second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(1000); // wait for a second
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_4, RELAY_ON);
delay(1000);
digitalWrite(Relay_4, RELAY_OFF);
}//--(end main loop )---
Techone - Tried it with no luck. I also tried to use my arduino board and I get the same thing. Im guessing these relay boards could be the issue. I can tap the relays lightly and make the contacts open. It seems like something is keeping them engaged.
Johnwasser - that is the place that I bought the board from. Funny thing that I have noticed. Diodes d1-d4 stay lite dimly when that pin is not energized. I'm thinking that there is enough current to keep the replays energized. You can hear the relays dis engage as soon as you pull the power.