Friday, July 14, 2017

8 - Raspberry Pi and PIR (Passive Infrared): motion detection and notification to Gmail


- Layout for this exercise:

1 - Introduction

- The goal of this exercise is to use a Raspberry Pi board with a Passive Infrared (PIR) motion detector to sense movement of people, animals, or other objects, and send an email notification when an intruder is detected. 

- A PIR sensor is an electronic device that measures infrared light radiating from objects in its field of view.

- PIR sensors are commonly used in burglar alarms and automatically-activated lighting systems.

- Further information about PIR sensors:

2 - Schematics

-  The PIR sensor has got 3 pins that are connected to the Raspberry Pi board in this way:

  • VCC = pin4 (5 V) 
  • GND = pin34 (GND)
  • Sensor = pin11 / GPIO17

- Also, there is a red LED connected to the pin13 / GPIO27 and pin9 (GND) that blinks after detecting movement:

3 - Code

- This is the Python code used in this exercise:

- Let's analyze the code.

- First, some Python libraries are imported for the email notification:

- The pins are assigned to the sensor (in) and the red LED (out) according to the previously explained schematics:

- The PIR sensor is initially set to 0, and then takes its value from the PIR sensor input. When the status changes to 1, the subsequent actions are performed:

- Immediately a notification email is sent. For that purpose some data must be introduced in the code, for instance the email account and password (blurred in the next picture), and the message to be sent:

- Also, when the variable 'status' takes the value 1 (movement is detected) a message is printed on the screen and the red LED starts to blink constantly:

4 - Running and testing the program

- When the program is run and the PIR sensor detects some movement a message is printed (the number 1 corresponds to the value of the variable 'status') and an email is sent:

- Testing the circuit and the code in this video: