/* * Motion Activated Relay Switch * * This script will switch on/off power to a device via a relay module. * The Arduino will be motion activated using a PIR sensor * so when motion is not detected for a defined amount of time the Arduino will * switch off the relay port to power 'off' the device. When motion is detected * again the power will be switched 'on' to turn on the device. * * The Arduino output pin goes to HIGH if motion is detected. When there is no motion * detected we do not want to switch this off right away but wait for the timeout * period and then switch the power off. To ensure this does * not power off unexpectedly this program will ignoring 'No Motion' phases shorter * than a defined timeout. If no motion has been detected for longer than the defined * timeout it will switch off (LOW) the replay port(s). * * @author: Chris Weatherford / cweatherford (_) gmail (_) com * @date: Dec 2017 * @Original author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at * @Origianl code: https://playground.arduino.cc/Code/PIRsense * * kr1 (cleft) 2006 * released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license * http://creativecommons.org/licenses/by-nc-sa/2.0/de/ */ /* ****************************************** *** SET TIMEOUT *** *** defined shut off period after no *** *** motion is detected (in minutes) *** ****************************************** */ int timeout = 30; //the time we give the sensor to calibrate (10-60 secs according to the datasheet) int calibrationTime = 30; //the time when the sensor outputs a low impulse long unsigned int lowIn; //convert timeout to milliseconds long unsigned int intNoMovementTimeout = timeout * 60000; //[timeout minutes defined above] * [60000 milliseconds in a minute] boolean RelayOff = true; boolean getLowTime; int pirPin = 3; //the digital pin connected to the PIR sensor's output int relayPin = 8; //the digital pin connected to the relay module void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(relayPin, OUTPUT); digitalWrite(pirPin, LOW); //give the sensor some time to calibrate Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } void loop(){ if(digitalRead(pirPin) == HIGH){ //if motion is detected if(RelayOff){ //and if the Relay is switched OFF //makes sure we wait for a transition to LOW before any further output is made: RelayOff = false; //turn off the 'Relay Off' variable digitalWrite(relayPin, HIGH); //set the state of the relay pin to 'ON' Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); Serial.println("motion was detected while relay was switched OFF; now haved switched ON"); delay(50); //pause for X milliseconds } else { Serial.println("motion detected but the relay is already switched ON"); delay(5000); } getLowTime = true; //turn on the 'Get Low Time' variable } if(digitalRead(pirPin) == LOW){ //When PIR first detects no movement record the time if(getLowTime){ lowIn = millis(); //save the time of the transition from high to LOW getLowTime = false; //make sure this is only done at the start of a LOW phase Serial.print("Recording time when 'no motion' was first detected - "); //output Serial.println(lowIn / 1000); } //Only turn off relay if the PIR Sensor has seen no movement for more than the defined 'No Movement Timeout' time if(!RelayOff && millis() - lowIn > intNoMovementTimeout){ RelayOff = true; //turn on the 'Relay Off' variable digitalWrite(relayPin, LOW); //set the state of the relay pin to 'OFF' Serial.print("Turning off relay now. Motion ended at "); //output Serial.print((millis() - intNoMovementTimeout)/1000); Serial.println(" sec"); delay(50); //pause for X milliseconds } } }