// Pin Definitions
const int LDR_PIN = A0; // LDR connected to Analog Pin 0
const int PIR_PIN = 2; // PIR sensor connected to Digital Pin 2
const int LED_PIN = 9; // LED connected to PWM Pin 9
// Thresholds
const int DARK_THRESHOLD = 500; // Adjust based on your room lighting
int ldrValue = 0;
bool motionDetected = false;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
[Link](9600); // For debugging via Serial Monitor
}
void loop() {
ldrValue = analogRead(LDR_PIN);
motionDetected = digitalRead(PIR_PIN);
if (ldrValue > DARK_THRESHOLD) {
// It is NIGHT time
if (motionDetected) {
analogWrite(LED_PIN, 255); // 100% Brightness (Full Power)
} else {
analogWrite(LED_PIN, 50); // ~20% Brightness (Energy Saving Mode)
}
} else {
// It is DAY time
analogWrite(LED_PIN, 0); // Light OFF
}
delay(100); // Stability delay
}