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. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | Arduino UNO R4/R3 Board | 1 | Amazon | AliExpress |
| 2 | HC-SR501 PIR Sensor Module | 1 | Amazon | AliExpress | SunFounder |
| 3 | Buzzer 5V | 1 | Amazon | AliExpress | SunFounder |
| 4 | LED | 1 | Amazon | AliExpress | SunFounder |
| 5 | 220-ohm Resistor | 1 | Amazon | AliExpress| SunFounder |
| 6 | Connecting Wires | 10 | Amazon | AliExpress | SunFounder |
| 7 | Type C USB Cable | 1 | Amazon | AliExpress |
| 8 | Breadboard | 1 | Amazon | 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.
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.
- 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).
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.
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.


















