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:

7 - Raspberry Pi and Sense HAT (IV): colors with 8x8 RGB LED matrix


- Layout for this exercise:

- Sense HAT includes a 8×8 RGB LED matrix that is available to program for displaying different color effects

1 - Scrolling time and date in the RGB LED matrix

- This Python program uses color red (255,0,0) for the time and color blue (0,0,255) for the date:

- Giving execution permissions:

- Running the program:

- The output can be seen in this video:

2 - Rainbow

- Program for displaying the 7 colors of the rainbow:

- Giving execution permissions:

- Running the Python program:

6 - Raspberry Pi and Sense HAT (III): working with the Magnetometer


- Layout for this exercise:

1 - Introduction

- A magnetometer is an instrument that measures magnetism, either magnetization of magnetic material like a ferromagnet, or the direction, strength, or the relative change of a magnetic field at a particular location. 

- A compass is a simple example of a magnetometer, one that measures the direction of an ambient magnetic field.

2 - Magnetic North

- The function get_compass() gets the direction of North from the magnetometer in degrees. On the other hand, the function get_compass_raw() gets the raw XYZ axis magnetometer data:

- Giving execution permissions:

- Running the program:

2 - Sensing a magnet

- The function get_compass_raw() detects the presence of a magnet. For instance when the reading for the Z axis is higher than an established limit, turning the LED matrix with red light (255,0,0) every 0.1 second:

- Giving execution permissions:

- Running the program when a magnet is getting closer vertically to the Sense Hat module:

5 - Raspberry Pi and Sense Hat (II): IMU (Inertial Management Unit) with Accelerometer, Gyroscope and Magnetometer


- Layout for this exercise:

1 - Introduction

- The Sense HAT has a movement sensor called an IMU (Inertial Management Unit) which can measure the type of movement it is experiencing:

- An IMU is an electronic device that measures and reports a body's specific force, angular rate, and sometimes the magnetic field surrounding the body, using a combination of accelerometers and gyroscopes, sometimes also magnetometers.

- An IMU is built basically with 3 sensors in one. The readings for these 3 sensors are combined to have an accurate orientation of the Sense HAT module:

 a) gyroscope: measures momentum and rotation. 

b) accelerometer: measures acceleration forces and can be used to find the direction of gravity. 

c) magnetometer: measures the Earth's own magnetic field, so it's a bit like a compass.

- An IMU sensor is used on all manned and unmanned spacecraft to track movements and maintain an understanding of orientation.

- All objects in space have three axes around which they can rotate. The three axes are:

- Pitch, roll, yaw are terms used in aviation and airplane control to describe the movement around the 3 axes:

a) pitch: like a plane taking off

b) roll: the plane doing a victory roll

c) yaw: steering the plane like a car

- If you know how much rotation has happened on each axis, then you know which way the object is pointing.

This image shows where these axes are in relation to the Sense HAT:

2 - Programming Raspberry Pi and Sense Hat to determine its orientation

- The next program offers accurate information about the orientation of the Raspberry Pi and Sense Hat module.

- The function set_imu_config(True,True,True) sets the magnetometer, gyroscope and accelerometer (in that order) to make measurements about the position (determined by the pitch, roll and yaw output parameters):

- Giving permissions of execution:

- The initial position of the Raspberry Pi and Sense Hat is flat on a table:

- Running the program:

- Tilting up the Raspberry Pi and the Sense Hat modules:

- After tilting up the module a variation for the pitch is detected while roll and yaw remain almost equal:

4 - Raspberry Pi and Sense Hat (I): measuring temperature, humidity and pressure


- Layout for this exercise:

1 - Introduction

- The Sense HAT is an add-on board for Raspberry Pi made especially for the Astro Pi mission (it launched to the International Space Station in December 2015) and is now available to buy.

- The Sense HAT has an 8×8 RGB LED matrix, a five-button joystick and includes the following sensors:

  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity
  • Gyroscope

- Also, there is a Python library providing easy access to everything on the board:

- HAT stands for “Hardware attached on top”. It is a hardware specification for add-on modules for the Raspberry Pi:

- To install Sense Hat on Raspberry Pi:

2 - Displaying text

- A basic exercise to check whether Sense Hat is working correctly:

3 - Measuring temperature, humidity and pressure

- Sense HAT has got built-in sensors for temperature, humidity and pressure:

- Giving execution permissions:

- Running the Python script:

- About the temperature readings it is important to notice that the Sense HAT sensor gets a little extra warm because of the heat generated by the Raspberry Pi module.