How to use PIR (HC-SR501) Sensor with the Arduino




In this tutorial we will learn how to use PIR (HC-SR501) sensor with the Arduino.
Also, in this tutorial we will use Relay Module, Lamp, LED and Buzzer.

---

Hardware Required:

Arduino Board - https://goo.gl/Rqc5w2
HC-SR501 PIR - https://goo.gl/RyX4JA
5V 1 Way Relay - https://goo.gl/yAxcPL
LED Kit - https://goo.gl/B4GRIS
Buzzer - https://goo.gl/Y6m0Cg
F to F Jumper - https://goo.gl/TdGrkk
M to M Jumper - https://goo.gl/VRzUN4
Breadboard - https://goo.gl/08nnr1

Recomended Site - https://goo.gl/V9nYtx

---

Get the Code:
//define the pins
int LED = 4;
int PIR = 7;
int Buzzer = 2;

void setup() {
  //define the LED and Buzzer pin as output
  pinMode(LED, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  //define the sensor pin as input
  pinMode(PIR, INPUT);
}

void loop() {
  //using the digitalRead function we will read the signal of the sensor
  int value = digitalRead(PIR);
  //if its high or if an any object is detected it will activate the LED and Buzzer
  if (value == HIGH){
    digitalWrite(LED, HIGH);
    digitalWrite(Buzzer, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
    digitalWrite(Buzzer, LOW);
  }
}
//define the pins
int Relay = 4;
int PIR = 7;


void setup() {
  //define the Relay pin as output
  pinMode(Relay, OUTPUT);
  //define the sensor pin as input
  pinMode(PIR, INPUT);
}

void loop() {
  //using the digitalRead function we will read the signal of the sensor
  int value = digitalRead(PIR);
  //if its high or if an any object is detected it will activate the Relay Module
  if (value == HIGH){
    digitalWrite(Relay, LOW); //For activating the Relay we will send a LOW as the Relay input pin works inversely.
  }
}