const int pirPin = 2; // PIR sensor pin (Motion Detector)
const int ldrPin = A0; // LDR sensor pin (Light Sensor)
const int relayPin = 3; // Relay pin (Controls Light)
const int lightThreshold = 400; // Light level to determine brightness
void setup() {
pinMode(pirPin, INPUT); // PIR sensor as input
pinMode(relayPin, OUTPUT); // Relay as output
Serial.begin(9600); // Start Serial Monitor
}
void loop() {
int pirValue = digitalRead(pirPin); // Read motion sensor (PIR)
int ldrValue = analogRead(ldrPin); // Read light sensor (LDR)
// Check if the room is dark
bool isDark = (ldrValue < lightThreshold);
// Check if motion is detected
bool motionDetected = (pirValue == HIGH);
// Light should turn ON only if it's dark and motion is detected
bool lightOn = (motionDetected && isDark);
// Control the relay (turn light ON/OFF)
digitalWrite(relayPin, lightOn ? HIGH : LOW);
// Print status in a clean format
Serial.print("LDR Value: "); Serial.print(ldrValue);
Serial.print(isDark ? " (Low Light)" : " (Enough Light)");
Serial.print(" | PIR: "); Serial.print(motionDetected ? "Motion Detected" : "No Motion Detected");
Serial.print(" | Light "); Serial.println(lightOn ? "ON" : "OFF");
delay(1000); // Wait 1 second before repeating
}