Night Light Smart Motion Sensor Using Arduino
by 1005172 in Circuits > Arduino
8 Views, 0 Favorites, 0 Comments
Night Light Smart Motion Sensor Using Arduino
My circuit is a Night Light motion sensor, which is used to detect if there is any motion during the dark. The LDR detects if there is light or not, and the PIR sensor detects if there is any motion in the dark. So if you cover your hand over the LDR, and then wave your other hand or just keep it slightly above the PIR sensor, the RGB LED should turn from OFF to blue.
Supplies
- Breadboard
- Normal LED
- RGB LED
- PIR sensor
- LDR sensor
- Bunch of jumper wires
- Arduino
- Computer (to connect Arduino)
- Pushbutton
- Four 330 ohm resistors
- Two 10k ohm resistors
POWERING THE BREADBOARD
Arduino 5V → + rail
Arduino GND → – rail
PLACING THE LDR ON THE BREADBOARD
Placement
- LDR legs → b15 and b17
Connections
- + rail → a15
- c17 → Arduino A0
- 10kΩ resistor:
- d17 → d20
- e20 → – rail
PLACING THE PIR SENSOR
Placement
- VCC → b25
- OUT → b26
- GND → b27
Connections
- + rail → a25
- – rail → a27
- c26 → Arduino D2
PLACING RGB LED
Placement
- Red → b35
- Long (+) → b36
- Green → b37
- Blue → b38
Positive
- b36 → + rail
RESISTORS TO RGB CONNECTION
RED Leg
- 330Ω resistor:
- d35 → d40
- e40 → Arduino D10
GREEN Leg
- 330Ω resistor:
- d37 → d42
- e42 → Arduino D11
Blue Leg
- 330Ω resistor:
- d38 → d44
- e44 → Arduino D12
PUSHBUTTON
Placement (across island)
- e45 / f45
- e47 / f47
Connections
- + rail → a45
- a47 → Arduino D4
- 10kΩ resistor:
- d47 → – rail
CODE
// ===== PIN DEFINITIONS =====
int ldrPin = A0;
int pirPin = 2;
int buttonPin = 4;
int ledPin = 9;
int redPin = 10;
int greenPin = 11;
int bluePin = 12;
bool manualMode = false;
// ===== SETUP =====
void setup() {
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void setRed() {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
}
void setBlue() {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}
// ===== MAIN LOOP =====
void loop() {
int lightLevel = analogRead(ldrPin);
int motion = digitalRead(pirPin);
// Button press toggles mode
static bool lastButtonState = LOW;
bool currentButtonState = digitalRead(buttonPin);
if (currentButtonState == HIGH && lastButtonState == LOW) {
manualMode = !manualMode; // toggle mode
delay(200); // debounce
}
lastButtonState = currentButtonState;
if (manualMode) {
turnEverythingOff();
setRed(); // RED = Manual Mode
}
else if (motion == HIGH && lightLevel < 400) {
digitalWrite(ledPin, HIGH);
setBlue(); // BLUE = Auto Mode active
}
else {
turnEverythingOff();
}
}
void turnEverythingOff() {
digitalWrite(ledPin, LOW);
// Common anode RGB LED
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
}
SCHEMATIC/CIRCUIT DIAGRAM
THIS IS THE DIAGRAM FOR THE CIRCUIT.
MISTAKES
My Mistakes:
- Trying to add too get too fancy with my circuit.
- LDR not sensing properly, so I had to take a new one.
- You have to wait 30-60 seconds for the PIR sensor to warm up.
- Taking too much time to figure out how to build my circuit, but then I broke it down all over again just to restart. DON'T DO THIS.