// Define pins
int PIR = 13; // PIR sensor pin
int LDR = A0; // LDR sensor pin (analog input)
int R = 6; // Red LED pin
int G = 5; // Green LED pin
int B = 3; // Blue LED pin
// Threshold for the LDR sensor (adjust based on environment)
int ldrThreshold = 632; // Higher value means it's darker
void setup() {
// Initialize the Serial Monitor
Serial.begin(115200);
// Set PIR pin as input
pinMode(PIR, INPUT);
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
// Turn off the RGB LED initially
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
}
void loop() {
// Read PIR sensor value
int movement = digitalRead(PIR);
// Read LDR sensor value (analog)
int ldrValue = analogRead(LDR);
// Only proceed if motion is detected
if (movement == HIGH) {
// Check if LDR detects low light (above the threshold means it's dark)
if (ldrValue > ldrThreshold) {
// Turn on the RGB LED to white (red, green, blue all HIGH)
digitalWrite(R, HIGH);
digitalWrite(G, HIGH);
digitalWrite(B, HIGH);
Serial.println("Motion detected and it's dark - turning on the light.");
} else {
// It's not dark, so keep the LED off
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
Serial.println("Motion detected but enough light - light off.");
}
} else {
// No motion detected, keep the LED off
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
Serial.println("No motion - light off.");
}
// Small delay to avoid excessive readings
delay(500);
}