In this tutorial we use a PIR motion sensor to detect human presence. With a Python script on your Raspberry Pi, we monitor if motion is detected. When you have completed this tutorial, you will be able to connect the HC-SR501 sensor to your Pi via the GPIO pins. You’ll also have the basic code to convert the output signal of the sensor to useable information in your Python script.
What you’ll need for this tutorial
First, you need to have a Raspberry Pi 3 running on the latest version of Raspbian. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “First Python program on the Raspberry Pi” and/or “Raspberry Pi first program using GPIO” to have a quick introduction.
Next you’ll need some extra hardware:
- a breadboard (we are using a 400 points breadboard)
- an HC-SR501 PIR motion sensor
- Dupont jumper wires
- a T-cobbler (optional)
- a 40 pin GPIO cable (optional)
If you miss any equipment, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.
The HC-SR501 PIR motion sensor
PIR stand for Passive Infrared Detection. The sensor measures variations of infrared light. As we all emits infrared light, the infrared variations will be detected by the sensor when we move in front of the sensor. When this happens, the sensor will set the output to high. As the high output signal is 3,3V, the output signal can be directly connected to a GPIO input pin of the Raspberry Pi.
The module can be configured as follows:
- Jumper set: The jumper is used to control the trigger mode. When the jumper cap is at the “L” position, the mode is set up as “unrepeated trigger mode”, which means when the module is outputting an HIGH voltage because of human motion it will not be triggered again even if another human motion is detected. When the jumper cap is at the “H” position, the module is setup as “repeated trigger mode”, which means the delaying time will be recalculated when a second human motion is detected during its delaying time.
- Sensitivity adjust: to adjust the detection range
- Time Delay Adjust
Some modules are not equipped with jumpers but have 3 contacts instead. In this case, you have to connect the middel contact with a soldering iron with either the “L contact” or the “H contact”.
Setting up the hardware part
Before starting connecting wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable of the board!
- connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
- plug the cobbler onto the breadboard as shown in the figure above or below
- plug the other end of the 40 pin cable in the T-cobbler
- connect the VCC pin of the sensor with a 5V pin (red wire)
- connect the OUTPUT pin of the sensor to pin 23 (yellow wire)
- connect the GND of the sensor to a GPIO GND pin (black cable)
Writing the code
The aim of this tutorial is to write a very simple Python script that allows us to visualize when motion is detected. To write the code, we use the Thonny IDE. You can find Thonny under the application menu of your Raspberry Pi.
Write or paste following code in the IDE:
import RPi.GPIO as GPIO
PIR_PIN = 23
print('Starting up the PIR Module (click on STOP to exit)')
Some explanations about the code:
GPIO.setmode (GPIO.BCM): The GPIO.BCM option means that we are referring to the pins by the “Broadcom SOC channel” number, these are the numbers after “GPIO”
GPIO.setwarnings (False): We use this line of code to avoid warning messages.
GPIO.setup(PIR_PIN,GPIO.IN): We define the PIR-pin (= 23) as an input pin
time.sleep(1)We wait for 1 second to settle the sensor
while True:is an infinitive loop (until we stop the program)
- Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the code line after the while command.
if GPIO.input(PIR_PIN):if the PIR-pin is high (sensor has detected movement)
time.sleep(1)Waiting for 1 second before we start the loop again
When you are done with the code, click on the Run button. You will see a message on the screen when the sensor detects movement.
To stop the program just click on the STOP button.
Congratulations! With this setup you can integrate motion detection in your code. You can further use the script to process the result in an application where you want to monitor human presence. Have fun with it!