Introduction: 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

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.

Step 2: SSR

Picture of 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

Picture of 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

Picture of 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

Picture of The Schematic

Here is the schematic that shows all the wiring:


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

Picture of 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.

http://arduinotronics.blogspot.com/2012/03/light-sensing-with-cds-ldr.html

Comments

JustinasS1 (author)2017-07-18

Hello, I get "stray '\302' in program" error :/ help

sspence (author)JustinasS12017-07-18

Compiles fine for me. Please recheck your sketch.

b6025138 (author)2017-06-29

Would this work with outdoor lighting?

sspence (author)b60251382017-06-29

Yes It will. It will work with any 120v load that matches the specs of the SSR used.

inder678 (author)2017-03-15

can I replace SSR with following one

http://www.amazon.in/Generic-Channel-Relay-Module-...

sspence (author)inder6782017-03-15

That's a relay, not a SSR, but yes, it would work.

inder678 (author)sspence2017-03-20

is there any change if i use relay instead of SSR?

sspence (author)inder6782017-03-23

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.

Faisal AliD (author)2016-10-28

please send me its video

sspence (author)Faisal AliD2016-10-28

what video?

sifu-dianji (author)2016-08-12

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!

sspence (author)sifu-dianji2016-08-12

Don't use delay statements, use a millis timer.

sifu-dianji (author)sspence2016-08-12

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

Connor1186 (author)sifu-dianji2016-09-13

Remove the Delaytime (tx) pot and figure out if it needs more or less resistance to have virtually no length to the high time.

sspence (author)2015-12-16

I gave you hints, and expected you to put some work into it yourself. I was too busy to rewrite the code or explain it all.

data_2 (author)2015-12-14

Still no 2nd (need 4th and 5th) pir

sspence (author)data_22015-12-14

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

data_2 (author)2015-12-10

code for 2 pirs??????? see comment

(sspence

(author)


sriramsrikp



2 years ago)

Will not upload why? I am able to have 1 pir working. But the code for the 2nd will not upload Something about

"motionDetect2 = digitalRead(pirPin2);"

-------------------------------------------------------------------------------------------------------------
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 pirPin2 = 3;


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 (motionDetect == HIGH || motionDetect2 == HIGH)


{
motionDetect = digitalRead(pirPin);

motionDetect2 = digitalRead(pirPin2);


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);
}
}


sspence (author)data_22015-12-11

looks like you are missing a
int motionDetect2= 0;

right after

int motionDetect= 0;

data_2 (author)sspence2015-12-11

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.

data_2 (author)data_22015-12-11

this is the code at this time. but pir do not work (switch on arduino will make light blink)

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

int ssrPin = 13;
int pirPin = 2; // PIR Data out
int pirPin2 = 3;
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);
motionDetect2 = digitalRead(pirPin2);
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);
}
}

krishna.cool.94849 (author)2015-11-10

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.

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?

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.

Thats cool .........thanks for the info!

fordjoe (author)2014-09-09

Is it safe to use this 25A SSR ( http://amzn.to/1rVuILv ) if I am only using it to control a 4 Amp vacuum?

sspence (author)fordjoe2014-09-09

Sure. It's rated for a max of 25 amps. It will control anything less than that.

hasnatk (author)sspence2014-09-16

Is it possible to activate two sensor at the same time?

i think delay(---) is the main problem.

because triggering two sensor at the same time by human motion is impossible.

if one sensor goes to high then wait until delay() is completed.

what do you think? Is there any idea to overcome? your comment is appriciated.

sspence (author)hasnatk2014-09-16

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.

hasnatk (author)sspence2014-09-17

Thanks for your comments and suggestion.

yeah short dealy() is good for another trigger. but usually we need one sensor activates and stay some time for light up.

however, by this time if another motion is occurred, the other sensor should be activated.

sspence (author)hasnatk2015-11-11

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.

HaripriyaR (author)2015-07-07

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?

The specification olf the SSR i used is: 40VA

24~380VAC across pins 1&2

500Kohm/0.25W across pins 3&4

sspence (author)HaripriyaR2015-07-08

Why are you using a 500k across pins 3 & 4? Those pins are for direct 5v control from the Arduino.

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.

ankur12543 (author)sspence2015-10-04

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

sspence (author)ankur125432015-10-04

6 pins is a DPDT switch, just use one side of it. Leave the other 3 empty.

grace wambui (author)2015-06-23

how do i use embedded atmel chip

sspence (author)grace wambui2015-06-23

See https://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/

grace wambui (author)2015-06-23

how do i use embedded atmel chip

sspence (author)grace wambui2015-06-23

see https://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/

grace wambui (author)2015-06-23

how will connect atmel chip if i dont use arduino

myl123 (author)2015-03-04

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

sspence (author)myl1232015-03-31

The LDR deactivates the lights during daylight hours in a security lighting application. The lights only come on from dusk to dawn.

monloi.perez (author)2015-03-09

Hi, are you selling this or any place that I can buy this?

sspence (author)monloi.perez2015-03-31

I'm not selling it, but there are parts links in the instructable.

myl123 (author)2015-03-04

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

myl123 (author)2015-03-04

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

Hermes.Mauricio.Duarte (author)2015-01-15

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

there's no problem supporting 4 motion sensors and 4 ssr's. The ssr's are just a LED connected to the Arduino.

Hermes.Mauricio.Duarte (author)2015-01-13

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

About This Instructable

189,961views

186favorites

License:

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: