//The active wire is connected to Normally Open and common //**************************************************************************** int relayPin = 8; // 5V relay module attached to digital pin D8 of Arduino Uno int PIRInterrupt = 2;// PIR Motion Sensor attached to digital pin D2 of Arduino Uno int LDRPin = A0; // LDR pin attached to Analog pin A0 of Arduino Uno int LDRReading; // LDR value is stored on LDR reading int LDRThreshold = 300;// LDR Threshold value // Timer Variables: long lastDebounceTime = 0; long debounceDelay = 10000; volatile byte relayState = HIGH; void setup() { Serial.begin(115200);// Initialize serial communication at 115200 bps pinMode(relayPin, OUTPUT); // Set relayPin as output digitalWrite(relayPin, LOW);// By default set relayPin to HIGH pinMode(PIRInterrupt, INPUT);// PIR motion sensor set as an input // Triggers detectMotion function on rising mode to turn the relay on, if the condition is met attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING); } void loop() { // If 10 seconds have passed, the relay is turned off if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ digitalWrite(relayPin, LOW); relayState =HIGH; Serial.println("Lamp is OFF"); } delay(50); } void detectMotion() { Serial.println("Motion detected"); LDRReading = analogRead(LDRPin); Serial.print("Photocell reading = "); Serial.println(LDRReading); // LDR Reading value is printed on serial monitor - easy to get your LDRThreshold if(LDRReading > LDRThreshold){// Only turns the Relay on if the LDR reading is higher than the LDRThreshold if(relayState == HIGH){ digitalWrite(relayPin, HIGH); } relayState = LOW; Serial.println("Lamp is ON"); lastDebounceTime = millis(); } }