Posted on 2 Comments

PIR motion sensor on a Raspberry Pi Pico

Raspberry Pi Pico PIR motion sensor pinout

Let’s see how to connect a PIR motion sensor on a Raspberry Pi Pico. On the basis of a MicroPython script we’ll be able to detect motion or human presence.

When you have completed this tutorial, you will be able to connect a HC-SR501 sensor or HC-SR505 sensor (2 types of PIR motion sensors) to your Raspberry Pi Pico. You’ll also have the basic code to convert the output signal of the sensor to useable information.

  1. Prepare the hardware

    – First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.

    – Next you need an USB cable with micro-USB plug.
    – You also need a Raspberry Pi Pico of course. For this tutorial you need pin headers soldered to the GPIO-pins of your board.

    And finally you’ll need some extra components :
    – a breadboard (we are using a 400 points breadboard)
    – an HC-SR501 PIR motion sensor or an HC-SR505 mini PIR motion sensor
    –  Dupont jumper wires

    Visit our shop if you miss any components.Raspberry Pi Pico breadboard PIR

  2. Get to know the PIR motion sensor

    PIR stand for Passive Infrared Detection. The PIR motion 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 Pico.

    There are two common types of sensors : the HC-SR501 and the smaller HC-SR505. Let’s see how to configure both type of sensors.

    HC-SR501
    The module can be configured as follows:
    HC-SR501 PIR motion sensorTime 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 MicroPython 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

    HC-SR505
    For this sensor no configuration is needed. When motion is detected, the output signal will remain high for about 10 seconds (= fix time delay).
    HC-SR505 mini PIR motion sensorBe careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.

  3. Setup the hardware part

    Raspberry Pi Pico HC-SR501 pinout– connect the VCC (power) pin of the sensor with a 5V pin (red wire)
    – connect the OUTPUT pin of the sensor to GP22 (yellow wire)
    – connect the GND of the sensor to a GND (ground) pin (black cable)

    For the HC-SR505 it looks like this :

    Raspberry Pi Pico HC-SR505
    And for the HC-SR501 :
    Raspberry Pi Pico HC-SR501

  4. Write the code

    The aim here is to write a very basic MicroPython script that allows us to visualize when motion is detected. To do this, the microcontroller checks every second the output signal from the PIR sensor. If the output is high, it means that motion has been detected. Remember that the signal will remain high for a while (see time delay here above).

    Now, open Thonny and write or paste following code in the IDE:

    from machine import Pin
    import time
     
    pir = Pin(22, Pin.IN, Pin.PULL_DOWN)
    n = 0
     
    print('Starting up the PIR Module')
    time.sleep(1)
    print('Ready')
     
    while True:
         if pir.value() == 1:
              n = n+1
              print('Motion Detected ',n)
         time.sleep(1)


    Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tabs”.

    MicroPython Pico PIR motion
    Some explanations about the code :

    from machine import Pin : to partially import the machine module to have access to the GPIO pins.
    import time : to import the time module. This will allow us to use time-related tasks.
    pir= Pin(22, Pin.IN, Pin.PULL_DOWN): here we declare the PIR motion sensor pin (=GP22) as an input pin. To avoid a ‘floating’ input, we use an internal pull-down resistor.
    time.sleep(1) We wait for 1 second to settle the sensor
    while True: is an infinitive loop (until we stop the program).
    if pir.value() == 1: if the PIR-pin is high (the sensor has detected movement)
    time.sleep(1): wait for 1 second

  5. Run your script

    Now, it’s time to save your script. You can either save it on your computer or on your Pico board.

    Then, click on the Run button of the Thonny IDE. When the PIR sensor detects motion, ‘Motion Detected’ with a counter will appear in the Thonny shell.Raspberry Pi Pico PIR motion sensor pinout

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!

Troubleshooting

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 PIR motion sensor” here above.

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 22

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?

2 thoughts on “PIR motion sensor on a Raspberry Pi Pico

  1. Clear introduction!

  2. This is excellent, and just works! I tried several other posts about setting this up but none of them worked. Yours worked perfectly.
    There was a bit of inconsistency on other posts regarding the voltage. Nobody mentioned using the PIN-39 (VSYS), like you did.
    For others reading, the 2 potentiometers can be tricky. I recommend setting them both completely counter clockwise at first. Then adjust accordingly.

    I was able to add an option to strobe an LED without much difficulty at all.
    Thank you!

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.9 / 5. Vote count: 22

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

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.