Commercial security cameras often come with monthly subscription fees, proprietary cloud lock-in, and unpredictable privacy terms.
To create a completely local, self-contained surveillance device for my workshop, I built this DIY motion-activated security system using a Raspberry Pi, an HC-SR501 Passive Infrared (PIR) sensor, and a Pi Camera module.
When physical infrared motion is detected, the Pi triggers the camera, captures a high-resolution snapshot, timestamps the image, and dispatches an instant notification with the image attached.
Hardware Bill of Materials
- Raspberry Pi 3 Model B+ (running Raspbian / Raspberry Pi OS Lite)
- Raspberry Pi Camera Module v2 (8MP Sony IMX219)
- HC-SR501 PIR Motion Sensor Module (adjustable sensitivity and delay potentiometers)
- Female-to-Female Jumper Wires (3 leads)
- MicroSD Card (16GB+) & 5V 2.5A Micro-USB Power Supply
Sensor Wiring & GPIO Pinout
The HC-SR501 PIR sensor has 3 pins located underneath the Fresnel lens:
┌───────────────────────────────┐
│ HC-SR501 PIR SENSOR │
│ [ VCC ] [ OUT ] [ GND ] │
└───┬────────────┬─────────┬────┘
│ │ │
│ 5V Power │ GPIO 17 │ Ground
▼ ▼ ▼
┌───────────────────────────────┐
│ Pin 2 Pin 11 Pin 6 │
│ RASPBERRY PI GPIO │
└───────────────────────────────┘
- VCC: Connect to Raspberry Pi Pin 2 (5V Power)
- OUT: Connect to Raspberry Pi Pin 11 (GPIO 17 / BCM 17)
- GND: Connect to Raspberry Pi Pin 6 (Ground)
The Python Automation Script
The script runs a lightweight event listener using the gpiozero and picamera libraries:
import os
import time
from datetime import datetime
from gpiozero import MotionSensor
from picamera import PiCamera
# Pin 17 maps to GPIO 17 (Physical Pin 11)
pir = MotionSensor(17)
camera = PiCamera()
camera.resolution = (1920, 1080)
IMAGE_DIR = "/home/pi/security_captures"
os.makedirs(IMAGE_DIR, exist_ok=True)
print("[INFO] PIR Security System Active. Calibrating sensor...")
time.sleep(2)
def handle_motion():
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filepath = f"{IMAGE_DIR}/capture_{timestamp}.jpg"
print(f"[ALERT] Motion detected at {timestamp}! Capturing snapshot...")
camera.capture(filepath)
print(f"[INFO] Image saved: {filepath}")
# Send notification or trigger webhook
# send_email_alert(filepath, timestamp)
pir.when_motion = handle_motion
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("[INFO] Shutting down PIR monitor.")
Running as a Background Systemd Service
To ensure the security monitor starts automatically on boot and recovers from crashes, wrap it in a systemd service unit (/etc/systemd/system/pirmonitor.service):
[Unit]
Description=Raspberry Pi PIR Motion & Camera Service
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/pir_security.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable pirmonitor.service
sudo systemctl start pirmonitor.service
pi@raspberrypi:~ $ sudo systemctl status pirmonitor.service
● pirmonitor.service - Raspberry Pi PIR Motion & Camera Service
Loaded: loaded (/etc/systemd/system/pirmonitor.service; enabled)
Active: active (running) since Sat 2018-12-29 01:04:12 UTC; 2h 15min ago
Main PID: 1420 (python3)
CGroup: /system.slice/pirmonitor.service
└─1420 /usr/bin/python3 /home/pi/pir_security.py
Dec 29 01:04:14 raspberrypi python3[1420]: [INFO] PIR Security System Active. Calibrating sensor...
Dec 29 01:18:42 raspberrypi python3[1420]: [ALERT] Motion detected at 2018-12-29_01-18-42! Capturing snapshot...
Dec 29 01:18:43 raspberrypi python3[1420]: [INFO] Image saved: /home/pi/security_captures/capture_2018-12-29_01-18-42.jpg
Next Steps & Modern Evolution
While physical hardware PIR sensors are great for dedicated rooms, scaling multi-camera homelab security requires processing digital network video streams.
If you are looking to monitor IP security cameras over RTSP without external hardware sensors, check out my modern build log on CCTV-Watch: headless RTSP stream capture and OpenCV motion telemetry. You can also learn more about daemon management in my guide on creating simple background systemd service units on Linux.