Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: January 15, 2013, 10:13:21 pm » |
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.  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; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 24
Posts: 1477
Now, More Than Ever
|
 |
« Reply #1 on: January 15, 2013, 10:31:57 pm » |
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
|
Don't React -- Read!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #2 on: January 15, 2013, 10:50:20 pm » |
Not sure of the specifics, but this is what I'm using. http://www.amazon.com/gp/product/B0057OC6D8/ref=oh_details_o00_s00_i00The 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
Full Member
Karma: 1
Posts: 169
|
 |
« Reply #3 on: January 15, 2013, 11:12:21 pm » |
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
|
|
|
|
Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #4 on: January 16, 2013, 12:12:00 am » |
Veeeeery confusing colour scheme on the wiring there.... would be simpler if you stuck with the red black yellow idea from the PIR 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
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #5 on: January 16, 2013, 05:49:01 am » |
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
|
|
|
|
|
Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #6 on: January 16, 2013, 08:51:11 am » |
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
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Anaheim CA.
Offline
Edison Member
Karma: 34
Posts: 2404
Experienced old Whitebeard with a Full head of Hair...
|
 |
« Reply #7 on: January 16, 2013, 09:12:00 am » |
And to use a transistor to control the relay. NEVER connect a coil Like a RELAY COIL TO AN Arduino pin, unbuffered.
Bob
|
|
|
|
|
Logged
|
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 282
Posts: 15443
Measurement changes behavior
|
 |
« Reply #8 on: January 16, 2013, 10:08:09 am » |
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.  Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #9 on: January 16, 2013, 01:58:50 pm » |
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
Sr. Member
Karma: 10
Posts: 379
|
 |
« Reply #10 on: January 16, 2013, 06:02:04 pm » |
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; 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
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 839
|
 |
« Reply #11 on: January 17, 2013, 01:47:49 am » |
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
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #12 on: January 17, 2013, 06:51:31 pm » |
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; } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 24
Posts: 1477
Now, More Than Ever
|
 |
« Reply #13 on: January 17, 2013, 08:42:45 pm » |
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 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
|
Don't React -- Read!
|
|
|
|
|