Relay not activating every time

I might be missing a resistor or something, but with the following code and prototype layout, is there something I'm missing? The logic seems to work out ok in the code, the relay 'led' lights, but the relay doesn't actuate every time the PIR goes HIGH. Sometimes it does, sometimes it doesn't. I don't get it. :frowning:

int PIR = 2;                   // arduino board pin for PIR sensor
int RELAY = 7;                   // arduino board pin for RELAY
int LED = 13;                    // arduino board pin for the LED light 
int val = LOW;                      // variable for reading the pin status 
int toggle = 1;                     // variable to track PIR activation

void setup() {
  pinMode(PIR, INPUT);         // declare sensor as input 
  pinMode(RELAY, OUTPUT);        // declare relay as output
  pinMode(LED, OUTPUT);          // declare LED as output 
  digitalWrite(RELAY, HIGH);
  delay(15000);
} 

void loop(){
  val = digitalRead(PIR);                 // read input value
  if ((val == HIGH) && (toggle == 1)) {        // check PIR input is HIGH
    digitalWrite(LED, HIGH);                // turn LED ON 
    digitalWrite(RELAY, LOW);            //activate relay
    digitalWrite(LED, LOW);                 // turn LED OFF
    toggle = 0;
  } else if ((val == HIGH) && (toggle == 0)) { 
    digitalWrite(LED, HIGH);                // turn LED ON
    digitalWrite(RELAY, HIGH);           //activate relay
    digitalWrite(LED, LOW);                 // turn LED OFF
    toggle = 1;
  } 
}

What is the coil resistance of that relay?
If that's less than 150? then you don't want to run it directly from an Arduino output, it needs the help of a transistor.
Does the fritzing diagram indicate a "flyback diode" across the coil, too?
(There are solid examples for using relays and transistors at the Arduino Playground.)

Not sure of the specifics, but this is what I'm using.

The specs say it uses 15-20mA driver current. I believe the Arduino outputs that much. But the way I have it wired, I think the voltage is split in parallel and getting reduced??

I don't see a resistor on the led either, that could be drawing the power supply down enough to make the relay unreliable. You are probably frying your Arduino outputs. Might slow things down too, I know I can't see a led change state in a few microseconds.
TomJ

Veeeeery confusing colour scheme on the wiring there.... would be simpler if you stuck with the red black yellow idea from the PIR 8)

Here's a good tutorial on driving a relay. And seeing as you have the LED on pin 13, why not lose it anyway since it will be using the built in one.

Sorry about the coloring. Is there a standard? I used black for power, green for ground, red for signaling. I removed the LED as you suggested. I get the same results. I'll check out the tutorial. Thanks for the link!

Not sure if it's a colour standard in the official sense, but take your cue from the sensor you connected to where the wires started out as red for +ve, black for ground and yellow for control.... servos are the same too.

Red and black for 5 and 0 is pretty much standard....

It would certainly be worth hooking the relay to its own power and simply using the Arduino as control.

And to use a transistor to control the relay. NEVER connect a coil Like a RELAY COIL TO AN Arduino pin, unbuffered.

Bob

Docedison:
And to use a transistor to control the relay. NEVER connect a coil Like a RELAY COIL TO AN Arduino pin, unbuffered.

Bob

Well that is a little drastic isn't it? There are many small DC 5 volt reed relays that draw well less then 30ma and as long as you install a diode across the relay coil terminals you can drive them fine directly from a arduino output pin, I've done it frequently with no drama. :wink:

Lefty

Thank you guys for the education. I'll update my wiring with the proper colors to the proper locations, and grab a diode on the way home from Radio Shack. The specs show this relay will work fine with 15-20mA triggering. I've confirmed that by using the signal from the PIR, which flips the relay on and off every time the PIR goes HIGH/LOW. I think the problem is the missing diode. I'll update after I try that.

Thanks again!

darrenmathews:
Not sure of the specifics, but this is what I'm using.

http://www.amazon.com/gp/product/B0057OC6D8/ref=oh_details_o00_s00_i00

The specs say it uses 15-20mA driver current. I believe the Arduino outputs that much. But the way I have it wired, I think the voltage is split in parallel and getting reduced??

That is a link to a relay module, not a relay.
Are you actually using the module, or a separate relay as used in this module ?
That's a big difference.

I did some looking up of that relay.
A shop offering this one, says;

Ali baba:
Coil parameters
Coil power: 0.36 W

Assuming this is correct information, P = 0.36 W
U = 5 V
Then I will be:
I = P/U;
0.36 / 5 = 0,072 A
That is 72 mA

So if you are using that relay directly without the module, you will need a transistor (and still the diode to protect that transistor).

If you are using the module, you do not need a diode because the module has transistors and diodes built in.
But you will need to power the module and there's no sign of you doing that in your Fritzing sketch.

I don't really understand what you are trying to achieve with the logic in your arduino script.

When your loop executes with the PIR signal high, it is going to execute those two different
parts of the loop alternately on each cycle of the loop, which is going to be rather often.

I don't even know if you would see your LED turning on and off at that rate, it is going to
depend on how fast digitalWrite() works.

If you are trying to detect the edges where the PIR input goes high, and then goes low,
it doesn't look to me as if your script is actually going to do that.

Problem solved.

I wanted to activate a relay ONLY when the PIR was initially activated, kinda like a light switch; wave your hand, the light comes on, then again later to turn it off. The Sainsmart relay module didn't need any additional diodes, resisters, etc. The voltage and signalling provided by the Arduino Uno is sufficient. The problem was in my code. Here is it in case anyone should need it. Thank you all! My first project, now I just need to mount and solder and get it installed in my new custom-made headboard.

int calibrationTime = 20;        
long unsigned int lowIn;         
long unsigned int pause = 3000; 
boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;
int ledPin = 13;
int RELAY = 7;
boolean toggle = LOW;

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(RELAY, OUTPUT);
  digitalWrite(pirPin, LOW);
  digitalWrite(RELAY, HIGH);

  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

void loop(){
     if(digitalRead(pirPin) == HIGH) {
       digitalWrite(ledPin, HIGH);
       if(lockLow){  
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
         digitalWrite(RELAY, toggle);
       }
       
     if(digitalRead(pirPin) == LOW) {       
       digitalWrite(ledPin, LOW); 
       if(takeLowTime){
        lowIn = millis();
        takeLowTime = false;   
        }
       
       if(!lockLow && millis() - lowIn > pause){  
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           toggle = 1 - toggle;
           }
       }
  }

darrenmathews:
The Sainsmart relay module didn't need any additional diodes, resisters, etc.

Sainsmart relay module? First mention of that.

darrenmathews:
The problem was in my code.

versus

The logic seems to work out ok in the code, the relay 'led' lights, but the relay doesn't actuate every time the PIR goes HIGH.