How ToIoT HardwaresRaspberry PiTutorials/DIY

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.
pi
picam

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:

Python
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

  1. Save the code as security_alert.py.
  2. Run the script with the following command:
python3 security_alert.py
  1. 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:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin