Acoptex.Com

Learn ESP32, ESP8266, Arduino, and Raspberry Pi

How to get email notification from PIR sensor attached to Raspberry PI Zero W board

We will learn how to use PIR sensor with the Raspberry Pi Zero W board. When the PIR sensor detects the motion it triggers an event that sends an email with message.  This project code is only set up to work with Gmail accounts.

Tag: Project 22a Raspberry PI Zero W board – PIR sensor, email notification. Acoptex.lt

Project resources

Script: piremail.py

Libraries:

  • RPi.GPIO
  • time
  • smtplib

Other attachments: None.

Parts required

1. Raspberry PI Zero W board 1 pc

Raspberry PI Zero W board

2. Micro SD card with NOOBS and SD card adapter 1 pc

3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc

Micro USB power supply

4. USB keyboard 1 pc

USB keyboard

5. USB mouse 1 pc

USB mouse

6. TV or PC monitor 1 pc

TV or PC monitor

7. HDMI cable 1 pc

HDMI cable

8. T-Cobbler Breakout and GPIO Cable 1 pc

T-Cobbler Breakout and GPIO Cable

9. Micro USB 2.0 OTG Cable 1 pc

Micro USB 2.0 OTG Cable

10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc

Mini HDMI to HDMI Adapter

11. 4-Port USB 2.0 Hub 1 pc

4-Port USB 2.0 Hub

12.  PIR sensor HC – SR 501 or HC – SR 505 or Mini PIR Motion Sensor Module SC0322 1 pc

pir sensor
pir sensor
pir sensor

13. Jumper cables F-M, M-M

jumper cables

14. Breadboard 1 pc

breadboard

Understanding the PIR sensor

You can read more about it here.

Understanding the Raspberry PI Zero W board

You can read more about it here.

Signals and connections of the PIR sensor

Signals and connections of the PIR sensor
Signals and connections of the PIR sensor
Signals and connections of the PIR sensor

We are going to use Mini PIR Motion Sensor Module SC0322 in this project.

Signals and connections of the Raspberry PI Zero W board

Signals and connections of the Raspberry PI Zero W board
Signals and connections of the Raspberry PI Zero W board
Signals and connections of the Raspberry PI Zero W board

Wiring

Raspberry pi zero w boardand pir sensor wiring

Step by Step instruction

We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.

1. Setup and preparation

We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board. 

1.Do wiring.

2.Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.

3.Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).

4.Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI,  etc).

5.Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable. 

6.Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.

7.If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.

8.Connect Micro USB power supply to Raspberry PI Zero board micro USB input.

9.The Raspberry PI desktop will start up.

10.Open Terminal windowand type the command: sudo apt-get update

11.Then type the command: sudo apt-get install 

Raspberry OS

Raspberry OS

2. Setting the GMAIL accounts

  1. As we are using GMAIL accounts in this project you need to make some too.
  2. Go to gmail website and make two accounts: first account you will need to send email and second account you will neeed to receive it.
  3. Please note the first_account_email_address, first_account_access_password, second_account_email_address, second_account_access_password as you will need them later.
  4. Change your settings on the first account to allow less secure apps into your account.
  5. Go to your Google Account.

Gmail account

6.On the left navigation panel, click on Security.

Gmail account

7.Scroll down and on the bottom of the page, in the Less secure app access panel, click on Turn on access.

3. Making the program

1.Open the Raspberry Pi Configuration tool from the main menu. Go to Raspberry icon-> Preferences -> Raspberry PI configuration

Raspberry Pi OS

2.Select Interfaces and make sure that all settings are enabled. If it’s not enabled, enable it and reboot your Raspberry PI board to begin. We suggest to enable all settings and you will not need to come back to these configuration tool again.

Raspberry Pi OS

3.Click on OK button. We need to reboot our Raspberry PI. 

4.After restart of Raspberry Pi go to Terminal. Type this command in the Terminal: sudo nano piremail.py

Raspberry Pi OS

5.Copy and paste the code from piremail.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.

6.Edit the first_account_email_address, first_account_access_password, second_account_email_address to correspond with your desired emails.

Raspbian OS

7.Press Ctrl+X, Y, Enter buttons to save the file.

8.Type the command in the Terminal: sudo python piremail.py

Raspbian OS

9.When the motion detected the GPIO4 reads HIGH and your destination email should get an email saying “ALARM MOTION DETECTED!!!”.

Raspbian OS

10.Press Ctrl+C when your done to stop the program.

Raspberry Pi and PIR

Wrapping up

We have learnt how to use PIR sensor with the Raspberry Pi Zero W board. When the PIR sensor detects the motion it triggers an event that sends an email with message. 

Check for more DIY projects here.

Thank you for reading and supporting us.

Check for more DIY projects on Acoptex.lt and Acoptex.com!

If you are looking for high quality PCBs PCBWay is the best choice: