PIR Motion Sensor with Arduino Uno: hc-sr501 Wiring, Setup, and Coding

3
(1)

Last Updated on 13th April 2024 by peppe8o

In this tutorial, I’m going to show you how to connect and setup Arduino Uno with a PIR sensor, Also examining its pinout, working, convention, and working protocol.

Detecting motion inside a monitoring area, such as security area or home restricted place, requires monitoring all the object moves within thas perimeter. The PIR sensor (HC-SR501) with Arduino is a good and cheap option, as it has ability to measure up to 7 meters

PIR Motion Sensor Description

PIR sensor is used for security purposes to detect unauthorized persons or objects. There are two sensors inside the PIR sensor known as pyroelectric sensors which work on the heat energy changings, These two sensors sit adjacent to one another, and when the sign differential between the two sensors changes like assuming that an individual goes into the room or any object comes in the path. In front of the sensor is lenses housing which increases the sensing area.

PIR Motion Sensor Features

PIR sensor works on the basis of heat change: when the object is coming in front of it, the heat change is detected, then the PIR sensor send HIGH logic to the output pin. This sensor is normally used for lockers, unauthorized areas and home security. These are important features of the PIR Motion sensor, which includes its distancing, rays type and functionality description.

  • The detecting distance of this sensor is from 25cm to 10 meters.
  • Infrared radiations detected by heat energy change
  • Detection Delay time adjusting from the Potentiometer on the backside of the sensor
  • TTL logic as the output

What We Need

Arduino Uno R3 board

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with the following links:

Amazon Arduino Uno box
Amazon PIR HC-SR501 box
Amazon Breadboard box
Amazon Dupont Wiring box

Step-by-Step Procedure

PIR Sensor contains 3 pins, which are VCC, Input pin and ground. In the connection diagram, you can see that GND is connected with Pin 1, the second pin is connected to Pin 2 and the third pin is connected to the VCC +5 volts. The output of the PIR sensor is HIGH and LOW which means it has zero or 5 volts.

Wiring Diagram

Please connect the PIR sensor to Arduino according to the following picture:

Wiring Diagram

Please find below the connection table:

PIN No.PIN NamePurpose
1.GroundGround PIN >> Connect to 0V or GND
2.VoutOutput >> Connect with an Digital GPIO PIN
3.VCCVoltage PIN >> Connect to +5V
Connection table

Get my PIR sensor Code

Connect your PC to Arduino and open Arduino IDE. For the very first steps, you can refer to Connecting Windows PC with Arduino tutorial. Please download my PIR sensor code, by using the following link:

pirarduino.zip

Extract it and open the .ino file in your Arduino IDE.

Code Explanation

PIR sensor pin is defined as a global variable and in the setup function, we set the baud rate at 9600 to initialise the serial monitor. This sets the transmission rate with our serial monitor so that we can display the performed measurements. PIR sensor pin is set as input for the microcontroller, LED is included in the code to turn on the LED when our sensor detects motion.

int pirPin = 2; // Sensor input pin
int ledPin = 13; // LED output pin
void setup() {
  Serial.begin(9600); // Serial Monitor Initialized
  pinMode(pirPin, INPUT); // PIR sensor pin set as input
  pinMode(ledPin, OUTPUT); //LED pin set as output
  digitalWrite(pirPin, LOW); // PIR sensor set low
  digitalWrite(ledPin, LOW); // LED OFF
}

In the following section, the PIR pin is read continuously. When an object comes then this pin becomes HIGH, which we detect as the object motion. This turns on the LED and shows on the serial monitor an “Object Detected” warning.

If no object is detected, then the LED remains OFF and the display on Serial Monitor will show the message “NO Object”.

void loop() {
  if (digitalRead(pirPin) == HIGH) // Object Detection Condition
  { digitalWrite(ledPin, HIGH); // LED ONN
    Serial.println("Object Detected"); // Print on Serial Monitor
  }
  else {
    Serial.println("NO Object");  // Print on Serial Monitor
    digitalWrite(ledPin, LOW); // LED OFF
  }}

Results and Discussion

In the following picture, you can see that the serial monitor is showing no object when the sensor does not detect any object. LED is OFF as LED works only when an object is detected.

Arduino PIR HC-SR501 no object detected

In this picture, the object is detected, which makes the LED turn ON, also displaying on the serial monitor “OBJECT DETECTED”.

Arduino PIR HC-SR501 object detected

Enjoy!

Please find more tutorials on Arduino in peppe8o Arduino archives.

Umar Jamil

For any queries and help for work, please contact me at:
Whatsapp: +92-346-661-7017
Email: [email protected]

How useful was this post?

Click on a star to rate it anonymously!

Average rating 3 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?