How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi
Contents
- 1 Working of the HC‑SR501 PIR Sensor
- 1.1 Adjusting Sensitivity and Delay
- 2 Circuit Diagram and Wiring
- 3 Python Code to Read Motion Events
- 4 Sample Output and Next Steps
Every living creature emits infrared radiation, invisible to the naked eye. A passive infrared (PIR) sensor, such as the HC‑SR501, detects the subtle temperature changes caused by moving bodies and triggers a digital output. The HC‑SR501 employs two pyroelectric elements and a differential circuit to suppress false alarms caused by ambient temperature variations, making it ideal for security and automation projects.
Interfacing the HC‑SR501 with a Raspberry Pi is straightforward because the sensor’s output is 3.3 V TTL and it can be powered directly from the Pi’s 5 V rail.
The module has three pins:
- Vcc – 4.5 V to 20 V, power input
- OUTPUT – 0 V (LOW) / 3.3 V (HIGH)
- GND – ground
Working of the HC‑SR501 PIR Sensor
The sensor’s rectangular aperture houses two infrared detection elements positioned at opposite ends. As a heat‑emitting object moves across the field, the time each element registers a temperature change differs. The larger this time difference, the more sensitive the unit. A Fresnel lens concentrates infrared rays onto the elements, while an internal filter blocks non‑IR wavelengths to improve accuracy.
Adjusting Sensitivity and Delay
- Detection Delay (0.3 s to 600 s): Turn the potentiometer clockwise to increase delay, counter‑clockwise to decrease.
- Sensing Distance (3 m to 7 m): Turn the potentiometer clockwise to increase range, counter‑clockwise to shorten it.
Circuit Diagram and Wiring
Connect the sensor as follows:
- Vcc to pin 2 (5 V)
- OUTPUT to pin 26 (GPIO 19)
- GND to pin 6 (ground)
Double‑check the pin numbers if you use the BCM numbering scheme; GPIO 19 corresponds to pin 26 on the 40‑pin header.
Python Code to Read Motion Events
Before running the script, install the RPi.GPIO library if you haven’t already:
sudo apt-get install python3-rpi.gpio
Then create pir_motion.py with the following content:
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
pir_pin = 26 # Pin 26 (GPIO 19) is connected to the sensor’s OUTPUT
GPIO.setup(pir_pin, GPIO.IN)
print("Waiting for sensor to settle…")
time.sleep(2) # Allow the sensor to stabilize
print("Detecting motion…")
try:
while True:
if GPIO.input(pir_pin): # Sensor output is HIGH
print("Motion detected!")
time.sleep(2) # Debounce delay to avoid multiple triggers
time.sleep(0.1) # Short loop delay
except KeyboardInterrupt:
print("Exiting…")
finally:
GPIO.cleanup()
Run the script with python3 pir_motion.py. The console will display “Motion detected!” each time the sensor registers movement.
Sample Output and Next Steps
Upon successful wiring and code execution you should see output similar to the following in your terminal:
Waiting for sensor to settle…
Detecting motion…
Motion detected!
Motion detected!
...
From here you can expand the program to trigger alarms, capture images with a camera, or log events to a file or database. The HC‑SR501 is a reliable, low‑cost component for a wide range of automation and security projects.
Read more about advanced PIR sensor usage, calibration techniques, and integration with home‑automation platforms on our Advanced PIR Usage page.
Manufacturing process
- How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
- Integrating Sensor Data into a Raspberry Pi: A Hands‑On Guide
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- Secure Home Monitoring with Home Assistant on Raspberry Pi: Motion, Alarm, and Video Capture
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build an Automated Aeroponics System with Raspberry Pi and Humidity Sensor
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- Raspberry Pi Home Security System: PIR Motion Detection + Camera Email Alerts
- gatoBot: Raspberry Pi Zero W Web‑Controlled Robot with Live Video Streaming