DIY Arduino PIR Motion Sensor Lighting & Security

196,952

187

89

Posted in TechnologyMicrocontrollers

Introduction: DIY Arduino PIR Motion Sensor Lighting & Security

About: 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), Wind, and veggie oil fueled diesel generator power for 6 y...

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.

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

Share

    Recommendations

    • Spotless Contest

      Spotless Contest
    • Microcontroller Contest

      Microcontroller Contest
    • Space Challenge

      Space Challenge
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    89 Comments

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

    1 reply

    Compiles fine for me. Please recheck your sketch.

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

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

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

    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.

    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!

    3 replies

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

    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

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

    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.

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

    1 reply

    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

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


    2 replies

    looks like you are missing a
    int motionDetect2= 0;

    right after

    int motionDetect= 0;

    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.