DIY Arduino PIR Motion Sensor Lighting & Security

Picture of DIY Arduino PIR Motion Sensor Lighting & Security
From the minds at

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

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

We obtained our PIR Sensor from Amazon.

Connect the 3 pins of the PIR to +5vdc, Arduino Pin 2 (data out), and Ground.
Soulfien11 hours 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)  Soulfien11 hours ago
Awesome. Thank you.
Oliverhall21 days ago

Nice tutorial!

You can get the hc-sr501 PIR motion sensor for cheap here:

MadridFC3 months ago
My SSR operates when the DC volatge is i even if my arduino gives a high the ssr doesnt close the switch and my lamp doesnt glow.what to do?
sspence (author)  MadridFC3 months ago
SSR's don't switch DC, They switch AC. You need a MOSFET.
angpal596 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)  angpal596 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.
Ronot600010 months 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)  Ronot600010 months 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)  sspence10 months ago
Ronot600010 months 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)  Ronot600010 months 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.
Ronot600010 months 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)  Ronot600010 months 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)  sspence10 months 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
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.

Get More Out of Instructables

Already have an Account?


PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!