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

Step 1: 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.
Still no 2nd (need 4th and 5th) pir
Are you expecting me to write your code for you? I can do that if you wish. Donations can be made at http://arduinotronics.blogspot.com
<p>code for 2 pirs??????? see comment</p><p> (<a href="http://www.instructables.com/member/sspence" rel="nofollow">sspence</a><br> <br> (author)<br> <br> <br> <a href="http://www.instructables.com/member/sriramsrikp" rel="nofollow">sriramsrikp</a><br> <br> <br> <br> 2 years ago)</p><p>Will not upload why? I am able to have 1 pir working. But the code for the 2nd will not upload Something about </p><p>&quot;motionDetect2 = digitalRead(pirPin2);&quot;</p><p>-------------------------------------------------------------------------------------------------------------<br> int inPin1 = 11; // switch connected to digital pin 11<br> int inPin2 = 12; // switch connected to digital pin 12<br> <br> int ssrPin = 13;<br> int pirPin = 2;</p><p>int pirPin2 = 3;</p><p><br> int motionDetect= 0;<br> int manualSwitch = 0;<br> int motionSwitch = 0;</p><p> void setup() {<br> pinMode(ssrPin, OUTPUT);<br> pinMode(pirPin, INPUT);<br> pinMode(inPin1, INPUT);<br> pinMode(inPin2, INPUT);<br> digitalWrite(ssrPin, LOW);<br> }<br> <br> void loop() {<br> <br> motionSwitch = digitalRead (inPin1);<br> manualSwitch = digitalRead (inPin2);<br> </p><p>if (motionDetect == HIGH || motionDetect2 == HIGH)</p><p><br> {<br> motionDetect = digitalRead(pirPin);</p><p>motionDetect2 = digitalRead(pirPin2);</p><p><br> if (motionDetect == HIGH)<br> {<br> digitalWrite(ssrPin, HIGH);<br> delay (180000); //Optional 3 minute delayed off<br> digitalWrite(ssrPin, LOW);<br> }<br> }<br> else if (manualSwitch == HIGH) // Manual On<br> {<br> digitalWrite(ssrPin, HIGH);<br> }<br> else // Manual Off<br> <br> {<br> digitalWrite(ssrPin, LOW);<br> }<br> }</p><br>
looks like you are missing a <br>int motionDetect2= 0;<br><br> right after <br><br>int motionDetect= 0;
<p> Sorry Steve That make it so no lights work. ( I only need 1 over ride switch) I can take it back to 1 pir and it starts to work.</p>
<p>this is the code at this time. but pir do not work (switch on arduino will make light blink)</p><p>int inPin1 = 11; // switch connected to digital pin 11<br>int inPin2 = 12; // switch connected to digital pin 12<br><br>int ssrPin = 13;<br>int pirPin = 2; // PIR Data out <br>int pirPin2 = 3;<br>int motionDetect= 0;<br>int manualSwitch = 0;<br>int motionSwitch = 0;<br><br>void setup() {<br>pinMode(ssrPin, OUTPUT);<br>pinMode(pirPin, INPUT);<br>pinMode(inPin1, INPUT);<br>pinMode(inPin2, INPUT);<br>digitalWrite(ssrPin, LOW);<br>}<br><br>void loop() {<br><br>motionSwitch = digitalRead (inPin1);<br>motionDetect2 = digitalRead(pirPin2);<br>manualSwitch = digitalRead (inPin2);<br><br>if (motionSwitch == HIGH) // Motion Mode<br>{<br> motionDetect = digitalRead(pirPin);<br> if (motionDetect == HIGH)<br> {<br> digitalWrite(ssrPin, HIGH);<br> delay (180000); //Optional 3 minute delayed off<br> digitalWrite(ssrPin, LOW);<br> }<br>}<br>else if (manualSwitch == HIGH) // Manual On<br>{<br> digitalWrite(ssrPin, HIGH);<br>}<br>else // Manual Off<br><br>{<br> digitalWrite(ssrPin, LOW);<br>}<br>} </p>
Hey......i have a doubt with your code ........why are turning off the after each if case.....? That would switch the light rapidly on and off.? Without the delay...... And if the output of the pir sensor is set in no-trigger mode then you will get output only when a human enters it leaves the detection range. ...... That would make this code useless right?
In motion mode, if motion is detected, the sketch pauses for the delay period with the lights on, 3 minutes in this case. <br><br>in manual on mode, it only goes off if the switch is changed to auto or off. in manual off mode, it never comes on. There is no problem with this code. The only improvement I might make is to keep the sketch running during the tripped on phase, and restarting the delay if additional motion is detected during that phase.
Oh......thanks for the info....... Just to clarify .... In motion mode if motion is detected it'll turn on only for the delay time.......... Right? It won't turn and stay on when the human is inside the range right?
<p>correct, but if there is still motion when the timer runs out, the delay restarts for another period. With a small change, motion during the on time could keep resetting the on timer, instead of weaiting tioll the end of the delay to check again.</p>
Thats cool .........thanks for the info!
<p>Is it safe to use this 25A SSR ( http://amzn.to/1rVuILv ) if I am only using it to control a 4 Amp vacuum?</p>
Sure. It's rated for a max of 25 amps. It will control anything less than that.
<p>Is it possible to activate two sensor at the same time?</p><p>i think delay(---) is the main problem.</p><p>because triggering two sensor at the same time by human motion is impossible.</p><p>if one sensor goes to high then wait until delay() is completed. </p><p>what do you think? Is there any idea to overcome? your comment is appriciated. </p>
<p>Redusing the delay would allow near realtime multiple sensor activation. Using interrupts would allow multiple reads very quickly. Larger Atmel chips have more interrupts available.</p>
<p>Thanks for your comments and suggestion.</p><p>yeah short dealy() is good for another trigger. but usually we need one sensor activates and stay some time for light up. </p><p>however, by this time if another motion is occurred, the other sensor should be activated. </p>
<p>Instead of a delay statement, set a variable when motion is detected. every time through the loop, check that variable. if it's high, keep light on, if it's low, turn light off. No blocking that way, and can have several sensors that won't interfere with each other.</p>
<p>I have used 220V ac instead of 120V ac. The LED burnt out each time I turned on the voltage. Can you tell me why this is happening? Can I build the circuit without an SSR? </p><p>The specification olf the SSR i used is: 40VA</p><p>24~380VAC across pins 1&amp;2</p><p>500Kohm/0.25W across pins 3&amp;4</p>
Why are you using a 500k across pins 3 &amp; 4? Those pins are for direct 5v control from the Arduino.<br><br>Since you are using 220vac on the primary side, You have to use 220vac lights. Yes, you have to use a SSR because an arduino is 5vdc only, and would fail spectacularly at 220vac. You could use a arduino relay board instead of the ssr.
hi i am having problem with connecting the spdt swicth it has 6 pins but in the given circuit you have used 4 pins only , here i m confused please help me
<p>6 pins is a DPDT switch, just use one side of it. Leave the other 3 empty.</p>
<p>how do i use embedded atmel chip</p>
See http://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/
<p>how do i use embedded atmel chip</p>
see http://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/
<p>how will connect atmel chip if i dont use arduino</p>
Sir, I'm about to use your creative work as my project. I just wanted to ask, we already have the PIR sensor, what's the use of the LDR??
The LDR deactivates the lights during daylight hours in a security lighting application. The lights only come on from dusk to dawn.
Hi, are you selling this or any place that I can buy this?
I'm not selling it, but there are parts links in the instructable.
Sir, I'm about to use your creative work as my project. I just wanted to ask, we already have the PIR sensor, what's the use of the LDR??
Sir, I'm about to use your creative work as my project. I just wanted to ask, we already have the PIR sensor, what's the use of the LDR??
<p>Thanks! I understand now how it all works. This is great! I just have one more question though. Can the arduino take 4 motion sensors (like the ones in this tutorial) and 4 SSr's relays? my concern is the 5v, if it will be OK to handle all that. I think I could use just the two switches to control on/off the whole thing....</p>
there's no problem supporting 4 motion sensors and 4 ssr's. The ssr's are just a LED connected to the Arduino.
<p>Stupid but quick question. The diagram shows two +5vdc, but the Arduino uno only has 1 (?). Does this mean the switch (center pin) and the motion sensor share the same +5vdc pin in the Arduino? (does it matter which order you connect the three motion sensor points into the arduino too?)</p>
yes, they share the same +5vdc. the motion sensor typically has three pins, one is ground, one is 5v. the out has to connect to whatever pin you specify in code.
<p>does this PIR sensor work? </p><p><a href="http://www.amazon.com/gp/product/B007XQRKD4/ref=ox_sc_act_title_8?ie=UTF8&psc=1&smid=A2JO7YP9I9Y3D6" rel="nofollow">http://www.amazon.com/gp/product/B007XQRKD4/ref=ox_sc_act_title_8?ie=UTF8&amp;psc=1&amp;smid=A2JO7YP9I9Y3D6</a></p>
That should work fine.
<p>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.</p>
<p>We are using pin 2 for the data connection of our sensor, add a second sensor to pin 3.</p><p>1. in the code, add (in the appropriate places):</p><p>int pirPin2 = 3;</p><p>pinMode(pirPin2, INPUT);</p><p>motionDetect2 = digitalRead(pirPin2);</p><p>2. and replace</p><p>if (motionDetect == HIGH)</p><p>with</p><p>if (motionDetect == HIGH || motionDetect2 == HIGH)</p>
<p>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??</p>
<p>Right after the existing lines that match:</p><p>after</p><p>int pirPin = 2;</p><p>put</p><p>int pirPin2 = 3;</p><p>-----</p><p>after</p><p>pinMode(pirPin, INPUT);</p><p>put</p><p>pinMode(pirPin2, INPUT);</p><p>----</p><p>after</p><p>motionDetect = digitalRead(pirPin);</p><p>put</p><p>motionDetect2 = digitalRead(pirPin2);</p>
<p>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.</p>
<p>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).</p>
<p>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?? </p><p>And where I have to add the LDR coding in the main code??</p><p>Pls help...</p>
<p>The code is not currently written to restart the timer if a new event is seen, but it could easily be made to.</p>
<p>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??</p><p>And what will be the code to restart the timer if a new event is seen??</p><p>Thank you sir...</p>
<p>Gnd is not shorted to 11 &amp; 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.</p>
<p>ok sir. I will try. Thank you for spending some time to answering for my clarifications. :-)</p>
<p>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: </p><p>digitalWrite (ssrPin, LOW);</p><p>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.</p>

About This Instructable




Bio: Professionally, I'm an IT Engineer (Executive Level) and Electronics Tech. I'm a Amateur Radio Operator (KK4HFJ). I lived off grid, with Solar (PV ... More »
More by sspence:DIY I2C LCD Display with InputsRaspberry Pi & the Neo 6M GPSEarthquake / Vibration Sensor
Add instructable to: