Introduction: PIR With Light Sensor

Simple PIR motion detector with light sensor & delay timer.

The circuit uses a PIR module ( about £1.00 from ebay, an LDR (about 25p, again from ebay & an Arduino mini pro.

Upon reset, the ambient light is measured and, for test purposes only, the resulting atod output is shown on the serial monitor. The output is constantly measured until the PIR senses movement.

When the light falls below a predetermined level, (in this case a value of 60 - variable name now-dark) and movement is detected, Pin 13, which will be connected to a row of LEDs goes high, and a counter (timercnt) is decremented every second. Again, for test purposes only, the decrementing counter is displayed on the serial monitor.

Each time movement is detected, the counter is reset to its initial value - in this case 10. I will set this to 180 seconds when installed. The lights will stay on for 180 seconds provided there is no movement detected.

The variable 'ledon' is set to 1 when the lights are on, thus preventing the light from the row of LEDs affecting the circuit. This is only reset to 0 when the counter reaches 0 due to no movement detected.

I hope that all makes sense and is of some use.

Here is the Arduino code - not the tidiest (copy & paste not too clever!), but it works.

//*********************************************************************************

//T.Reynolds - November 2014
//motion detector with light sensor & timer

const int ledPin= 13; // output to LEDs / Realy

const int inputPin= 4;

int now_dark =60; // When light measurement falls below this level, check for movement.

int ledon =0; //When set, bypasses LDR so circuit is unnaffected

// by the lights it has just switched on!

// Resets when timercnt=0, i.e. no movement detected.

int analogPin = 3; // 22k resistor (or similar) to analog pin 3 and Gnd

// LDR pin 3 and +5V

int val = 0; // value read on analog pin 3

int timercnt;

void setup()

{

Serial.begin(9600); // test only

pinMode(ledPin, OUTPUT);

pinMode(inputPin, INPUT); }

void loop()

{ int value= digitalRead(inputPin);

if (ledon==0)

{

val = analogRead(analogPin);

delay(1000); // test only

Serial.println(val); // test only

}

if (value == HIGH && val < now_dark || value == HIGH && ledon==1)

{

digitalWrite(ledPin, HIGH);

timercnt=10; // Count down from here - 10 secs is test only

// Lights will stay on for 10 secs if no movement detected.

// Each time movement detected, timercnt set to 10.

}

if (value==LOW && timercnt >0)

{

Serial.print(">"); // test only

Serial.println(timercnt); //test only

ledon=1;

delay(1000); timercnt--;

if (timercnt ==0)

{

turnoff();

ledon=0;

}

}

}

void turnoff()

{

digitalWrite(ledPin, LOW);

}

// ====================================================