Interfacing HC-SR501 PIR Motion Sensor with Arduino

1,050 views

Introduction

The HC-SR501 PIR (Passive Infrared) motion sensor is a widely used component in various projects related to motion detection and automation. It is a simple yet highly sensitive device that can detect even the slightest movement in its range.

In this article, we will explore how to interface the “HC-SR501 PIR motion sensor” with Arduino, a popular microcontroller platform. We will discuss the basic working principle of the sensor, its components, and how to connect it to the Arduino board. Additionally, we will demonstrate how to program the Arduino to receive and process the sensor data, and finally, we will showcase a simple project that utilizes this interfacing technique.

What is HC-SR501 PIR Motion Sensor?

The HC-SR501 PIR (Passive Infrared) motion sensor is a commonly used electronic component in various projects related to motion detection and automation. The sensor is designed to detect the infrared radiation emitted by humans and animals in motion within its detection range. It is a highly sensitive device that can detect even the slightest movement, making it an excellent choice for applications that require motion detection.

The HC-SR501 PIR motion sensor is small in size and easy to use. It has a detection range of up to 7 meters and a 120-degree detection angle. It operates on a wide range of input voltages, typically between 4.5V and 20V, making it compatible with most microcontrollers and other electronic devices.

Hardware Components

To interface HC-SR501 PIR Motion Sensor with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
PIR Motion SensorHC-SR5011
LEDs1
Passive Buzzer1
Breadboard1
Resistor Assortment1
Jumper Wires1

HC-SR501 PIR Motion Sensor Pinout

Pin NumberPin NameDescription
1VCCConnected to the ground of circuit
2The input voltage is +5V for typical applications. Can range from 4.5V- 12VDigital pulse high (3.3V) when triggered (motion detected) digital low(0V) when idle(no motion detected
3GroundConnected to the ground of the circuit

HC-SR501 PIR Motion Sensor Circuit

Make connections according to the circuit diagram given below.

HC-SR501-PIR-Motion-Sensor-With-Arduino-Circuit-1
HC-SR501-PIR-Motion-Sensor-With-Arduino-Circuit-1

Wiring / Connections

ArduinoHC-SR501 Sensor
5VVCC
GNDGND
D2OUT

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

// Define connection pins:
#define pirPin 2
#define ledPin 13

// Create variables:
int val = 0;
bool motionState = false; // We start with no motion detected.

void setup() {
  // Configure the pins as input or output:
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);

  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Read out the pirPin and store as val:
  val = digitalRead(pirPin);

  // If motion is detected (pirPin = HIGH), do the following:
  if (val == HIGH) {
    digitalWrite(ledPin, HIGH); // Turn on the on-board LED.

    // Change the motion state to true (motion detected):
    if (motionState == false) {
      Serial.println("Motion detected!");
      motionState = true;
    }
  }

  // If no motion is detected (pirPin = LOW), do the following:
  else {
    digitalWrite(ledPin, LOW); // Turn off the on-board LED.

    // Change the motion state to false (no motion):
    if (motionState == true) {
      Serial.println("Motion ended!");
      motionState = false;
    }
  }
}

Code Explanation

This Arduino code is written to interface the HC-SR501 PIR motion sensor with an Arduino board. It defines two connection pins: pirPin (which is connected to the PIR motion sensor) and ledPin (which is connected to the onboard LED of the Arduino board).

The code initializes the pins as input or output, sets up the serial communication at a baud rate of 9600, and creates two variables: val and motionState.

The loop() function continuously reads the pirPin using the digitalRead() function, and stores the output in the val variable. If the pirPin is HIGH (which means motion is detected), the on-board LED connected to ledPin is turned on using the digitalWrite() function. Additionally, the motionState the variable is changed to true, and the message “Motion detected!” is printed to the serial monitor using the Serial.println() function.

If the pirPin is LOW (which means no motion is detected), the onboard LED is turned off, the motionState variable is changed to false, and the message “Motion ended!” is printed on the serial monitor.

Overall, this code demonstrates a basic approach to interface the HC-SR501 PIR motion sensor with an Arduino board and detect motion in the environment.

Alarm System with a PIR motion sensor & buzzer

With some simple changes, you can create an alarm system with the HC-SR501 and a piezoelectric buzzer.

Alarm-System-With-PIR-Motion-Sensor-Buzzer-With-Arduino-Circuit-2
Alarm-System-With-PIR-Motion-Sensor-Buzzer-With-Arduino-Circuit-2

Code

/* Example code to create an alarm system with HC-SR501 PIR motion sensor, buzzer and Arduino. More info: www.www.makerguides.com */

// Define connection pins:
#define buzzerPin 5
#define pirPin 2
#define ledPin 13

// Create variables:
int val = 0;
bool motionState = false; // We start with no motion detected.

void setup() {
  // Configure the pins as input or output:
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);

  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Read out the pirPin and store as val:
  val = digitalRead(pirPin);

  // If motion is detected (pirPin = HIGH), do the following:
  if (val == HIGH) {
    digitalWrite(ledPin, HIGH); // Turn on the on-board LED.
    alarm(500, 1000);  // Call the alarm(duration, frequency) function.
    delay(150);

    // Change the motion state to true (motion detected):
    if (motionState == false) {
      Serial.println("Motion detected!");
      motionState = true;
    }
  }

  // If no motion is detected (pirPin = LOW), do the following:
  else {
    digitalWrite(ledPin, LOW); // Turn off the on-board LED.
    noTone(buzzerPin); // Make sure no tone is played when no motion is detected.
    delay(150);

    // Change the motion state to false (no motion):
    if (motionState == true) {
      Serial.println("Motion ended!");
      motionState = false;
    }
  }
}

// Function to create a tone with parameters duration and frequency:
void alarm(long duration, int freq) {
  tone(buzzerPin, freq);
  delay(duration);
  noTone(buzzerPin);
}

This is code for an alarm system using an HC-SR501 PIR motion sensor, a buzzer, and an Arduino board.

The program defines three pins for the connections: buzzerPin, pirPin, and ledPin, and creates some variables for later use.

In the setup() function, the pins are configured as input or output and the serial communication is started with a baud rate of 9600.

The loop() function reads the pirPin and stores the value as val. If the motion is detected (val == HIGH), the onboard LED is turned on, the alarm() function is called, and the motionState variable is set to true. If no motion is detected (val == LOW), the LED is turned off, and the buzzer is stopped, and motionState is set to false.

The program also includes a function called alarm() that creates a tone with parameters duration and frequency.

Finally, the program uses Serial.println() to print a message to the serial monitor when motion is detected or ends.

Applications

Here are some common applications of the HC-SR501 PIR motion sensor:

  • Security systems and alarms
  • Automatic lighting systems
  • Home automation projects
  • Energy-saving devices
  • Wildlife photography and monitoring
  • Interactive art installations
  • Robotics and automation projects
  • Smart city applications
  • Human detection for health and safety purposes (e.g. to detect falls of elderly or disabled individuals)

Conclusion

HC-SR501 PIR motion sensor is a versatile component that can be easily integrated into various Arduino-based projects. By following the steps outlined in this article, you can interface the sensor with your Arduino board and start detecting motion in your environment.