Introduction
A smart home security system with Raspberry Pi allows you to monitor and control your home security through your smartphone or computer. With some basic electronics knowledge and coding skills, I was able to build an automated system that sends me alerts whenever there's suspicious activity at home. My neighbors were quite impressed with its capabilities and even asked me to help them set up their own!
In this guide, I'll walk you through the components needed, how to assemble the hardware, write the software code, and set up remote access from your devices. Follow along to make your own next-level DIY smart home security on a budget.
Hardware Components
To build the core security system unit, you will need:
-
Raspberry Pi - This is the brain of the operation. I used a Raspberry Pi 3 Model B, but any Raspberry Pi model should work.
-
MicroSD Card - Get one with at least 8GB of storage for the Raspberry Pi OS and software. I used a 16GB card to be safe.
-
Power Supply - You'll need a 5V micro USB power supply capable of at least 2.5A current. The official Raspberry Pi power supply works perfectly.
-
Camera Module - I used the Raspberry Pi Camera Module V2, which simply plugs into the Pi. But any USB camera module will work too.
-
PIR Motion Sensor - This passive infrared sensor detects motion and triggers alerts. I used the HC-SR501 model.
-
Breadboard - Used to easily connect electronic components like the PIR sensor. Get one with 400+ tie points.
-
Jumper wires - Male-to-female DuPont wires for hooking up components. A 40-piece kit covers all needs.
-
Resistors - I used two 1kΩ resistors. Different resistance values can work too.
For remote access and control, you'll also need:
-
Smartphone or computer - For remote viewing of the camera feed and receiving alerts.
-
WiFi router - To connect the Raspberry Pi to your home network wirelessly.
With these components, we can assemble a DIY smart security system with motion detection, camera recording, and notifications. Now let's look at how to put everything together.
Assembling the Hardware
Follow these steps to assemble the hardware components:
Set up the Raspberry Pi
-
Install the Raspberry Pi OS on the microSD card. I prefer the Raspberry Pi OS Lite since we don't need the full desktop environment.
-
Insert the microSD card into the Raspberry Pi and connect the power supply.
-
Connect the Raspberry Pi to your home WiFi router using an ethernet cable or by configuring WiFi.
-
Enable the Raspberry Pi Camera Module in the system settings if using the official camera.
Connect the motion sensor
-
Place the PIR motion sensor on the breadboard.
-
Connect VCC on the sensor to the Raspberry Pi 5V pin and GND to GND using jumper wires.
-
Add a 1kΩ resistor between the DOUT pin on the sensor and GPIO pin 4 on the Pi. This prevents false triggers.
-
Connect another 1kΩ resistor between DOUT and the 3V3 pin on the Pi.
The motion sensor is now connected! The DOUT pin will output a HIGH signal (3.3V) when motion is detected.
Add the camera module
Simply connect the camera module ribbon cable to the CSI port on the Raspberry Pi. The software will handle the rest later.
That completes the hardware assembly. Just plug in the power and the core of the security system is ready! Now we need to program its smart capabilities.
Software Programming
With Python and some Raspberry Pi-specific libraries, we can program the motion detection, camera recording, and notification functionality:
Import libraries
import RPi.GPIO as GPIO
import picamera
from time import sleep
from datetime import datetime
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
This imports the Python libraries we need for hardware pins, camera module, timing, email notifications, etc.
Configure motion sensor pins
pirPin = 4 #PIR connected to this GPIO pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(pirPin, GPIO.IN) #Set pin as input
Here we specify the GPIO pin connected to the PIR sensor and set it to input mode to read the digital signal.
Define camera module
camera = picamera.PiCamera()
camera.resolution = (1024, 768) # set resolution
camera.start_preview() # start camera
This initializes the camera module and begins a preview stream.
Function to detect motion
def detectMotion(pirPin):
if GPIO.input(pirPin):
print("Motion Detected!")
return True #return True on motion
else:
return False
Simple function that returns True when the PIR sensor pin goes HIGH.
Function to capture image
def captureImage():
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
camera.capture('/home/pi/security_images/'+timestamp+'.jpg')
print("Image Captured")
Uses the camera module to capture an image file with a timestamped filename when called.
Function to send email alert
def sendAlert(imgFileName):
msg = MIMEMultipart()
msg['Subject'] = 'Security Alert!'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
imgData = open(imgFileName,'rb').read()
image = MIMEImage(imgData, name=os.path.basename(imgFileName))
msg.attach(image)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("[email protected]", "password")
s.send_message(msg)
s.quit()
print("Email Alert Sent!")
Uses SMTPlib to send an email with the captured image as an attachment. Credentials need to be configured.
Main program loop
while True:
if detectMotion(pirPin):
imgName = captureImage()
sendAlert(imgName)
sleep(0.5)
Continuously checks for motion. If detected, it captures an image, saves it, and emails an alert with the image attached. A slight delay prevents over-triggering.
This covers the core programming logic. Additional code can enhance the system with remote access, live streaming, automation rules, etc. But this achieves basic smart security capabilities!
Setting Up Remote Access
To view the camera feed and receive alerts from your smartphone or computer:
-
Enable port forwarding on your router for the Raspberry Pi IP address and chosen ports. This makes the Pi accessible over the internet.
-
Set up a free Dynamic DNS service like DuckDNS for a domain name that points to your changing IP address.
-
Install software like MotionEye on the Pi to expose the camera over HTTP and enable remote access.
-
Use the domain name and MotionEye's web interface to view live and recorded video streams.
-
Configure email and push notification services on MotionEye to receive alerts remotely.
With these steps, you can remotely monitor your home security system and receive motion alerts from anywhere!
Closing Thoughts
Building your own smart security system with Raspberry Pi is an achievable weekend project that takes DIY home automation to the next level. With just a few common hardware components and some Python coding, you can construct an automated system that rivals professional setups at a fraction of the cost.
This project is also a great starting point to explore more advanced features like facial recognition, two-way audio, self-hosted storage, home automation integration, and more. Don't be surprised if your tech-savvy neighbors start asking you to build them a system too!
So empower your inner maker, tap into the potential of Raspberry Pi, and enjoy peace of mind knowing your home is protected with your own custom-built smart security solution. Just be ready to field inquiries from impressed neighbors who want security like yours!