Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 30 of 47 · Camera and vision · Intermediate

Security camera with PIR and alarm

A PIR sensor sees someone move. The Pi takes a photo of the intruder, sounds the buzzer and keeps the picture. The PIR saves power: the camera works only when needed.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Security camera with PIR and alarm: the board, its power supply and the parts wired to the 40-pin header
Privacy and safety. A camera records people. Tell everyone who lives in or visits the place, keep the pictures private, and delete old ones. Switch the Pi off before you plug in or remove the camera ribbon cable.

Parts in this circuit

PartType
Pi CameraPi Camera Module 3
PIR sensorPIR sensor
Alarm buzzerBuzzer
Alarm lampLED

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1RedPIR sensor - VCCPin 2 (5V)
2ColouredPIR sensor - OUTPin 7 (GPIO4)
3BlackPIR sensor - GNDPin 6 (GND)
4ColouredAlarm buzzer - +Pin 16 (GPIO23)
5BlackAlarm buzzer - -Pin 9 (GND)
6ColouredAlarm lamp - anode +Pin 11 (GPIO17)
7BlackAlarm lamp - cathode -Pin 14 (GND)

Python code (gpiozero)

from picamera2 import Picamera2
from gpiozero import MotionSensor, Buzzer, LED
from signal import pause
from time import sleep

cam = Picamera2()
cam.configure(cam.create_still_configuration())
cam.start()
sleep(1)

pir = MotionSensor(4)
alarm = Buzzer(23)
lamp = LED(17)
shots = 0

def intruder():
    global shots
    shots += 1
    lamp.on()
    alarm.beep(on_time=0.2, off_time=0.2, n=3)
    cam.capture_file("intruder%d.jpg" % shots)
    print("Intruder! Photo", shots, "saved")

def quiet():
    lamp.off()

pir.when_motion = intruder
pir.when_no_motion = quiet
print("Armed. Click the PIR dome to wave a hand.")
pause()

How the code works

You will learn

  • Combining two sensors: PIR first, camera second
  • Events that run several actions
  • Saving evidence with a new file name each time

Try this

  • Move the ball to a new place, click the PIR dome and look at the photo.
  • Take 3 photos, half a second apart, instead of one.
  • Add a button that arms and disarms the alarm.

On a real Pi

  • A home security camera records people. Tell the people who live and visit there, and keep the pictures private and safe.
  • Store the photos on a USB drive or send them to your phone. A microSD card wears out if it is written all the time.
  • A PIR needs about 30 seconds to settle after power-up. Do not arm the alarm in that time.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
7Raspberry Pi Camera Module 3 (12 MP, autofocus)
Or a Camera Module 2 / HQ camera
1Robu.in Amazon.in ThinkRobotics
8CSI camera ribbon cable, 15-pin, 30 cm
Check the port size of your board
1Robu.in Amazon.in ThinkRobotics
9Camera mount or small tripod1Robu.in Amazon.in ThinkRobotics
10HC-SR501 PIR motion sensor1Robu.in Amazon.in ThinkRobotics
11Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
12Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 29. Auto night light from the cameraAll projects31. Count objects with the camera →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.