Raspberry Pi Home Security System with Camera and PIR Sensor
Introduction
Welcome, visitors! In this tutorial, you’ll learn how to build a Raspberry Pi Home Security System that captures an image whenever motion is detected and sends it via email notification. This project is simple yet powerful, combining hardware components with Python programming for a practical security solution.
Project Overview
Key Features
✅ Captures images when motion is detected using a PIR sensor and camera module.
✅ Sends the captured image as an email alert to notify you of suspicious activity.
✅ Utilizes Python for easy implementation.
Required Components
Hardware Components
- Raspberry Pi 3 Model B (or newer)
- Raspberry Pi Camera Module
- PIR Motion Sensor (generic)
Software and Online Services
- Python (installed on Raspberry Pi)
- Gmail Account (for sending alerts)
- SSH Connection (for remote control and monitoring)
Wiring and Connection
PIR Sensor Wiring Guide
- Power Pin (VCC): Connect to Pin 3 (5V Power) on Raspberry Pi.
- Ground Pin (GND): Connect to Pin 5 (GND) on Raspberry Pi.
- Output Pin (OUT): Connect to GPIO23 on Raspberry Pi.


Ensure connections are secure to avoid detection errors.
Step 1: Install Required Libraries
Before writing the code, ensure the necessary libraries are installed. Run the following commands on your Raspberry Pi:
sudo apt update
sudo apt install python3-picamera python3-smtplib
Step 2: Python Code for Motion Detection and Email Alerts
Create a new Python file and paste the following code:
from picamera import PiCamera
from time import sleep
import smtplib
import time
from datetime import datetime
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import RPi.GPIO as GPIO
# Email Configuration
toaddr = 'TO_EMAIL_address'
me = 'FROM_EMAIL_address'
Subject = 'Security Alert!!'
# GPIO Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
# Camera Setup
P = PiCamera()
P.resolution = (1024, 768)
P.start_preview()
while True:
if GPIO.input(23):
print("Motion Detected...")
sleep(2) # Camera warm-up time
P.capture('movement.jpg')
sleep(10)
# Prepare Email
subject = 'Security Alert!!'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
with open('movement.jpg', 'rb') as fp:
img = MIMEImage(fp.read())
msg.attach(img)
# Send Email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(user='FROM_EMAIL', password='PASSWORD')
server.send_message(msg)
server.quit()
Step 3: Update Email Credentials
In the above code:
- Replace
TO_EMAIL_address
with the email where you want to receive alerts. - Replace
FROM_EMAIL_address
with your Gmail address for sending alerts. - For the password, create an App Password in your Google account to ensure security.
Step 4: Running the Code
- Save the code as
security_alert.py
. - Run the script with the following command:
python3 security_alert.py
- Trigger motion near the PIR sensor to test if the system captures an image and sends an email alert.
Step 5: Testing and Troubleshooting
✅ If the email is not sent:
- Ensure your Gmail account allows Less Secure Apps or create an App Password for security.
- Verify the SMTP port is set to 587.
✅ If motion detection fails:
- Check PIR sensor wiring to GPIO23.
- Confirm the PIR sensor’s range and sensitivity settings are properly adjusted.
✅ If images are not saved:
- Ensure your camera module is connected and enabled in
raspi-config
.
Step 6: Expanding the Project
Once your security system is functional, you can expand it with additional features:
- Add Telegram or SMS notifications for faster alerts.
- Integrate cloud storage for captured images.
- Implement face recognition for enhanced security.
Conclusion
This Raspberry Pi Home Security System is an ideal beginner project for enhancing security at home or in small offices. By combining a PIR motion sensor with a camera and email alerts, you’ve created a robust solution that efficiently detects and informs you of potential threats.
You may like also:
Pingback: How To Setup Static IP Address on Raspberry Pi - Raspberry Pi
Pingback: Interfacing a light Sensor (LDR) with Raspberry Pi - Raspberry Pi
Pingback: Raspberry Pi GPIO Basics - IoTbyHVM - Explore TechBytes
Pingback: Getting Started with The Sense HAT - Raspberry Pi
Pingback: Redis : What and Why? - IoTbyHVM - Explore TechBytes
Pingback: IoT vs M2M | Difference between M2M and IoT - IoTbyHVM
Pingback: Setting up SPI on Raspberry Pi - IoTbyHVM - Explore TechBytes
Pingback: piCore (Tiny Core) Linux on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Setup Docker on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: How to Run Ubuntu 18.04 or 18.10 on Raspberry Pi
Pingback: MQTT | What is MQTT | MQTT in Depth | QoS | FAQs | MQTT Introduction
Pingback: Arduino SPI Tutorial - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Introduction and Tutorials - OnionLinux