How to Connect and Use the HCSR501 PIR Sensor With a Raspberry Pi Pico/Pico W
by mahmoodmustafashilleh in Circuits > Raspberry Pi
1 Views, 0 Favorites, 0 Comments
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 — 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.
Supplies
Materials Needed
To follow along, you’ll need:
- ShillehTek HCSR501 PIR Sensor
- Raspberry Pi Pico
- LED (any color)
- 220Ω resistor
- ShillehTek Breadboard and jumper wires
- 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:
- 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.
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 remains stable and defaults to LOW when no motion is detected, minimizing potential signal fluctuations or noise. This configuration is particularly useful for accurate motion detection and consistent behavior in the circuit.
Programming the Raspberry Pi Pico
Here’s the MicroPython code to detect motion and trigger the LED:
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.