Imagine you were at summer trip to other city. No body was at your home and door was locked, but some intruder try to enter your room by windows or with duplicate keys. As a result, he got a good chance to empty the room without you knowing until you return from the trip. How is it, if you get instant notification, if someone enters in your room without you knowing. Yes, we can make our own Raspberry pi 4 intruder alert alarm with email notification. Whenever it detects someone enter in your room. It will give email notification and buzzer will make sound. So let’s get started.

Components Required:
  1. Raspberry pi 4,
  2. Dual Fan Heat Sink aluminium Armor Case. (Optional)
  3. Push Button,
  4. Piezo Buzzer,
  5. PIR sensor (HC-SR501),
  6. 220 ohms Resistor,
  7. LED,
  8. Breadboard,
  9. Jumper Wire.
Creating Email Account for Raspberry Pi:

I suggest you to create new Gmail or Yahoo account for raspberry pi notification sender email. Because you will be giving less secure apps access to the account you send from. Make sure you give access to less secure apps, otherwise email won’t be sent.

Allow less secure apps for gmail

Find SMTP Server Setting for Email Account:

Simple Mail Transfer Protocol (SMTP), sending an email through code you will need SMTP server details. Each email provider has different SMTP server.

You can see settings here for Gmail account and for Yahoo account.

Gmail SMTP Server Settings:

Gmail SMTP server address: smtp.gmail.com

Gmail SMTP Port TLS: 587

Gmail SMTP Port SSL: 465

Yahoo Mail SMTP Server Settings:

Yahoo Mail SMTP server address: smtp.mail.yahoo.com

Yahoo Mail SMTP port TLS: 587

Yahoo Mail SMTP port SSL: 465

PIR sensor is not working:

It happens when you buy new sensor, It is not working. But it is not sensor fault, if you adjust sensor sensitivity and trigger time port. It will start to work as expected. Make sure knob of trigger time port is to the left side as low trigger time and sensitivity port at middle.

PIR sensor Trim port setting time triggerPIR sensor Trim port setting time trigger

Circuit Diagram for Intruder Alarm with Email Notification:

Intruder Alarm with Email Notification- Raspberry pi 4 Project

LED – GPIO17

PIR sensor- GPIO20

Push Button- GPIO16

Buzzer- GPIO27

Code for Intruder Alarm with Email Notification:
#import necessary libraries
from gpiozero import LED, Button, MotionSensor, Buzzer 
import smtplib
from time import sleep
from email.mime.text import MIMEText

#create objects to refer to each LED, the button, and the PIR sensor
led = LED(17)
pir = MotionSensor(20)
button = Button(16)
buzzer = Buzzer(27)

#Create Alarm State by default it off.
Alarm_state = False

#replace the next three lines with your credentials
from_email_addr = 'piraspberry364@gmail.com'
from_email_password = 'Asqw@3254'
to_email_addr = 'pir55452@gmail.com'

#set your email message
body = 'Raspberry Pi Alert: Motion is detected in your room.'
msg = MIMEText(body)

#set sender and recipient
msg['From'] = from_email_addr
msg['To'] = to_email_addr

#set your email subject
msg['Subject'] = 'INTRUDER ALERT..!!'

while True:

    if button.is_pressed:
        Alarm_state = True
        print('Alarm ON')

    if Alarm_state == True:
        led.on()
        sleep(1)
        if pir.motion_detected == True:
            print('motion detected')
            buzzer.beep()
            sleep(1)
            buzzer.off()
            led.off()
            #connecting to server and sending email
            #edit the following line with your provider's SMTP server details
            server = smtplib.SMTP('smtp.gmail.com', 587)
            #comment out the next line if your email provider doesn't use TLS
            server.starttls()
            server.login(from_email_addr, from_email_password)
            server.sendmail(from_email_addr, to_email_addr, msg.as_string())
            server.quit()
            print('Email sent')
            Alarm_state = False
Code Explanation:

import libraries for led, buzzer, pushbutton and PIR sensor.

from gpiozero import LED, Button, MotionSensor, Buzzer

import sleep for delay execution in program.

from time import sleep

we are importing libraries for SMTP and email related function.

import smtplib
from email.mime.text import MIMEText

create objects that refer to LED, button, buzzer and the PIR sensor

led = LED(17)
pir = MotionSensor(20)
button = Button(16)
buzzer = Buzzer(27)

Creating alarm state. Which by default is off.

Alarm_state = False

Replace the line with your sender email address and password. And enter email address where you want to receive email. Make sure you have given permission to sender email account access to less secure app. That’s the reason we have created new gmail account.

from_email_addr = 'sender_email_ ID@gmail.com'
from_email_password = 'sender_email_Password'
to_email_addr = 'Reciever_email_ID@gmail.com'

we are creating a variable body that will store email body text. Set your email message here. Then we are creating an object called “msg”, that generates the email by itself using msg = MIMEText(body)

body = 'Raspberry Pi Alert: Motion is detected in your room.'
msg = MIMEText(body)

setting sender and recipient.

msg['From'] = from_email_addr
msg['To'] = to_email_addr

setting your email subject. Feel free to change this.

msg['Subject'] = 'INTRUDER ALERT..!!'

It will check if button is pressed, if button is pressed it will make alarm state true (it means alarm mode is ON) and print alarm is ON.

if button.is_pressed:
Alarm_state = True
print('Alarm ON')

if alarm state is true. It will turn ON LED, it means we are in Alarm mode. Then only PIR sensor will detect if any movement happen. If alarm mode is OFF. It will not detect motion. I will tell you later why I set On/Off mode.

if Alarm_state == True:
led.on()
sleep(1)

if it detect motion, buzzer start to beep, after 1 second it will be off and our led which was showing alarm mode in ON become OFF.

if pir.motion_detected == True:
print('motion detected')
buzzer.beep()
sleep(1)
buzzer.off()
led.off()

Establishing communication with a SMTP server. I am using gmail, if you are also using gmail account for sending notification. You don’t need to change this. If you are using other email provider. You need to change SMTP server address and port.

server = smtplib.SMTP('smtp.gmail.com', 587)

server.starttls() is necessary for email provider to use Transport Layer Security (TLS) to encrypt messages. If email provider doesn’t use TLS then remove or comment out this line.

server.starttls()

login with email id and password, send the email and stop communication with server.

server.login(from_email_addr, from_email_password)
server.sendmail(from_email_addr, to_email_addr, msg.as_string())
server.quit()

Print “email sent” and make alarm state to False. It means you have to again press button to turn on Alarm mode. If we don’t set alarm mode by pressing button. It will send email whenever it detect motion. If you locked your dog inside, it will send email until, he is in PIR sensor range. It can send thousands of email quickly by doing that, google may block/suspend your account. That’s the reason we are using alarm mode ON/OFF.

print('Email sent')
Alarm_state = False
RUN The Script:

After setting up everything as mention in this tutorial, Open Thonny python IDE. Copy paste this code in Thonny Python IDE or Python 3 IDE. Save the file and click on RUN. You have to press push button to turn ON alarm mode then only it will detect motion and send email notification. When it detected motion, it will turn off alarm mode. You have again press push button to turn it ON.

For more content like this please subscribe to my YouTube channel.

Till then Keep Learning Keep Making.

Video: