Security Camera Raspberry Pi: How to Build One in 4 Steps
The fastest way to build a security camera Raspberry Pi setup is to pair a Raspberry Pi 4 or 5 with a Camera Module 3, install the free Motion software, and view the stream through a VPN. Motion handles detection, recording and a live stream from one config file. The build costs less than many commercial cameras, and your footage stays on your own network.
Most tutorials fail at the same three points. The camera is not detected, the guide targets an older camera stack, or remote viewing relies on a forwarded router port that exposes the Pi to the internet. Those problems eat hours.
This guide covers the parts you need, then four steps from a blank microSD card to a camera you can watch from anywhere, including how to size storage. Vadzo designs MIPI and USB camera modules for embedded boards, so we also note when a step-up sensor is worth it.
Security Camera Raspberry Pi: What you need before you start
You need a Pi, a camera, a power supply and a microSD card, and your Pi model decides which camera cable you buy. Sort this out first, because a wrong cable is the most common reason a build stalls on day one.
The parts list
Part | Recommended | Notes |
|---|---|---|
Board | Raspberry Pi 4 or 5 | A Pi Zero 2 W works for a small camera at lower resolution |
Camera | Camera Module 3 | 12MP with autofocus |
Power | Official supply | 5V 3A for a Pi 4, 5V 5A for a Pi 5 |
Storage | 32 GB or larger microSD | High-endurance card or USB SSD preferred |
Cable | Matches your board | Pi 4 uses a 15-pin connector. Pi 5 and Pi Zero use 22-pin |
The camera connector differs by model, so check whether the ribbon in the box fits your board. If it does not, buy an adapter cable.
When the stock camera is not enough
The Camera Module 3 suits indoor rooms, doorways and garages. For low light, 4K video or wide temperature ranges, a board-level MIPI camera with a swappable M12 lens is a better base. Vadzo's Bolt-1335CRS is a 13MP color camera that streams 4K, with iHDR and low-light support, rated -30°C to 70°C, from $165. The Bolt-234CGH is a 1080p global shutter camera with autofocus, rated -40°C to 85°C, from $190, which keeps fast-moving subjects free of rolling-shutter distortion. Vadzo's MIPI cameras have driver support for Raspberry Pi 4 and 5.
Match the camera cable to your Pi model before you order anything else.
Step 1. Flash the OS and test the camera
Flash Raspberry Pi OS Lite (64-bit) with Raspberry Pi Imager, then confirm the camera works before you install any security software. Testing the camera in isolation tells you whether a later failure is hardware or software.
Prepare the card
In Imager, pick your Pi model and Raspberry Pi OS Lite, then open the edit settings. Set a hostname such as gatecam, a username, a strong password and your Wi-Fi details. Turn on SSH and paste your public key if you have one. Power the Pi off, lift the camera connector latch, seat the ribbon fully and press the latch down.
Test the camera
Boot the Pi, SSH in and run these commands:
sudo apt update && sudo apt full-upgrade -y sudo reboot rpicam-hello --list-cameras rpicam-still -n -o test.jpg
If the list shows your sensor, the camera is detected. If it says no cameras are available, power down and reseat both ends of the ribbon. The -n flag skips the preview window, which a headless Pi has no screen for.
Do not install any software until rpicam-hello lists your camera.
Step 2. Install Motion and turn on detection
Install Motion from the package manager, point it at your camera, set a detection threshold and enable it as a service. One program then gives you detection, recording and a live stream.
Install and configure Motion
sudo apt install -y motion sudo nano /etc/motion/motion.conf
Set or change these lines, leaving the rest at their defaults:
width 1280
height 720
framerate 15
stream_port 8081
stream_localhost off
stream_maxrate 10
threshold 1500
event_gap 10
movie_output on
target_dir /var/lib/motion
For a USB camera, add videodevice /dev/video0. For a Pi camera on the CSI connector, add libcam_device camera. Motion needs libcamera support for this, so if the log shows no camera, install a current Motion release that includes it, or use a UVC USB camera instead. Then start it with sudo systemctl enable --now motion.
Tune the detection
The threshold is the number of changed pixels that counts as motion. At 1280 x 720 the frame holds 921,600 pixels, so 1500 triggers on roughly 0.16% of the image changing. Raise it if trees or shadows cause false alerts. Lower it if people walk past unrecorded. event_gap is the quiet seconds before one recording ends, so 10 keeps a single visit in a single clip.
If you want a browser settings page and several cameras, motionEye runs on top of Motion and exposes the same options as sliders. Check the log with journalctl -u motion -f while you walk past the lens.
Tune the threshold by walking past the camera, not by guessing.
Step 3. Add remote viewing without exposing the Pi
Open http://gatecam.local:8081 on your home network to see the live stream, and use a VPN for everything outside it. Never forward port 8081 on your router.
Check the stream and add a password
Add these lines to motion.conf, then run sudo systemctl restart motion:
stream_auth_method 2 stream_authentication viewer:choose-a-long-password
Digest authentication stops anyone on your Wi-Fi from opening the stream. The stream is still not encrypted, which is why it should never face the internet directly.
Reach it from outside with a VPN
Install WireGuard on the Pi or on your router and add your phone as a peer. When the VPN is on, your phone behaves as if it sits on your home network, so the same URL works from anywhere. A forwarded port, by contrast, puts an unencrypted video stream in front of automated scanners that probe open ports constantly.
Also keep the Pi itself locked down: use SSH keys, disable password login, and run sudo apt update && sudo apt full-upgrade monthly.
Reach the camera through a VPN, never through an open router port.
Step 4. Mount the camera and plan storage
Pick a mounting spot with good Wi-Fi and stable power, then size your storage before the card fills. Continuous recording fills a card in days, while motion-only recording lasts months.
Size your storage
Assume 1080p H.264 at about 4 Mbps. That is 0.5 MB per second, or 1.8 GB per hour, or about 43 GB per day.
Recording mode | Per day | Days on a 64 GB card |
|---|---|---|
Continuous | 43 GB | about 1.5 |
2 hours of motion | 3.6 GB | about 17 |
30 minutes of motion | 0.9 GB | about 71 |
Motion does not delete old clips by itself. Add this line with sudo crontab -e to remove clips older than 14 days:
0 3 * * * find /var/lib/motion -type f -mtime +14 -delete
A high-endurance card or a USB SSD survives constant writes far better than a basic card.
Mount and protect the hardware
Indoors, a small stand or a wall bracket is enough. Outdoors, use a weatherproof enclosure with a drip loop in the cable, and keep the Pi out of direct sun, because it throttles when hot. Bare camera modules like the Camera Module 3 are not weatherproof, so the enclosure must cover them. Check Wi-Fi signal at the mounting spot with your phone first.
Record on motion and delete on a schedule, and a 64 GB card lasts months.
Your Pi camera is ready to run
You now have a working camera: tested hardware, Motion detecting and recording, a password-protected stream, VPN access and a storage plan that will not fill up. The steps that matter most are the camera test before any software, and the VPN instead of a forwarded port.
If the stock camera limits you, with poor night footage, a wider field of view or a need for 4K, the next step is a better sensor on the same Pi. Browse Vadzo's MIPI and USB camera modules to compare sensors, lenses and interfaces, or ask the application engineering team to help you choose.



