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

by mahmoodmustafashilleh in Circuits > Raspberry Pi

59 Views, 1 Favorites, 0 Comments

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

HCSR501 Pico W.png
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:

  1. Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  2. Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  3. 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:

  1. Adjustable Sensitivity: Fine-tune the detection range.
  2. Time Delay Configuration: Control how long the sensor output stays active after detecting motion.
  3. Low Power Consumption: Ideal for battery-powered projects.
  4. 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.

Supplies

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)

Connecting the HCSR501 to the Raspberry Pi Pico

Understanding the HCSR501 Pins

The HCSR501 PIR sensor has three main pins:

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

Wiring Diagram

PIR Sensor:

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

LED with Resistor:

  1. Connect the positive leg of the LED (longer leg) to GPIO 2 (Pin 4) through a 220Ω resistor.
  2. 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:

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

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):

  1. This controls how long the sensor’s output remains HIGH after detecting motion.
  2. 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):

  1. This adjusts the detection range of the sensor.
  2. 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.

Add an image

Delete this image




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:

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

Debouncing:

  1. 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:

  1. 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:

  1. Home Security Systems: Trigger alarms or cameras when motion is detected.
  2. Automated Lighting: Activate lights only when someone is in the room.
  3. IoT Devices: Integrate motion detection into smart devices or automation systems.
  4. 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.