Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » How to use HC-SR501 PIR Motion Sensor with Arduino
Arduino Projects

How to use HC-SR501 PIR Motion Sensor with Arduino

Mamtaz AlamBy Mamtaz AlamUpdated:February 2, 20256 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
HC-SR501 PIR Motion Sensor with Arduino
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview

This guide focuses on interfacing of the HC-SR501 PIR (Passive Infrared) Motion Sensor with an Arduino UNO Board. The HC-SR501 sensor is designed to detect motion through infrared signals emitted by living beings, making it ideal for applications like security systems, automated lighting, and motion-activated devices.

In this guide, we will cover the basic steps of connecting the HC-SR501 sensor to an Arduino UNO board. We will also write the code for the Arduino to process the signals from the PIR sensor. Then we will write a code that will activate a buzzer and LED when the motion is detected. Thus the PIR Sensor can be used to make a Motion Triggered Alarm System.


Bill of Materials

For this tutorial, I am using the SunFounder Arduino Starter Kit which has 90+ components.

Here is the list of components I am using from this Kit.

S.N.ComponentsQuantityPurchase Links
1Arduino UNO R4/R3 Board1Amazon | AliExpress
2HC-SR501 PIR Sensor Module1Amazon | AliExpress | SunFounder
3Buzzer 5V1Amazon | AliExpress | SunFounder
4LED1Amazon | AliExpress | SunFounder
5220-ohm Resistor1Amazon | AliExpress| SunFounder
6Connecting Wires10Amazon | AliExpress | SunFounder
7Type C USB Cable1Amazon | AliExpress
8Breadboard1Amazon | AliExpress | SunFounder




HC-SR501 PIR Motion Sensor Module

The Passive Infrared (PIR) Motion Sensor is designed to detect movement. It’s frequently utilized in security and automated lighting systems.

HC-SR501 PIR Motion Sensor Module

This sensor features two slots sensitive to infrared radiation. When an object, such as a person, passes in front of the sensor, it detects a change in the amount of infrared radiation and triggers an output signal.

HC-SR501 Specifications

  • Voltage: 4.5V – 20V
  • Power Consumption: 65mA
  • TTL output: 3.3V, 0V
  • Delay time: Adjustable (.3->5min)
  • Lock time: 0.2 sec
  • Trigger methods: L – disable repeat trigger, H enable repeat trigger
  • Sensing range: less than 120 degree, within 7 meters



Working of the HC-SR501 PIR Motion Module

The PIR sensor consists of two slots linked to a differential amplifier. When a static object is positioned in front of the sensor, both slots receive equal amounts of radiation, resulting in zero output. However, when a moving object is present, one slot absorbs more radiation than the other, causing the output to vary high or low. This fluctuation in output voltage signifies motion detection.

After connecting the sensing module, it undergoes a one-minute initialization period. During this phase, the module may activate 0 to 3 times at intervals. Subsequently, it enters standby mode. It’s important to minimize exposure to light sources and other interference near the module’s surface to prevent false activations due to interfering signals. Additionally, it’s advisable to use the module in environments with minimal wind, as wind can also affect the sensor’s performance.

HCSR501 Sensing Distance & Delay Adjustments

There are two potentiometers attached on the PCB board of the PIR Sensor, which are used to adjust distance and delay.

Turning the knob of the distance adjustment potentiometer clockwise, the range of sensing distance increases, and the maximum sensing distance range is about 0-7 meters. If turn it anticlockwise, the range of sensing distance is reduced, and the minimum sensing distance range is about 0-3 meters.

Rotate the knob of the delay adjustment potentiometer clockwise, you can also see the sensing delay increasing. The maximum of the sensing delay can reach up to 300s. On the contrary, if rotate it anticlockwise, you can shorten the delay with a minimum of 5s.

HCSR501 Trigger Modes

The HC-SR501 has a jumper which is used to select the trigger mode. Using the jumper cap, we can select two trigger modes.

  • H: Repeatable trigger mode, after sensing the human body, the module outputs a high level. During the subsequent delay period, if somebody enters the sensing range, the output will keep being a high level.
  • L: Non-repeatable trigger mode, outputs a high level when it senses the human body. After the delay, the output will change from a high level to a low level automatically.



Interfacing HC-SR501 PIR Motion Sensor with Arduino

Let us see how we can interface the HC-SR501 PIR Motion Sensor Module with Arduino UNO Board. The HC-SR501 PIR Motion Sensor’s connection with an Arduino is a straightforward process involving basic wiring and simple programming. Here is the connection diagram.

HC-SR501 PIR Sensor Arduino UNO Connection

  • Connect the VCC pin of the HC-SR501 to the 5V pin on the Arduino.
  • Connect the GND pin of the HC-SR501 to one of the GND (Ground) pins on the Arduino.
  • Connect the OUT pin of the HC-SR501 to a digital input pin on the Arduino (for example, pin D2).

Interfacing HC-SR501 PIR Motion Sensor with Arduino

Ensure the sensor is placed in a way that optimizes its detection range and field of view.


Source Code/Program for Basic Testing

Once the physical connections are made, you can write a simple Arduino sketch to test the sensor’s functionality.

The following test code would typically read the state of the digital input pin connected to the sensor’s output and take an action like printing a message to the serial monitor, when motion is detected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Define the pin number for the PIR sensor
const int pirPin = 2;
// Declare and initialize the state variable
int state = 0;
 
void setup()
{
  pinMode(pirPin, INPUT);  // Set the PIR pin as an input
  Serial.begin(9600);      // Start serial communication with a baud rate of 9600
}
 
void loop()
{
  state = digitalRead(pirPin);         // Read the state of the PIR sensor
  if (state == HIGH)                  // If the PIR sensor detects movement (state = HIGH)
  {
    Serial.println("Somebody here!");  // Print "Somebody here!" to the serial monitor
  }
  else
  {
    Serial.println("Monitoring...");
    delay(100);
  }
}


Upload the above code to the Arduino UNO Board, then the device will be ready for testing.

When no object is placed in front of the PIR Sensor, it will show the monitoring status.

For motion detection, place some object or simply put your hand in front of the sensor.

The Serial Monitor will show the Motion Detection status.

This is how you can interface the HC-SR501 PIR Motion Sensor Module with Arduino and do the basic testing.


Motion Triggered Alarm using HC-SR501 PIR Sensor & Arduino

Since, we have tested the HC-SR501 PIR Sensor above, now we can develop a project called Motion Triggered Alarm. In this project, when the PIR sensor detects motion, the LED and Buzzer is activated. Here is a simple connection diagram for this project.

Motion Triggered Alarm using HC-SR501 PIR Sensor & Arduino

In the above circuit the PIR Sensor, LED and Buzzer is connected to the digital pin 2, 3 and 4 of the Arduino respectively.

Use a breadboard for connection and place PIR Sensor in a proper orientation.



Now, we have assembled the hardware on the breadboard, so its time to write an Arduino Code for Motion Based Alarm function. Here is the complete code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Define pin numbers for the PIR sensor, LED, and Buzzer
const int pirPin = 2;
const int ledPin = 3;
const int buzzerPin = 4;
 
// Declare and initialize a variable to hold the state of the PIR sensor
int state = 0;
 
// Setup function to initialize pin modes and start serial communication
void setup()
{
  pinMode(pirPin, INPUT);             // Set the PIR sensor pin as an input
  pinMode(ledPin, OUTPUT);            // Set the LED pin as an output
  pinMode(buzzerPin, OUTPUT);         // Set the Buzzer pin as an output
  Serial.begin(9600);                 // Initialize serial communication at 9600 baud rate
 
  // Initialize LED and Buzzer in the OFF state
  digitalWrite(ledPin, LOW);
  digitalWrite(buzzerPin, LOW);
}
 
// Function to play a melody
void playMelody() {
  // Melody notes: frequencies in Hz (example: C4, D4, E4, ...)
  int melody[] = {262, 294, 330, 349, 392, 440, 494};
  int noteDurations = 200; // Duration for each note
 
  // Play each note in the melody array
  for (int thisNote = 0; thisNote < 7; thisNote++) {
    tone(buzzerPin, melody[thisNote]);
    delay(noteDurations);
    noTone(buzzerPin); // Stop playing the note
  }
}
 
// Main loop function
void loop()
{
  state = digitalRead(pirPin);        // Read and update the state of the PIR sensor
 
  if (state == HIGH)                  // Check if the PIR sensor detects movement
  {
    Serial.println("Somebody here!"); // Output a message to the serial monitor
    digitalWrite(ledPin, HIGH);       // Turn ON the LED
    playMelody();                     // Play melody on the Buzzer
    delay(50);                        // Brief delay for stability
  }
  else                                // No movement detected
  {
    Serial.println("Monitoring...");  // Output a monitoring message to the serial monitor
    digitalWrite(ledPin, LOW);        // Turn OFF the LED
    noTone(buzzerPin);                // Ensure the Buzzer is OFF
    delay(100);                       // Longer delay during idle state
  }
}

The above code is well written and well commented for better understanding. The project revolves around the PIR motion sensor’s capability to detect motion.

When motion is detected, a signal is sent to the Arduino, triggering the LED and Buzzer. The buzzer and LED remains active for a short period even if motion ceases.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleIoT Smart Exhaust Fan: ESP32 Based Monitoring & Control
Next Article Rain Detection using Raindrop Sensor Module & Arduino

Related Posts

DC Energy Meter using Arduino

Build a DC Energy Meter using Arduino – 32V/5A

Updated:August 26, 20252K
Interfacing ADXL375 Accelerometer with Arduino

Interfacing ADXL375 Accelerometer with Arduino (±200g)

Updated:June 28, 2025
PZEM-004T Arduino Energy Meter

DIY AC Energy Meter using PZEM-004T & Arduino

Updated:March 6, 20258K
Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Updated:February 2, 20259K
Password Based Door Lock Security System Using Arduino & Keypad

Password Based Door Lock Security System Using Arduino & Keypad

Updated:February 2, 20252436K
Earthquake Detector Alarm with with Accelerometer & Arduino

Earthquake Detector Alarm with Accelerometer & Arduino

Updated:February 2, 2025661K
Add A Comment

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT Based PM & Air Quality Monitoring System using ESP32
    IoT Based PM & Air Quality Monitoring System using ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • How to use INA226 DC Current Sensor with Arduino
    How to use INA226 DC Current Sensor with Arduino
  • IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
    IoT Based ECG Monitoring with AD8232 ECG Sensor & ESP32
  • How to use INA219 DC Current Sensor Module with Arduino
    How to use INA219 DC Current Sensor Module with Arduino
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.