How to detect motion with a pir sensor on the Raspberry?

PASSIVE INFRARED SENSORS

Passive infrared (PIR) sensors are devices that are used to detect motion. Sometimes they are referred to as pyroelectric sensors. As the name suggests, the devices are passive sensors, which means that the sensor does not use energy to detect infrared light.
A pyroelectric sensor consists of an electrically polarized crystal that generates an electric charge when exposed to infrared radiation. All animals emit infrared radiation, so PIR sensors can be used to detect the motion of humans and other large animals.

PIR sensor

The white dome on top of the PIR sensor is a Fresnel Lens.This lens focuses the infrared signals onto the pyroelectric sensor so that the module can detect weak signals from far distances. It also extends the sensor’s field of view.

Pyroelectric sensor

HOW PIR SENSORS WORK

MOTION DETECTION

The sensing element consists of crystals that respond to changes in infrared energy. The sensing element is split into two parts – sensing element A and sensing element B:

Sensor’s top view

When nothing is moving, the two sensing elements detect the same infrared levels and cancel each other out. When an infrared emitting object like a dog moves into the sensor’s field of view, sensing element A detects the infrared light first. This causes a positive differential change between the two sensing elements because the infrared light level hitting sensor element B is still low.

When the object passes the sensor, the infrared light level hitting sensor element B will be higher than element A, which causes a negative differential change between the two sensing elements. This potential difference is detected and amplified by an onboard logic chip (the BISS0001), and turned into a digital signal that can be detected by the Raspberry Pi.

OVERVIEW OF THE HC-SR501 PASSIVE INFRARED SENSOR

The HC-SR501 PIR sensor is a very popular passive infrared sensor among Raspberry Pi developers. It has the pyroelectric sensor, control chip, voltage regulator, tuning potentiometers, and pin connections on one small PCB:

  • The module has three pins: Ground and Vcc for powering the module. The Digital Out Pin gives a high logic level as a target moves within the sensor’s range.
  • Delay time adjust. The first potentiometer adjusts the delay time. This is the time the output signal stays HIGH if the PIR detects motion. This time can range from 0.3 seconds to 5 minutes. Turning the potentiometer clockwise or right increases the delay time. Consecutively, turning the potentiometer counterclockwise or left decreases the delay time.
  • Sensitivity adjust. The second potentiometer on the board is the sensitivity adjustment. We use this potentiometer to control the sensing range, which is approximately 3-7m. Turning this pot clockwise or right decreases the range, while turning it counterclockwise or left increases the sensing range.
  • Trigger settings. There are two operational modes:
    • Single trigger or non-repeatable mode: Here, when the sensor output is high and the delay time is over, the output will change from a HIGH to a LOW level.
    • Repeatable trigger mode: This mode will keep the output high all the time until the detected object moves out of the sensor’s range.

HOW TO CONNECT A PIR SENSOR TO THE RASPBERRY PI

Before we build the example project, let’s see how to connect the PIR sensor to the Raspberry Pi on its own.

These are the parts you will need:

  • Raspberry Pi
  • HC-SR501 PIR Sensor
  • Jumper wires

Connect the PIR sensor to the Raspberry Pi following this wiring diagram:

PROGRAMMING THE PIR SENSOR WITH PYTHON

We are going to use the gpiozero MotionSensor class to read data from the module. Here is a starter code to interface the PIR sensor with the Raspberry Pi. If you do not have the gpiozero library installed, it’s time to do so now. Just use the command sudo apt install python3-gpiozero, and you are good to go.

In this program we will print “Motion detected!” to the terminal when motion has been detected. Here’s the Python code:

from gpiozero import MotionSensor

pir = MotionSensor(4)

while True:
	pir.wait_for_motion()
	print("Motion detected!")

The gpiozero MotionSensor library makes it easy to develop sensor applications on the Raspberry Pi. This library has several functions such as waiting for a motion, what to do when the sensor detects motion, and what to do when no motion is detected. For our motion detection system, we import the MotionSensor library using the code: from gpiozero import MotionSensor. We use the digital pin 4 for reading data from the PIR sensor: pir = MotionSensor(4). Then, we enter an infinite loop that waits for the presence of motion within the sensor’s range using pir.wait_for_motion(). Finally, we take action when the PIR sensor’s state changes.

This is what the terminal output should look like when motion is detected:

CONTROL A 5V RELAY WITH A PIR SENSOR

In this example project, we will activate a 5V relay when the PIR sensor detects motion.

We will need the following parts:

  • Raspberry Pi
  • HC-SR501 PIR Sensor
  • SRD-05VDC-SL-C 5V relay
  • Jumper wires

PYTHON CODE FOR PIR SENSOR CONTROLLED 5V RELAY

This code will activate the 5V relay when the PIR sensor detects motion:

from gpiozero import MotionSensor, LED
from time import sleep

pir = MotionSensor(4)
relay = LED(17)

while True:
  pir.when_motion = relay.on
  pir.when_no_motion = relay.off

CODE DESCRIPTION

We follow the same setup as we did before, but for this example, we are going to import an additional library from gpiozero. We import the LED library and attach an instance of the LED to pin 17 of the Raspberry Pi. This will act as our output pin: relay = LED(17). The input pin of the motion sensor remains unchanged on pin 4: pir = MotionSensor(4). The LED library will also help us to change the state of the output pin when the sensor detects motion: pir.when_motion = relay.on. To make sure that the output remains LOW when we do not have motion, we use: pir.when_no_motion = relay.off. Finally, schedule the Python script with cron for running soon after boot.