Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi

Contents

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:

Working of the HC‑SR501 PIR Sensor

How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi

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

Circuit Diagram and Wiring

How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi

Connect the sensor as follows:

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

  1. How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
  2. Integrating Sensor Data into a Raspberry Pi: A Hands‑On Guide
  3. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  4. Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
  5. Secure Home Monitoring with Home Assistant on Raspberry Pi: Motion, Alarm, and Video Capture
  6. Control an LED with a PIR Motion Sensor on Raspberry Pi
  7. Build an Automated Aeroponics System with Raspberry Pi and Humidity Sensor
  8. Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
  9. Raspberry Pi Home Security System: PIR Motion Detection + Camera Email Alerts
  10. gatoBot: Raspberry Pi Zero W Web‑Controlled Robot with Live Video Streaming