LED Night Light With PIR and LDR Sensors
by 756217 in Circuits > Arduino
795 Views, 1 Favorites, 0 Comments
LED Night Light With PIR and LDR Sensors
This LED night light has many implications in everyday life such as a night light for the elderly who may have trouble seeing in the dark, During my research I found similar projects with an LDR but not containing a PIR sensor, so I decided to give it a go.
This LED night light project including a PIR and LDR sensor can detect the difference between day and night, It also utilizes a motion sensor to detect movement which then turns on the LED.
Supplies
Wiring Components
The most essential part of the project is wiring the components necessary for the project to even work
1, PIR: connect the VCC pin to power on the breadboard and the GND pin to ground, then connect the output pin to Arduino pin 10
2, LDR: connect one pin of the LDR to a 330 ohm resistor which then goes to power, the other pin should go directly to ground, Connect a wire where the LDR meets the resistor to Arduino analog pin 0
3, LED: Provide ground to both LED cathode pins which are shorter (negative), and connect a resistor to both anode (positive) pins which are connected to a wire which meets a wire providing power connected to Arduino Uno pin 9 ( to control LEDs at the same time using the same code)
4, Connect GND to ground on the Arduino as 5v to power
Code
The code is equally as important for the successful operation of the project:
Void setup is the initial code run when the Arduino is turned off and on which initializes necessary settings to run your code such as declaring inputs and outputs. serial begin declares the speed at which data travels (bits)a
// Code for LED Night Light
This is where you established the pins for the components used in the project
v const int ldrPin = A0; // LDR pin
const int pirPin = 10; // PIR sensor pin
const int ledPin = 9; // LED pin
Now we create a variable called night threshold and define the amount
// Define a threshold for night
const int nightThreshold = 900;
Now we define the rate at which data is transmitted to the serial monitor
void setup() {
// Initialize data travel speed at 9600 bits per second standard
Serial.begin(9600);
Defining our components as inputs our outputs
// Set pin modes
pinMode(ldrPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
}
Our main code that makes the project work and loops on repeat :
void loop() {
// Read the LDR value
int ldrValue = analogRead(ldrPin); Telling the Arduino to read the LDR value
// Print LDR value for debugging
Serial.print("Ldr Value: "); telling the Arduino to display data from LDR to Monitor
Serial.println(ldrValue);
// Check if it's night (LDR value below the night threshold)
if (ldrValue > nightThreshold) { Telling the Arduino to check if the number the LDR is detecting
// Read PIR sensor is above the threshold variable we made earlier and if so to read
int pirValue = digitalRead(pirPin); the PIR sensor for motion
// Print PIR value for debugging
Serial.print("Pir Value: "); Telling the Arduino to display the PIR value to the serial monitor
Serial.println(pirValue);
// If PIR sensor is HIGH, turn on the LEDs Telling the the Arduino if the PIR detects motion turn on the LEDS
if (pirValue == HIGH) { and display it onto the monitor, and if not the LEDs should stay
digitalWrite(ledPin, HIGH); // Turn on LEDs OFF and display No motion detected LEDS OFF to monitor
Serial.println("Motion Detected Leds ON");
} else {
digitalWrite(ledPin, LOW); // Turn off LEDs
Serial.println("No motion detected Leds OFF");
}
} else {
digitalWrite(ledPin, LOW); // Turn off LEDs This code correlates with the first part of the code if the LDR
Serial.println("Daytime LEDs OFF"); detects day instead of light the LEDS stay off and display onto the
} Monitor
}
Downloads
Upload
Upload your code from the Arduino application to the Arduino via a wire and you're good to go!
Above is a video on the working night light