Make your how home security device with a Raspberry Pi. This Raspberry Pi based project uses a PIR motion sensor with the Pi to notify you via email when motion is being detected. Currently, it notifies the user whenever motion is detected by the sensor irrespective of any other factors, but it can be upgraded to send notification only during the certain times of the day or adding a camera to it so that it can even send an image attached with the email whenever motion is detected.


Project Video

No Video Available

Step by Step Guide

Any recent model of Raspberry Pi can be used for this project 

1. Raspberry Pi 3 Model B
2. 1x PIR motion Sensor - 3.3V PIR motion sensor with digital output.If 5V Sensor is used, an appropriate level shifter should be used.
3. 16 GB SD card for Raspberry Pi with Raspbian OS 
4. Power Supply for Raspberry Pi - 5V 2.5Amps power supply with micro USB connector for Raspberry Pi.
5. 3x Jumpers (F-F) x 3 (SKU:2452749) - Jumpers to connect PIR sensor to Raspberry Pi.


Any model of Raspberry Pi can be used for this project as long as it has an active Internet connection. The PIR sensor connected to the Raspberry Pi senses motion and the output of it goes high whenever motion is detected.  This is picked up by the Python script running on Raspberry Pi and it sends a predefined email to the user. warning them of an intruder. 

The code is written in Python. GMAIL is used in this project but any email service supporting SMTP can be used.



Connect the PIR Sensor to the GPIOs of Raspberry Pi as shown, using the F-F jumpers.

            Raspberry Pi                       PIR Sensor
            3.3V (Pin 1)   ----------------    Vcc Pin
            Gnd.(Pin 6)    ----------------    Gnd. Pin
            GPIO04 (Pin 7)  -------------  Output Pin

Insert the SD card to the Raspberry Pi.
Connect an Ethernet cable with internet connectivity to the Pi  (For wired Ethernet connection. If Wi-Fi is used,it is not required.)
If Pi is not to be operated headless mode, connect a Display, Keyboard, and Mouse.
Connect the power supply to the Raspberry Pi and turn it on and wait till it gets booted up.

Getting Started:
If the Raspberry Pi is being used in Headless mode i: e. no Display is connected, it can be accessed from other PC using SSH but both should have to be on the same network and the IP address of the Pi has to be known.Plug in the SD card to any PC and create an empty file named SSH to boot folder to enable SSH connection. If the file is not created, as per the new Raspbian OS Raspberry Pi will not accept SSH connection. Connect to the Pi using SSH, if that is being used or otherwise open up the terminal in Raspberry Pi in the connected display by using Keyboard and mouse.After we have access to the terminal of Pi by using SSH or Display we then need to add the packages to support email on Raspberry Pi as by default the required packages are not installed. As python packages can be installed by using pip (By default not installed), it has to be installed first to the pi. To install pip and later email package use the following commands,

      $sudo apt-get update
      $sudo apt-get install python-pip #install pip for python package installation.
      $sudo pip install email

To support PIR sensor, no any additional packages are required as the output of the motion sensor is digital only and can be red using Python GPIO library for Pi which comes installed by default with Raspbian.
Now after email package is added copy the codes to a python file and run it by using the command,

      $sudo python

Before running the script make sure you have an active Internet connection on your Raspberry Pi and make the required changes in the script like user-name and password for gmail. PIR sensors need some time to calibrate when turned on and during that time no movement should be made.

eMail alert

You are now ready to receive emails and watch out for those intruders!



import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
import time
import RPi.GPIO as GPIO
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Add as per your credentials.
def sendemail(to_addr):
        mail_content = MIMEMultipart()
        mail_content['Subject'] = 'Motion detected by the PIR Sensor'
        mail_content['From'] = FROM_EMAIL_ID
        mail_content['To'] = FROM_EMAIL_ID
        mail_content.attach(MIMEText('Motion detected by the PIR sensor installed at front gate. Please check, if it is unwanted.', 'plain'))
        server = smtplib.SMTP(SMTP_SERVER)
        problems = server.sendmail(FROM_EMAIL_ID, to_addr, mail_content .as_string())
def func_called(channel):
        print 'Motion Detected.'
GPIO.add_event_detect(4,GPIO.RISING,callback=func_called, bouncetime=300) # bouncetime defines the minimum interval between two consecutive triggers.
while 1:
        #print GPIO.input(4)
        time.sleep(60) #only used to avoid application exit.