Introduction
Have you ever wanted to build a system that can detect movement and capture photos? With a Raspberry Pi, a PIR motion sensor, and a camera module, this project becomes easy and fun! In this guide, we’ll walk you through setting up a simple motion detection system.
You’ll learn how to connect a PIR motion sensor to your Raspberry Pi, so it can sense motion in the area. When the sensor detects movement, our script will use the Raspberry Pi camera to capture photos. We’ll use the Python programming language to bring everything together, making it easy to customize and expand on later.
This project is perfect for anyone interested in Raspberry Pi, home automation, or learning the basics of Python. Let’s get started!
Table of contents
Open Table of contents
Connecting the PIR sensor
To connect a PIR (Passive Infrared) motion sensor to your Raspberry Pi, you’ll need a few components.
What You’ll Need
- Raspberry Pi (any model with GPIO pins)
- PIR motion sensor (most common models have three pins)
- Jumper wires
Understanding the PIR Motion Sensor Pins
Most PIR motion sensors have three pins:
- VCC - Power supply (usually 3.3V or 5V)
- GND - Ground
- OUT - Output signal (sends a HIGH signal when motion is detected)
Steps to Connect the PIR Motion Sensor to the Raspberry Pi
- Connect the VCC pin of the PIR sensor to the 3.3V pin on the Raspberry Pi. This will provide power to the sensor.
- Connect the GND pin of the PIR sensor to a GND (ground) pin on the Raspberry Pi.
- Connect the OUT pin of the PIR sensor to one of the GPIO pins on the Raspberry Pi. For this example, we’ll use GPIO pin 4. This pin will detect the signal from the PIR sensor when it senses motion.
With these connections, the PIR sensor will send a signal to the Raspberry Pi’s GPIO pin 4 when it detects motion.
Tuning the Sensor
Most PIR sensors have two potentiometers. These control the sensitivity of the sensor, and the period of time for which the PIR will signal when motion is detected.
When the timeout is turned anti-clockwise, the PIR will output a signal for about 2.5 seconds, whenever motion is detected. If the potentiometer is turned clockwise, the output signal will last for around 250 seconds. When tuning the sensitivity, it is best to have the timeout set as low as possible.
Motion detection
First, lets create a script that prints something to the console, when motion is detected. In this way we can check that everything is wired correctly, and test the motion sensor. We will use the gpiozero Python module. It should be installed by default on the Raspberry Pi OS, if not you can install it with following command:
sudo apt install python3-gpiozero
The script should continuously monitor for motion, and when detected, it will print a message. Later we will use it to add other actions like taking a photo, but for now we want to make sure that everything is working. Here is a breakdown of the script:
- Import the Motion Sensor Library
from gpiozero import MotionSensor
- Initialize the
MotionSensor
object and specify that the sensor is connected toGPIO
pin4
on the Raspberry Pi. This lets our code interact directly with the sensor.
pir = MotionSensor(4)
- Starting the Motion Detection Loop, print the message when movement is detected.
print("Detecting motion")
while True:
pir.wait_for_motion()
print("Motion detected")
pir.wait_for_no_motion()
The Python full code of the script can be found below:
#!/usr/bin/python3
from gpiozero import MotionSensor
pir = MotionSensor(4)
print("Detecting motion")
while True:
pir.wait_for_motion()
print("Motion detected")
pir.wait_for_no_motion()
Run the script from the terminal, move your hand in front of the sensor to see if motion is detected. You should see something similar on the terminal window:
Detecting motion
Motion detected
Motion detected
If the motion is not detected, you may need to tune the sensor with the potentiometers.
Capturing photos
Now, as we know that everything works, let’s change the script to create a basic motion-activated camera system with the Raspberry Pi. When the PIR sensor detects motion, it triggers the camera to capture a photo, saves the image with a unique timestamp, and waits for the next motion detection. This setup is ideal for creating a simple security camera or wildlife observation system.
- First we will import necessary modules.
from gpiozero import MotionSensor
from picamzero import Camera
import datetime
- Create the sensor and camera objects.
pir = MotionSensor(4)
camera = Camera()
camera.flip_camera(vflip=True)
Note: the last line is used to flip the camera - depending on the position of your camera module this may be removed if not needed.
- Motion detection loop - when sensor detects movement, the camera module takes a photo and saves it ia a file with current timestamp in the name.
print("Detecting motion")
while True:
pir.wait_for_motion()
print("Motion detected")
timestamp = datetime.datetime.now()
camera.take_photo("photo_" + timestamp.isoformat() + ".jpg")
pir.wait_for_no_motion()
Full code of the script:
#!/usr/bin/python3
from gpiozero import MotionSensor
from picamzero import Camera
import datetime
pir = MotionSensor(4)
camera = Camera()
camera.flip_camera(vflip=True)
print("Detecting motion")
while True:
pir.wait_for_motion()
print("Motion detected")
timestamp = datetime.datetime.now()
camera.take_photo("photo_" + timestamp.isoformat() + ".jpg")
pir.wait_for_no_motion()
Capturing photos in sequence
Another example is a script, that builds a motion-activated photo sequence system. When the PIR sensor detects movement, the Pi Camera captures a sequence of three images spaced one second apart, saving each one with a unique timestamped filename. This setup is allowing you to capture many images during any motion event.
The script is basically the same as in previous example. The only difference is that we will replace the camera.take_photo
function with the camera.capture_sequence
.
Below is an example, that takes a sequence of three photos, with one second interval.
camera.capture_sequence("images/photo_" + timestamp.isoformat() + ".jpg", num_images=3, interval=1)
You can change the number of images and the interval depending on your needs.
Full script:
#!/usr/bin/python3
from gpiozero import MotionSensor
from picamzero import Camera
import datetime
pir = MotionSensor(4)
camera = Camera()
camera.flip_camera(vflip=True)
print("Detecting motion")
while True:
pir.wait_for_motion()
print("Motion detected")
timestamp = datetime.datetime.now()
camera.capture_sequence("images/photo_" + timestamp.isoformat() + ".jpg", num_images=3, interval=1)
pir.wait_for_no_motion()
Conclusion
Building a motion detection system with a Raspberry Pi, a PIR sensor, and a camera module is a rewarding project that combines hardware and programming skills. By connecting the PIR motion sensor, setting up the camera, and writing Python code, you’ve created a setup that can detect movement and automatically capture photos or videos.
This project is a great foundation for many practical applications, like home security, wildlife observation, or even automated time-lapse photography. With some additional code or components, you can expand on this project by adding features like notifications, cloud storage, or remote monitoring.