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.

Step 2: SSR

A SSR is a solid state relay. It consists of a photo transistor and a triac, along with supporting circuitry. This isolates the 120vac load from the Arduino, so no damage from the high voltage AC can happen.

For background info on SSR's see http://en.wikipedia.org/wiki/Solid_state_relay

We obtained our SSR from Amazon.

Connect Screw Terminal 4 to Ground, Screw Terminal 3 to Arduino Pin 13, and Screw terminals 1 & 2 insert in Series (this is important) with the HOT wire going to your appliance (make sure your cord is unplugged or breaker is off if hardwiring).

Step 3: Manual Control Switch

We wanted a way to override the PIR, and offer a Automatic (PIR), Manual ON, and Manual OFF mode. We added a SPDT Switch, with center off, and connected the center pin to +5vdc, and the two outer pins to Arduino pins 11 & 12. The two outer pins also have a 10k ohm resistor (each) to Ground.

Radio Shack carries a 5 pack of resistors for $1.20 or so.

We obtained the SPDT w/ Center Off switch from Amazon.

For more info on SPDT and other types of switches, see http://en.wikipedia.org/wiki/Single_pole,_double_throw#Contact_terminology

Step 4: The Arduino Code

The code that makes this all happen is as follows:

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
     digitalWrite(ssrPin, LOW);
else if (manualSwitch == HIGH) // Manual On
  digitalWrite(ssrPin, HIGH);
else // Manual Off

  digitalWrite(ssrPin, LOW);

Step 5: The Schematic

Here is the schematic that shows all the wiring:

Step 6: Using a Light Sensor (LDR/CdS)

One option is to prevent the lights from coming on if the ambient light levels are over a certain amount. This is the typical operation of an outdoor motion sensor. If the sun is up, the lights do not come on. This CdS tutorial will get you started on adding that option. If the reading is over a certain amount, inhibit the light on function.

<p>can I replace SSR with following one</p><p></p><p><a href="http://www.amazon.in/Generic-Channel-Relay-Module-Electronic/dp/B00C59NOHK/ref=sr_1_1?ie=UTF8&qid=1489573766&sr=8-1&keywords=5v+relay+module+220v" rel="nofollow">http://www.amazon.in/Generic-Channel-Relay-Module-...</a></p>
<p>That's a relay, not a SSR, but yes, it would work.</p>
<p>is there any change if i use relay instead of SSR?</p>
<p>if the relay module can triger with 5v and 20ma, no changes necessary. If it has a higher coil voltage or current needs, then you will need a transistor to trigger the relay.</p>
<p>please send me its video</p>
what video?
<p>anyone here know if you can get/make a real time (ie. no time delay) PIR? I'm trying to make a robotic hexapod that tracks movement. the code and body are already working, but the 2 second delay makes it move erraticaly- spining round and round and charging forwards into walls. thanks for any suggestions!</p>
Don't use delay statements, use a millis timer.
<p>sorry if this was badly worded, but my problem is that the PIR itself, not the arduino's code, goes to +5v for 2 seconds when it senses movement- are there models available that don't do this? all of the ones I have seen on ebay/amazon do this</p>
<p>Remove the Delaytime (tx) pot and figure out if it needs more or less resistance to have virtually no length to the high time.</p>
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="https://www.instructables.com/member/sspence" rel="nofollow">sspence</a><br> <br> (author)<br> <br> <br> <a href="https://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 https://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/
<p>how do i use embedded atmel chip</p>
see https://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>

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 Inputs Raspberry Pi & the Neo 6M GPS Earthquake / Vibration Sensor 
Add instructable to: