Instructables

DIY Arduino PIR Motion Sensor Lighting & Security

Picture of DIY Arduino PIR Motion Sensor Lighting & Security
From the minds at http://arduinotronics.blogspot.com/

We wanted to save energy, and create convenience, by adding motion sensors to our lighting circuits. Maybe you want some notification of an intruder. Both can be done with a PIR Motion sensor.  When I walk into a room, the lights come on automatically, and when I leave, shut off after a short period of time. You can choose how long that time delay is in the code. No more fumbling for a light switch in the dark with my arms full of groceries!

I also wanted a override switch for times I want the light to stay on, or off.

So, I took an Arduino, added a PIR sensor, a SSR, a SPDT switch,  two 10k ohm resistors and whipped up a small sketch to glue it all together. Enjoy!

Original article, and more at http://arduinotronics.blogspot.com/2013/01/motion-sensors-ssrs.html

Multiple motion sensors, and multiple SSR's can be serviced by a single Arduino. A embedded Atmel chip can be built into the project instead of dedicating a complete Arduino board.

A CdS light sensor can be added to prevent the lights from activating if light level's are considered sufficient (user programmable). http://arduinotronics.blogspot.com/2012/03/light-sensing-with-cds-ldr.html
 
Remove these adsRemove these ads by Signing Up

Step 1: The PIR Sensor

Picture of The PIR Sensor
The first step was connecting a PIR sensor. A PIR detects "motion" by recognizing an increase in Infra Red emission in the focus area, put out by the human body.

For background info on PIR, see http://en.wikipedia.org/wiki/Passive_infrared_sensor

We obtained our PIR Sensor from Amazon.

Connect the 3 pins of the PIR to +5vdc, Arduino Pin 2 (data out), and Ground.
sriramsrikp4 months ago

How to add another PIR sensor in this circuit??? i.e. how to connect two PIR sensor in a single circuit??? If i cross either of the sensor means the light should glow.

sspence (author)  sriramsrikp4 months ago

We are using pin 2 for the data connection of our sensor, add a second sensor to pin 3.

1. in the code, add (in the appropriate places):

int pirPin2 = 3;

pinMode(pirPin2, INPUT);

motionDetect2 = digitalRead(pirPin2);

2. and replace

if (motionDetect == HIGH)

with

if (motionDetect == HIGH || motionDetect2 == HIGH)

Thankyou very much for ur reply sir. Could you please tell me that where i have to add the first part code in the existing code??

sspence (author)  sriramsrikp4 months ago

Right after the existing lines that match:

after

int pirPin = 2;

put

int pirPin2 = 3;

-----

after

pinMode(pirPin, INPUT);

put

pinMode(pirPin2, INPUT);

----

after

motionDetect = digitalRead(pirPin);

put

motionDetect2 = digitalRead(pirPin2);

After the modifying the existing code with the above one , the circuit have to work as i am thinking. If i cross any one of the two PIR sensors means the bulb will glow for 3 mins. suppose if i crossed any one of the two sensor, the bulb is started glowing for 3 mins. With in that 3 mins if cross the same sensor or another sensor means the time wil be reset for 3 mins and the wil glow for 3 mins. It wont bling once while the time is getting reset. Am i correct sir?? And the 5vdc and ground in the diagram mentioned is 5vdc adapter connected to the circuit. Am I correct sir?? This is my first Arduino project sir. Thats y I am clarifying my doubts this much. I hope u dont mind. Thank you sir.

sspence (author)  sriramsrikp4 months ago

The +5vdc is the 5vdc from the arduino, not the input. power to the arduino comes from a USB cable or a power supply using the barrel jack (9-12v typically).

Hello sir, I tried the coding for restart the timer if new event is seen by the sensor. But no success. could u pls help me by providing the code for that??

And where I have to add the LDR coding in the main code??

Pls help...

sspence (author)  sriramsrikp4 months ago

The code is not currently written to restart the timer if a new event is seen, but it could easily be made to.

Sir, In the schematic diagram, +5vdc is connected to either pin 11 or pin 12 with the help of switch s1 to transfer the positive charge to the circuit. But the Ground also connected to the same pin 11 and pin 12. Here the both +ve and -ve terminals are connecting in one pin itself. Will it create short circuit?? If not means how that part is working??

And what will be the code to restart the timer if a new event is seen??

Thank you sir...

sspence (author)  sriramsrikp4 months ago

Gnd is not shorted to 11 & 12, there are pulldown resistors in line. This prevents floating inputs. I do not have the time right now to build the timer restart code. It's not that difficult, give it a try.

ok sir. I will try. Thank you for spending some time to answering for my clarifications. :-)

Soulfien4 months ago

There is an error in the code. The delay cannot be adjusted to a small duration (5 seconds). It wasn't until I added in this line to the void loop that it worked as I wanted it to:

digitalWrite (ssrPin, LOW);

You have that line in the setup and that wasn't working. I added it to the loop and the delay was then able to actually shut off the light the moment the delay ended.

sspence (author)  Soulfien4 months ago
Awesome. Thank you.
Oliverhall4 months ago

Nice tutorial!

You can get the hc-sr501 PIR motion sensor for cheap here: http://voltatek.com/en/sensors/69-motion-sensor-m...

MadridFC8 months ago
My SSR operates when the DC volatge is 12V.so i even if my arduino gives a high the ssr doesnt close the switch and my lamp doesnt glow.what to do?
sspence (author)  MadridFC8 months ago
SSR's don't switch DC, They switch AC. You need a MOSFET.
angpal5911 months ago
I'm not getting any errors but I can't get either code to work, the light just stays on constantly, I'm using a SSR-40 DA. Also I don't get the switch. What is it for? When I first download it, the lights off, I wave my hand in front of it and the light comes on but never goes off.
sspence (author)  angpal5911 months ago
You may have the delay set on the motion sensor for a long timeout. The switch is for a over ride, as described in the instructable. on , off, or automatic.
Ronot60001 year ago
I think I see what I did, I'll try putting the close bracket at line 29 to close the nested if statement, and removing the extra I put at the end.
sspence (author)  Ronot60001 year ago
yep, I see it, should be a } after delay (180000); //Optional 3 minute delayed off


delay (180000); //Optional 3 minute delayed off
}

I'll fix the instructable.
sspence (author)  sspence1 year ago
fixed!
Ronot60001 year ago
Ha ha, sorry I'm new at this. I'm not driving anything that's not in the instructable, but I'll definitely check the voltage, maybe one of my components are defective.

Yes, the symptoms are as follows: With everything connected and nothing in front of the sensor the lamp blinks, I tried different lamps and some blink very quickly and some blink slowly. When I walk in front of the sensor the lamp goes from blinking to solidly on. When the wire from screw 3 of the SSR to arduino pin 13 is dosconnected, the lamp is solidly off. This lead me to believe the problem was the SSR "off state" but maybe not. I've tried a couples SSRs and the same thing happens.

The code I used is mostly the same as in your instructable. I couldn't get it to compile so I added a semicolon after "digitalWrite(ssrPin, HIGH)" in line 33, after "digitalWrite(ssrPin, LOW)" in line 38. It was also saying that it wanted an additional close bracket so I put it at the end, but maybe I've done this wrong as well. At what line is the loop meant to end?

int inPin1 = 11; // switch connected to digital pin 11
int inPin2 = 12; // switch connected to digital pin 12

int ssrPin = 13;
int pirPin = 2;
int motionDetect= 0;
int manualSwitch = 0;
int motionSwitch = 0;

void setup() {
pinMode(ssrPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
digitalWrite(ssrPin, LOW);
}

void loop() {

motionSwitch = digitalRead (inPin1);
manualSwitch = digitalRead (inPin2);

if (motionSwitch == HIGH) // Motion Mode
{
motionDetect = digitalRead(pirPin);
if (motionDetect == HIGH) {
digitalWrite(ssrPin, HIGH);
delay (180000); //Optional 3 minute delayed off

}
else if (manualSwitch == HIGH) // Manual On
{
digitalWrite(ssrPin, HIGH);
}
else // Manual Off

{
digitalWrite(ssrPin, LOW);
}
}
}
sspence (author)  Ronot60001 year ago
I did find two missing ;

but no missing }

fixed the code.

I have a whole new code sketch to upload this week with different features, but works better.
Ronot60001 year ago
I've been having some trouble with the light blinking in the ssr "off" state. From some googling I've found that this might be due to something called "leaking" but adding a 10k resistor from screw 3 to ground doesn't resolve it. The light does not blink when screw 3 is disconnected from the breadboard. Does anyone know what might be causing this?
sspence (author)  Ronot60001 year ago
disconnecting screw 3 would turn off the load. so you are correct, it won't blink. the only thing that the arduino is powering is a IR LED. There's is nothing to leak. You may not be driving the LED with enough voltage and or current. Measure the voltage at pins 3 & 4, should be 5v. If you can't drive it hard enough (too many other devices being powered off the Arduino 5v), drive a 2n2222 transistor with an separate power supply flowing through it to the SSR.
sspence (author)  sspence1 year ago
ok, I'm understanding your issue more. The SSR is leaking through pins 1 & 2, even though pin 3 is sent a low (gnd), but not if 3 is disconnected? A resistor would not help you, as it could be a failure of the SSR or not sending pin 3 a low to turn it off. sending pin 3 a low is the same as shorting pins 3 & 4 together, which should keep the load turned off, if the SSR is functioning properly. I believe it is, since disconnecting 3 does cause it to stay off, correct? Please post your code.
angpal591 year ago
(removed by author or community request)
never mind, I just did it a different way
sspence (author)  angpal591 year ago
want to share?
How do you do that? I am new to this even thoufg I've been a member I've never shared
sspence (author)  angpal591 year ago
Just paste your sketch, or email me at solar@essnmag.com
I keep getting " a function-definition is not allowed here before '{'token
sketch_feb28b.ino; In function 'void loop()':
sspence (author)  angpal591 year ago
Please post code.
sspence (author)  angpal591 year ago
I'd have to see your code.
vectorges1 year ago
I think the section of Step 5 dealing with the "AC from wall" needs some clarification. AC has load and ground I think you are running load to pin 1 and ground to the lamp, but it isn't clear from the schematic. Otherwise it is great. I love the idea of the override switches.
sspence (author)  vectorges1 year ago
AC has Hot, Neutral, and Ground. We are breaking the HOT Line and inserting the SSR into the Hot Line (Series). Neutral continues to the appliance unbroken. We are not breaking the ground line (if it exists).
sspence (author)  sspence1 year ago
I updated the schematic, labeling the Hot and Neutral lines.