Devices & Components
Arduino Uno Rev3
LDR, 5 Mohm
Jumper wires (generic)
Breadboard (generic)
Resistor 10k ohm
Relay (generic)
Hardware & Tools
Multitool, Screwdriver
Software & Tools
Arduino IDE
Project description
Code
Code
arduino
Code
1int relay = 8; 2volatile byte relayState = LOW; 3 4int PIRInterrupt = 2; 5 6// LDR pin is connected to Analog 0 7int LDRPin = A0; 8// LDR value is stored on LDR reading 9int LDRReading; 10// LDR Threshold value 11int LDRThreshold = 300; 12 13// Timer Variables 14long lastDebounceTime = 0; 15long debounceDelay = 10000; 16 17void setup() { 18 // Pin for relay module set as output 19 pinMode(relay, OUTPUT); 20 digitalWrite(relay, HIGH); 21 // PIR motion sensor set as an input 22 pinMode(PIRInterrupt, INPUT); 23 24 attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING); 25 // Serial communication for debugging purposes 26 Serial.begin(9600); 27} 28 29void loop() { 30 // If 10 seconds have passed, the relay is turned off 31 if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ 32 digitalWrite(relay, HIGH); 33 relayState = LOW; 34 Serial.println("OFF"); 35 } 36 delay(50); 37} 38 39void detectMotion() { 40 Serial.println("Motion"); 41 LDRReading = analogRead(LDRPin); 42 43 if(LDRReading > LDRThreshold){ 44 if(relayState == LOW){ 45 digitalWrite(relay, LOW); 46 } 47 relayState = HIGH; 48 Serial.println("ON"); 49 lastDebounceTime = millis(); 50 } 51}
Downloadable files
Schematics
Schematics
Schematics

Schematics
Schematics
Schematics

Comments
Only logged in users can leave comments