Posted on Leave a comment

HC-SR501 PIR motion sensor on a Raspberry Pi

raspberry pi HC-SR501 PIR motion sensor

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.

Prepare your Pi

First, you need to have a Raspberry Pi running on the latest version of Raspberry Pi OS. 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 “How to write your first Python program on the Raspberry Pi” and/or “How to use the Raspberry Pi GPIO pins” to have a quick introduction.

Prepare the extra hardware

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 hardware, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start. Raspberry Pi GPIO kit

Get to know 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:

HC-SR501 PIR motion sensor
  • Time Delay Adjust : When motion has been detected, the output signal will switch from LOW to HIGH. This HIGH position will remain for some time. Here you can adjust this time. Turn the potentiometer anti-clockwise to reduce the time. For our basic Python script (below), it is best to put the delay time to the minimum.
  • 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, it will not be triggered again even if another 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 reset (the delay counter will start from zero again) when a second motion is detected during its delaying time. If the Time Delay is set to the minimum, the position of the jumper won’t make a difference.
  • Sensitivity adjust: to adjust the detection range

Setup the hardware

Be careful ! Before starting to connect wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable from the board!

raspberry pi HC-SR501 motion sensor
  • 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)
Raspberry Pi HC-SR501 motion sensor

Write the code

The aim of this tutorial is to write a very basic 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
import time

PIR_PIN = 23

print('Starting up the PIR Module (click on STOP to exit)')
print ('Ready')

while True:
if GPIO.input(PIR_PIN):
print('Motion Detected')

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

Run the Python script

Before running the program you’ll have to give it a name and save it.

HC-SR501 PIR motion Python script

Once saved, 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 projects now. You can further use the script to process the result in an application where you want to monitor human presence. Have fun with it!


If the message “Motion Detected” is repeating itself continiously, even when there is no motion to detect :

  1. Check your wiring.
  2. Check if you adjusted the “Time Delay” potentiometer to a minimum time (turn anti-clockwise until the stop). If this isn’t the case, the sensor will be HIGH for quite a while. And during all this time, every second, the script will return “Motion Detected”. More information : see step “Get to know the HC-SR501 PIR motion sensor” here above.

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter respects your privacy. Read our privacy policy on how we handle your personal information.