Projects / Motion-triggered security camera on Raspberry Pi

Motion-triggered security camera on Raspberry Pi

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 5 done
  1. Pi 5RASPBERRPi 5

    Connect the camera

    With the Pi off, lift the connector latch, push the narrow end of the Pi 5 cable in with the contacts facing the latch side as printed on the board, and close the latch.

  2. Pi 5RASPBERRPi 5

    Test the camera

    Boot and run rpicam-hello -t 5000. A preview should appear for 5 s.

  3. Pi 5Pi 5motion

    Wire the PIR

    VCC to pin 2, GND to pin 6, OUT to GPIO4 (pin 7). Set its time trimmer fully anticlockwise.

  4. Pi 5motion

    Run the script

    Save it as motioncam.py and run python3 motioncam.py. Walk past after "Armed".

  5. Pi 5RASPBERR

    Start at boot

    Make it a systemd service so it restarts after power cuts.

In short

How do you build a motion-triggered security camera on Raspberry Pi?

A PIR sensor wakes a Raspberry Pi Camera Module 3, which records a 10-second MP4 clip with the date and time in the file name. You need Raspberry Pi 5, Raspberry Pi 27 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi Camera Module 3, Raspberry Pi 5 to Camera cable (22 to 15 pin), PIR Motion Sensor HC-SR501 and Jumper wires female to female (40 pcs). It takes 5 steps and about 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Camera ribbonPi 5 CAM/DISP 0 connectorContacts facing the right way (see the step)
PIR VCCPi pin 2 (5 V)Sensor power
PIR GNDPi pin 6 (GND)Ground
PIR OUTPi GPIO4 (pin 7)3.3 V motion signal, safe for the Pi

Code

Raspberry Pi OS Bookworm (64-bit). Picamera2, gpiozero and ffmpeg are preinstalled. Test the camera first with rpicam-hello.

import os, time, glob
from datetime import datetime
from gpiozero import MotionSensor
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput

FOLDER = os.path.expanduser("~/clips")
KEEP = 200                      # delete the oldest clips beyond this many
os.makedirs(FOLDER, exist_ok=True)

pir = MotionSensor(4)
cam = Picamera2()
cam.configure(cam.create_video_configuration(main={"size": (1280, 720)}))

def tidy():
    clips = sorted(glob.glob(FOLDER + "/*.mp4"))
    for old in clips[:-KEEP]:
        os.remove(old)

print("Waiting for the PIR to settle...")
time.sleep(30)
print("Armed")
while True:
    pir.wait_for_motion()
    name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".mp4"
    print("Motion: recording", name)
    cam.start_recording(H264Encoder(bitrate=4_000_000), FfmpegOutput(os.path.join(FOLDER, name)))
    time.sleep(10)
    cam.stop_recording()
    tidy()
    pir.wait_for_no_motion()

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
rpicam-helloLive preview for 5 s
Walk in front of the PIR"Motion: recording" and a new MP4 in ~/clips about 10 s later
Clip1280x720, plays in VLC, about 5 MB
More than 200 clipsOldest ones are deleted automatically

If it does not work

"No cameras available"

The ribbon is in backwards or loose. Reseat it with the Pi powered off.

Records non-stop

The PIR sees a heater, window or pet. Point it elsewhere and lower its sensitivity.

Clips will not play

Install VLC. Some players cannot open the raw stream: the FfmpegOutput used here writes a normal MP4.

Privacy

Tell people a camera is recording, and never point it at a neighbour's home.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the motion-triggered security camera on Raspberry Pi?

Raspberry Pi 5, Raspberry Pi 27 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi Camera Module 3, Raspberry Pi 5 to Camera cable (22 to 15 pin), PIR Motion Sensor HC-SR501 and Jumper wires female to female (40 pcs). Raspberry Pi 5 to Camera cable (22 to 15 pin): Pi 5 uses the smaller camera connector.

How long does the motion-triggered security camera on Raspberry Pi take to build?

About 90 minutes for a intermediate maker, in 5 steps.

How do I know the motion-triggered security camera on Raspberry Pi works?

rpicam-hello: Live preview for 5 s. Walk in front of the PIR: "Motion: recording" and a new MP4 in ~/clips about 10 s later. Clip: 1280x720, plays in VLC, about 5 MB.

No cameras available: what should I check?

The ribbon is in backwards or loose. Reseat it with the Pi powered off.

Records non-stop: what should I check?

The PIR sees a heater, window or pet. Point it elsewhere and lower its sensitivity.

Clips will not play: what should I check?

Install VLC. Some players cannot open the raw stream: the FfmpegOutput used here writes a normal MP4.

Privacy: what should I check?

Tell people a camera is recording, and never point it at a neighbour's home.