In this project you’re going to build a night security light with a relay module, a photoresistor and an Arduino.
A night security light only turns on when it’s dark and when movement is detected.
Here’s the main features of this project:
- the lamp turns on when it’s dark AND movement is detected;
- when movement is detected the lamp stays on for 10 seconds;
- when the lamp is ON and detects movement, it starts counting 10 seconds again;
- when there’s light, the lamp is turned off, even when motion is detected.
Recommended resources
The following resources include guides on how to use the relay module and the PIR motion sensor with the Arduino, which might be useful for this project.
Parts required
Here’s a complete list of the parts required for this project:
- Arduino UNO – read Best Arduino Starter Kits
- PIR Motion Sensor
- Photoresistor
- 10kOhm resistor
- Relay module
- Lamp cord set (view on eBay)
- Breadboard
- Jumper wires
Besides these electronics components, you also need an AC male socket, an AC wire and a lamp bulb holder (a lamp cord set). My lamp cord set is the one in the figure below.
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Code
Download or copy the following code to your Arduino IDE, and upload it to your Arduino board.
Warning: do not upload a new code to your Arduino board while your lamp is connected to the mains voltage. You should unplug the lamp from mains voltage, before upload a new sketch to your Arduino.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
// Relay pin is controlled with D8. The active wire is connected to Normally Closed and common
int relay = 8;
volatile byte relayState = LOW;
// PIR Motion Sensor is connected to D2.
int PIRInterrupt = 2;
// LDR pin is connected to Analog 0
int LDRPin = A0;
// LDR value is stored on LDR reading
int LDRReading;
// LDR Threshold value
int LDRThreshold = 300;
// Timer Variables
long lastDebounceTime = 0;
long debounceDelay = 10000;
void setup() {
// Pin for relay module set as output
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// PIR motion sensor set as an input
pinMode(PIRInterrupt, INPUT);
// Triggers detectMotion function on rising mode to turn the relay on, if the condition is met
attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);
// Serial communication for debugging purposes
Serial.begin(9600);
}
void loop() {
// If 10 seconds have passed, the relay is turned off
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){
digitalWrite(relay, HIGH);
relayState = LOW;
Serial.println("OFF");
}
delay(50);
}
void detectMotion() {
Serial.println("Motion");
LDRReading = analogRead(LDRPin);
// LDR Reading value is printed on serial monitor, useful to get your LDRThreshold
//Serial.println(LDRReading);
// Only turns the Relay on if the LDR reading is higher than the LDRThreshold
if(LDRReading > LDRThreshold){
if(relayState == LOW){
digitalWrite(relay, LOW);
}
relayState = HIGH;
Serial.println("ON");
lastDebounceTime = millis();
}
}
Schematics
Here’s the schematics for this project.
Note: if you have an earth (GND) connection in the mains voltage cable – a yellow and green cable – it should go outside the relay module, like the blue wire (neutral).
Demonstration
Here’s your circuit in action:
Wrapping up
In this project you’ve built a night security light with a photoresistor and a PIR motion sensor.
This is a great project to practice with relays and with the PIR motion sensor.
If you like Arduino projects, make sure you check our latest Arduino course: Arduino Step-by-step Projects – Build 25 Projects
Thanks for reading,
Sara Santos
This is a great project and the Arduino is perfect for prototyping but I bet you could run the sketch on an atTiny85 with just one or two more cheap components. I’ve done a couple of simple circuits this way and it makes for a nice, small form factor running the same code.
hi there,
can i use 1 channel relay module instead of 2 channel relay module for this project?
thanks.
Hi.
Yes, you can use that.
Regards,
Sara
Hello! This is great! I wonder how to modify the code so that I can control three or four relays with four PIR sensors?
Unfortunately at the moment I don’t have any tutorials on that exact subject…
If I want to run two PIR’s.
Can I run them in parallel,
or do I need to assign separate pin’s & alter the code.
TYIA
Hi Andrew.
By parallel, do you mean connecting both PIRs to the same GPIO?
I think that should work, although I’ve never tried that. I don’t know if it will conflict if motion is detected in both PIRs at the same time.
The best way to know is testing it.
Then, let me know the results.
Regards,
Sara
Thanks for the tutorial
The light always on, never turn off, could you please tell me why? and how to fix it?
Hi.
Check the wiring of the PIR motion sensor.
Also, check on the serial monitor if the PIR is being triggered correctly.
Additionally, check the wiring of your relay module.
Regards,
Sara
Hi,is it possible to replace led strip (rgb strip) or light bulb for the led you have used?If yes, how to do it?
hello Sara
i tried to run the code on ESP32, but the ESP keeps reseting, cause 2.
any ideea why?
thank you
Hi how can use an LDR module for this?
Hallo, I need a library of parts for the lamp and housing according to the picture, can you give it to me. I need it fast
this sketch does not work the ldr has no affect, I checked the wiring , the serial monitor, the exact code. The only ting that happens is the relay trips every ten seconds, I tried lowering the threshold value for the ldr and it was the same?