Easy Raspberry basics: Project 22a Raspberry PI Zero W board - PIR sensor, email notification

of Acoptex.com in Raspberry Pi Zero W

Raspberry basics: Project 22a

Project name: Raspberry PI Zero W board - PIR sensor, email notification

Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, PIR sensor, email notification, RPi.GPIO library, time library, Gmail, smtplib library

Attachments: piremail.py

Raspberry Pi
Monitor or TV
HDMI cable
USB keyboard
USB mouse
Power supply
8GB SD card

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1. Raspberry PI Zero W board 1 pc

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

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8. T-Cobbler Breakout and GPIO Cable 1 pc

9. Micro USB 2.0 OTG Cable 1 pc

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

11. 4-Port USB 2.0 Hub 1 pc

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


13. Jumper cables F-M, M-M

14. Breadboard 1 pc


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.

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

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

Signals and connections of the Raspberry PI Zero W board


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 window and type the command: sudo apt-get update
  11. Then type the command: sudo apt-get install 

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:
  • Go to your Google Account.
  • On the left navigation panel, click on Security.
  • 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

  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.

  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

  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.

  7. Press Ctrl+X, Y, Enter buttons to save the file.
  8. Type the command in the Terminal: sudo python piremail.py

  9. When the motion detected the GPIO4 reads HIGH and your destination email should get an email saying "ALARM MOTION DETECTED!!!".
  10. Press Ctrl+C when your done to stop the program.


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. 

Libraries in use

  • RPi.GPIO
  • time
  • smtplib


  • Look for attached code on the begining of this project

Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 02-01-2019
Viewed: 5171 times