Raspberry Pi Pico - Motion Sensor

This guide shows you how to use the HC-SR501 motion sensor with the Raspberry Pi Pico to detect a person. We will cover the following details:

Raspberry Pi Pico motion sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×HC-SR501 Motion Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of HC-SR501 Motion Sensor

HC-SR501 Motion Sensor

The HC-SR501 PIR sensor is a motion detector that can sense human or animal movement. It's used in many applications, including:

  • Automatic lighting: Turning lights on and off based on motion.
  • Door control: Opening and closing doors automatically.
  • Escalator control: Activating or deactivating escalators.
  • Security systems: Detecting intruders.

Pinout

The HC-SR501 motion sensor has three pins.

  • GND pin: Connect to GND (0V).
  • VCC pin: Connect to VCC (5V).
  • OUTPUT Pin: Outputs a LOW signal when no movement is detected and a HIGH signal when movement is detected. Connect this pin to an input pin on the ESP32.

The HC-SR501 features one jumper and two potentiometers that allow you to adjust its settings. For more details, refer to the Advanced Uses of Motion Sensor.

HC-SR501 Motion Sensor Pinout

How It Works

The HC-SR501 sensor detects motion by sensing changes in infrared radiation.** For the sensor to be triggered, an object must meet two conditions:

  • It must be in motion.
  • It must emit infrared radiation.

As a result:

  • Moving objects that don't emit infrared radiation (such as a robot toy) won't be detected.
  • Objects that emit infrared radiation but remain stationary (like a person standing still* won't be detected.

Humans and animals naturally emit infrared radiation, making them detectable by the sensor.

Output Pin Behavior:

  • No motion: When no person or animal is in the sensor's detection range, the OUTPUT pin remains LOW.
  • Motion detected: When a person or animal enters the detection range, the OUTPUT pin transitions from LOW to HIGH.
  • Motion ended: When the person or animal leaves the detection range, the OUTPUT pin transitions from HIGH back to LOW.

The video explains the basic function of the motion sensor. In real-life situations, its operation may differ slightly depending on its settings, as detailed in the Advanced Uses section.

Raspberry Pi Pico - HC-SR501 Motion Sensor

When you configure a pin on the Raspberry Pi Pico to be a digital input, it can tell if the connected object is in a LOW or HIGH state.

Attach the Raspberry Pi Pico's pin to the HC-SR501 sensor's OUTPUT pin. Next, use code on the Raspberry Pi Pico to check the OUTPUT pin’s value and detect motion.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Motion Sensor

This image is created using Fritzing. Click to enlarge image

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

How To Program For Motion Sensor

  • Config a Raspberry Pi Pico pin to digital input mode.
HC_SR501 = Pin(SENSOR_PIN, Pin.IN)
  • Read the sensor's OUTPUT pin status.
motion_state = HC_SR501.value()

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-motion-sensor """ from machine import Pin import utime # Constants for pin and state SENSOR_PIN = 0 # The Raspberry Pi Pico pin (GP0) connected to the HC-SR501 motion sensor # Setup the motion sensor pin HC_SR501 = Pin(SENSOR_PIN, Pin.IN) # Initialize state variables motion_state = 0 prev_motion_state = 0 # Main loop while True: prev_motion_state = motion_state # Store the previous state motion_state = HC_SR501.value() # Read the current state from the motion sensor # Check for motion detection (transition from LOW to HIGH) if prev_motion_state == 0 and motion_state == 1: print("Motion detected!") # Optional: Additional actions when motion is detected # Check for motion cessation (transition from HIGH to LOW) elif prev_motion_state == 1 and motion_state == 0: print("Motion stopped!") # Optional: Additional actions when motion stops utime.sleep(0.1) # Sleep for 100 milliseconds

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the HC-SR501 motion sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Move your hand in front of the sensor
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Motion detected! Motion stopped!
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

Advanced Uses

This section includes advanced information that may be overwhelming. If you are unsure about the content, feel free to skip it and move on to the next sections.

We can change the sensor settings using one jumper and two knobs, as explained earlier.

Detection Range Adjuster

This potentiometer sets the detection range (from about 3 meters to 7 meters).

  • When turned completely clockwise, the detection distance is about 3 meters.
  • When turned completely anti-clockwise, the detection distance is about 7 meters.
Motion Sensor Detection Range

We can adjust the settings of the potentiometer to achieve a distance ranging from 3 meters to 7 meters.

Time Delay Adjuster

This knob changes the delay time.

  • If you rotate it fully clockwise, the delay will be approximately 5 minutes.
  • If you rotate it fully counter-clockwise, the delay will be approximately 3 seconds.

Next, we will describe what time delay is and its connection to Repeat Trigger.

motion sensor adjust time delay

Repeat Trigger Selector

This jumper allows you to choose a trigger mode: single trigger or repeat trigger.

motion sensor trigger selection

We will call the time delay setting (which you can change using the Time Delay Adjuster) time_delay. Picture yourself moving in the sensor's detection zone for a long time, which we will name motion_time (this is longer than time_delay).

  • In single trigger mode, the OUTPUT pin changes from LOW to HIGH several times. It remains HIGH for a duration called "time_delay" and LOW for a constant period of 3 seconds.
motion sensor single trigger mode
  • The OUTPUT pin remains HIGH for the length of the motion time plus the additional delay time.
motion sensor repeatable trigger mode

Testing

Let's try the different trigger settings. First, turn the Time Delay Adjuster all the way to the left to set the delay time for 3 seconds.

  • Single Trigger Mode:
    • Set the jumper for single trigger mode.
    • Move your hand in front of the sensor for 10 seconds.
    • Take your hand away from the sensor area.
    • Wait for 3 seconds, then look at the serial monitor to see the results.
    Shell x
    >>> %Run -c $EDITOR_CONTENT
    MPY: soft reboot Motion detected! Motion stopped! Motion detected! Motion stopped! Motion detected! Motion stopped!
    MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
    • Repeatable Trigger Mode:
      • Put the jumper on to start the Repeatable Trigger mode.
      • Keep moving your hand in front of the sensor for 10 seconds.
      • Take your hand away from in front of the sensor.
      • Wait for 3 seconds, then look at the serial monitor to view the output.
      Shell x
      >>> %Run -c $EDITOR_CONTENT
      MPY: soft reboot Motion detected! Motion stopped!
      MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

      In single trigger mode, the sensor turns on two or three times. In repeatable trigger mode, it turns on just once.

      ※ NOTE THAT:

      During the low period of 3 seconds, the sensor cannot detect motion. This means it is not active, but it generally does not cause issues.

      It is recommended to use the repeatable trigger mode.

      In practical uses:

      • Devices or machines usually turn on when they detect a person close by.
      • Devices or machines do not turn off immediately when the person leaves. They turn off after a short delay.

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!