// Define the pins
const int LDRPin = 36; // LDR connected to A0
const int PIRPin = 2; // PIR sensor connected to D2
const int RelayPin =3; // Relay module connected to D3
// Threshold values
int lightThreshold = 500; // Adjust this based on your environment
int pirState = LOW; // Start with no motion detected
void setup() {
pinMode(RelayPin, OUTPUT);
pinMode(PIRPin, INPUT);
Serial.begin(9600); // For debugging
}
void loop() {
int lightLevel = analogRead(LDRPin);
int motionDetected = digitalRead(PIRPin);
Serial.print("Light Level: ");
Serial.println(lightLevel);
if (lightLevel < lightThreshold) { // If it's dark
if (motionDetected == HIGH) { // And motion is detected
digitalWrite(RelayPin, HIGH); // Turn on the light
pirState = HIGH;
}
} else { // If it's bright
digitalWrite(RelayPin, LOW); // Turn off the light
pirState = LOW;
}
delay(1000); // Wait 1 second before next loop
}