Security cameras can be expensive to purchase and install. However, with a Raspberry Pi mini computer, you can build your own low cost motion detecting security camera to monitor your home or office. In this comprehensive guide, I will walk through all the steps required to set up your own DIY security camera using a Raspberry Pi.
What You Will Need
To build your Raspberry Pi security camera, you will need the following components:
-
Raspberry Pi - This serves as the brains of our security camera. Any Raspberry Pi model will work, but I recommend the Raspberry Pi 3 B+ or Raspberry Pi 4 for best performance.
-
Camera Module - Allows the Raspberry Pi to capture video. You'll need one that is compatible with the Raspberry Pi camera serial interface (CSI). The official Raspberry Pi camera module works great.
-
MicroSD Card - To store the operating system and footage captured by the camera. Aim for at least 16GB.
-
Power Supply - Get an official Raspberry Pi power supply, or another high quality option that can provide ample power to the Pi.
-
Enclosure - To protect the Raspberry Pi. You can 3D print your own or purchase one made specifically for the Pi.
-
Motion Sensor - To detect movement and trigger recording. A passive infrared (PIR) sensor is an easy wiring option.
Installing the Operating System
The first step is to install an operating system onto the Raspberry Pi. This will allow us to operate the Pi and camera module.
I recommend using Raspberry Pi OS, the official operating system optimized for the Pi's hardware. Download the Raspberry Pi Imager from the Raspberry Pi website to easily install Raspberry Pi OS onto your microSD card.
With the SD card inserted into your computer, open Raspberry Pi Imager and:
- Choose Raspberry Pi OS as the operating system
- Select your SD card drive
- Click "Write" to install the OS
Once complete, insert the microSD card into the Raspberry Pi.
Setting Up the Camera Module
Now we need to hook up and configure the camera module.
Attach the camera module to the CSI port on the Raspberry Pi. It only fits one way, so line it up carefully.
Enable the camera in Raspberry Pi configuration settings:
- Launch Raspberry Pi Configuration from the main menu
- Go to the Interfaces tab
- Enable camera support
Test the camera from the command line:
raspistill -v -o test.jpg
This will take a photo and save it as test.jpg
so you can confirm the camera works.
Capturing and Storing Footage
With the camera operational, we can now set up continuous video recording. The Raspberry Pi will store captured security footage on the microSD card.
Install Motion to handle video recording:
sudo apt install motion
Configure Motion by editing /etc/motion/motion.conf
:
- Set
daemon
toon
to run Motion as a background service - Set
framerate
to at least 15 frames per second - Set
stream_localhost
tooff
to disable live streaming - Adjust
width
,height
, andquality
as desired - Set
target_dir
to a folder like/home/pi/security
to store footage
Restart Motion to apply the changes:
sudo service motion restart
Motion will now continuously capture video and store it in the target directory when motion is detected.
Adding Motion Detection
Obviously, we don't want our security camera recording non-stop. Let's add a motion sensor so it only records when movement occurs.
A PIR (passive infrared) motion sensor is easy to connect to the Raspberry Pi.
Wire it up:
- PIR VCC pin to Raspberry Pi 5V GPIO pin
- PIR GND pin to Raspberry Pi GND GPIO pin
- PIR OUT pin to Raspberry Pi GPIO 4
Enable GPIO 4 as an input in /boot/config.txt
:
gpio=4=in
Set Motion to trigger on GPIO 4:
- In
motion.conf
, setmotion_detection
toon
- Set
input 4
undermotion_detection
Now the camera will detect motion when the PIR sensor is triggered!
Viewing the Live Feed Remotely
To view the security feed remotely, we can stream the video over the local network.
Install a streaming server like ffserver:
sudo apt install ffmpeg ffserver
Configure ffserver by editing /etc/ffserver.conf
:
- Set the
HTTPPort
to serve the stream - Add a video feed from Motion under
Feeds
- Specify the stream format under
Streams
Start ffserver:
sudo ffserver -f /etc/ffserver.conf
You can now view the live stream by visiting:
http://raspberrypi_ip:HTTPPort
Conclusion
Building your own Raspberry Pi security camera is an inexpensive way to monitor your home or office. With these steps, you can set up motion-activated recording, remote streaming, and more. Some additional features you may want to add include:
- Automatically uploading footage to the cloud for offsite backup
- Sending smart notifications when motion is detected
- Building a wireless motion sensor so it can be flexibly positioned
- Using an IP camera instead of the Raspberry Pi module for higher video quality
The Raspberry Pi platform makes an extremely versatile and customizable security camera solution. With a DIY approach, you can build exactly the features you need without paying for expensive pre-made systems.