Pages: [1]   Go Down
Author Topic: Relay not activating every time  (Read 1080 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-sad

Code:
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;
  }
}



* V2_bb.jpg (253.58 KB, 1026x1110 - viewed 40 times.)
Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2452
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

alabama
Offline Offline
Full Member
***
Karma: 1
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Einstein once said you don't really understand anything until you can explain it to your Grandmother

Online Online
Faraday Member
**
Karma: 80
Posts: 3790
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Online Online
Faraday Member
**
Karma: 80
Posts: 3790
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2864
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bob

Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 44
Posts: 1508
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;

Quote from: 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.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2778
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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;
           }
       }
  }
Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2452
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Sainsmart relay module didn't need any additional diodes, resisters, etc.
Sainsmart relay module?  First mention of that.

The problem was in my code.
  versus
Quote
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.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Pages: [1]   Go Up
Jump to: