How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Motion detection is a fundamental aspect of many smart devices and security systems. The HCSR501 PIR (Passive Infrared) sensor is a popular, inexpensive, and reliable choice for detecting motion using infrared radiation. Whether you’re building a home automation project, a motion-activated alarm, or simply experimenting with sensors, this guide will walk you through connecting and programming the HCSR501 PIR sensor with a Raspberry Pi Pico.

In this article, we’ll provide an end-to-end tutorial on how to wire the HCSR501, set it up with a Raspberry Pi Pico, and use it in a project that triggers an LED based on motion detection.

— — -

Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:

  • Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  • Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  • Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.

ShillehTek Website (Exclusive Discounts):

https://shillehtek.com/collections/all

ShillehTekAmazon Store:

ShillehTek Amazon Store — US

ShillehTek Amazon Store — Canada

ShillehTek Amazon Store — Japan

Why the HCSR501 PIR Sensor?

The HCSR501 is a versatile sensor that detects motion by sensing infrared changes in its surroundings. Its features include:

  • Adjustable Sensitivity: Fine-tune the detection range.
  • Time Delay Configuration: Control how long the sensor output stays active after detecting motion.
  • Low Power Consumption: Ideal for battery-powered projects.
  • Wide Applications: Suitable for security systems, automated lighting, and IoT devices.

Using this sensor with a Raspberry Pi Pico opens up opportunities for creating motion-activated devices that are compact, energy-efficient, and easy to program.

Materials Needed

To follow along, you’ll need:

  1. ShillehTek HCSR501 PIR Sensor
  2. Raspberry Pi Pico
  3. LED (any color)
  4. 220Ω resistor
  5. ShillehTek Breadboard and jumper wires
  6. USB cable (to program the Pico)

Step 1: Connecting the HCSR501 to the Raspberry Pi Pico

Understanding the HCSR501 Pins

The HCSR501 PIR sensor has three main pins:

  • VCC: Connects to the power source.
  • GND: Ground connection.
  • OUT: Digital output pin, which goes HIGH (1) when motion is detected.

Wiring Diagram

PIR Sensor:

  • Connect the VCC pin of the HCSR501 to the 3.3V pin on the Pico (Pin 36).
  • Connect the GND pin of the HCSR501 to the GND pin on the Pico (Pin 38).
  • Connect the OUT pin of the HCSR501 to GPIO 0 on the Pico.

LED with Resistor:

  • Connect the positive leg of the LED (longer leg) to GPIO 2 (Pin 4) through a 220Ω resistor.
  • Connect the shorter leg of the LED to GND. (Pin 38)

Note on Power Supply

The HCSR501 PIR sensor is designed to operate with a 5V input on the VCC pin for optimal performance. However, in this setup, we are supplying 3.3V from the Raspberry Pi Pico’s 3.3V pin.

Why is This Working?

Voltage Tolerance: Many HCSR501 modules are built with onboard voltage regulators, allowing them to operate at lower voltages like 3.3V. This is why the sensor still functions in this configuration, even though it’s not explicitly designed for 3.3V operation.

Uncertainty: Running the sensor at a lower voltage could potentially impact its performance:

  • Detection Range: The detection range might be slightly reduced because the sensor’s internal components (like the amplifier) may not function at full capacity.
  • Signal Stability: The output signal could become less stable or prone to noise, though this often depends on the specific module’s design.

Step 2: Configuring the HCSR501 Time Delay and Sensitivity

The HCSR501 PIR sensor includes two adjustable potentiometers that allow you to fine-tune its behavior:

Time Delay Potentiometer (Left):

  • This controls how long the sensor’s output remains HIGH after detecting motion.
  • For this project, set the time delay potentiometer to the fully counter-clockwise position. This ensures the shortest possible delay (approximately 0.3 seconds), allowing the sensor’s output to reset quickly for more responsive motion detection.

Sensitivity Potentiometer (Right):

  • This adjusts the detection range of the sensor.
  • For this project, set the sensitivity potentiometer to the middle position. This provides a moderate range, ideal for general use, reducing false triggers from distant or irrelevant motion.

For this project, I configured the PIR sensor’s pull-up resistor to the low position (that is position 1 in the diagram below) instead of the high position. This ensures that the sensor’s output signal does not repeat high until the time delay is exhausted. Otherwise, if we leave it in the high position, repeat triggering can allow the HIGH position to remain beyond the time delay we set.

Step 3: Programming the Raspberry Pi Pico

Here’s the MicroPython code to detect motion and trigger the LED:

from machine import Pin
import time

# Initialize PIR sensor on GPIO 0
pir = Pin(0, Pin.IN, Pin.PULL_DOWN)
led = Pin(2, Pin.OUT) # Initialize LED on GPIO 2
pir_state = False # Start assuming no motion detected
last_motion_time = 0 # Timestamp of the last motion detected
debounce_time = 3 # Debounce period in seconds

print("PIR Module Initialized")
time.sleep(1) # Allow the sensor to stabilize
print("Ready")

while True:
val = pir.value() # Read input value from PIR sensor
current_time = time.time()

if val == 1: # Motion detected
if not pir_state and (current_time - last_motion_time >= debounce_time):
print(current_time - last_motion_time)
print("Motion detected!")
pir_state = True
led.on() # Turn on LED
last_motion_time = current_time # Update the last motion timestamp

elif val == 0:
if pir_state and (current_time - last_motion_time >= debounce_time):
pir_state = False
led.off()
last_motion_time = current_time # Update the last motion timestamp

time.sleep(0.1) # Small delay to prevent spamming

How It Works

Motion Detection:

  • The PIR sensor outputs HIGH when motion is detected. The program triggers the LED and prints "Motion detected!" in the console.

Debouncing:

  • A 3-second debounce period is implemented to prevent rapid toggling between the “motion detected” and “motion ended” states. Without debouncing, the sensor might occasionally emit brief or erratic signals upon detection, which could falsely trigger the LED to turn on or off. The debounce time ensures that the Pico W processes only stable and intentional motion signals, providing more reliable behavior.

LED Indication:

  • The LED lights up whenever motion is detected, providing a visual indicator.

If everything is set up correctly, you can wave your hand in front of the sensor, and the LED will light up for a short duration. Pretty cool, right? This simple setup can serve as the foundation for more advanced applications that rely on motion detection. With the concepts covered in this tutorial, you’re ready to explore and build more complex projects tailored to your needs.

Applications of the HCSR501 PIR Sensor

This simple setup has a wide range of practical uses:

  • Home Security Systems: Trigger alarms or cameras when motion is detected.
  • Automated Lighting: Activate lights only when someone is in the room.
  • IoT Devices: Integrate motion detection into smart devices or automation systems.
  • Energy Efficiency Projects: Reduce energy consumption by powering devices only when needed.

Conclusion

The HCSR501 PIR sensor is a fantastic tool for hobbyists and professionals alike. Its simplicity and versatility make it a go-to choice for motion detection in various applications. By following this guide, you’ve learned how to connect it to a Raspberry Pi Pico, write the code to detect motion, and create a motion-triggered LED indicator.

This project can be a stepping stone to more advanced systems, like integrating motion detection with cloud-based IoT platforms or controlling appliances wirelessly. With endless possibilities, let your creativity guide you to the next exciting project!

If you have not already be sure to follow Shilleh on Youtube and follow the ShillehTek Amazon Store for the latest products and posts for Raspberry Pi.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Learn how to safely control high-power components like a 5V laser diode using the Raspberry Pi Pico W and a...

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.