Using HC-SR501 Motion Sensor with relay and Arduino code
Using HC-SR501 Motion Sensor with relay and Arduino code
This video shows you how to control a relay when motion is detected. You can turn an alarm or AC bulb or any other load by detecting motion using Arduino and HC-SR501 motion sensor.
Arduino Source code for HC-SR501 Motion sensor to control AC bulb or load
/*
* This Arduino code uses HS-SR501 motion sensor and a relay
* and switches an alarm or a device using relay when motion is detected
* the device can be an alarm or light or any other device
*
* Written by Ahmad Shamshiri on August 05, 2018 at 12:36 for Robojax.com
* in Ajax, Ontario, Canada
* watch video instruction of this code: https://youtu.be/QQ7iDagQnQM
* This code has been downloaded from Robojax.com
*/
const int SENSOR_PIN = 2;// the Arduino pin connected to output (middle) wire of sensor
const int RELAY_PIN = 4;// the Arduino pin which is connected to control relay
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
Serial.println("Robojax Arduino Tutorial");
Serial.println("HC-SR501 sensor with relay");
pinMode(SENSOR_PIN, INPUT);// Define SENSOR_PIN as Input from sensor
pinMode(RELAY_PIN, OUTPUT);// Define RELAY_PIN as OUTPUT for relay
}
void loop() {
// Robojax.com HC-SR501 motion sensor tutorial Aug 05, 2018
int motion =digitalRead(SENSOR_PIN);// read the sensor pin and stores it in "motion" variable
// if motion is detected
if(motion){
Serial.println("Motion detected");
digitalWrite(RELAY_PIN, LOW);// Turn the relay ON
}else{
Serial.println("===nothing moves");
digitalWrite(RELAY_PIN,HIGH);// Turn the relay OFF
}
delay(500);
// Robojax.com HC-SR501 motion sensor tutorial Aug 05, 2018
}