HC-SR501 PIR Motion Sensor

### Lesson Plan: Using the HC-SR501 PIR Motion Sensor with the Raspberry Pi Pico WH


Hey explorers! Today, we’re going to explore the fascinating world of motion detection using the HC-SR501 PIR Motion Sensor. PIR stands for Passive Infrared, and this sensor is perfect for projects that need to detect movement, like security systems, automatic lighting, and even interactive art installations. Ready to catch some motion? Let’s get started!

**Learning Objectives**

By the end of this lesson, you’ll be able to connect and use the HC-SR501 PIR Motion Sensor with your Raspberry Pi Pico WH. You’ll learn to detect motion and use this information to control other components. This skill is essential for creating responsive and interactive projects. Let’s dive into the world of motion detection!

**Materials Needed**

For this lesson, you’ll need a Raspberry Pi Pico WH, an HC-SR501 PIR Motion Sensor, an LED, a 330-ohm resistor, a breadboard, jumper wires, a Micro USB cable, and your computer with a MicroPython IDE. These tools will help you bring motion detection to your projects.

**Background Information**

The HC-SR501 PIR Motion Sensor detects changes in infrared radiation, typically caused by human movement. When it detects motion, it outputs a high signal. It has adjustable sensitivity and delay time, allowing you to fine-tune its performance. This sensor is great for detecting people entering a room, moving objects, or any kind of motion in its field of view.

**Circuit Diagram**

Before we build the circuit, let’s visualize it. The HC-SR501 PIR sensor has three pins: VCC, GND, and OUT. We’ll connect VCC to the 5V pin on the Raspberry Pi Pico, GND to ground, and OUT to a GPIO pin (let’s use GP15). We’ll also connect an LED to show when motion is detected.

**Step-by-Step Instructions**

Let’s get building! Start by placing the HC-SR501 PIR sensor on the breadboard. Connect the VCC pin of the sensor to the 5V pin on the Raspberry Pi Pico. Next, connect the GND pin of the sensor to a ground pin on the Raspberry Pi Pico. Connect the OUT pin to GP15 on the Raspberry Pi Pico using a jumper wire.

Now, connect the anode (longer leg) of the LED to another GPIO pin (let’s use GP14). Connect the cathode (shorter leg) of the LED to a 330-ohm resistor, and connect the other end of the resistor to the ground rail on the breadboard.

Once your circuit is set up, connect the Raspberry Pi Pico to your computer using the Micro USB cable. Open your MicroPython IDE, and let’s get ready to code.

**Sample Code**

Time to write some code to detect motion with the PIR sensor and light up the LED. Copy and paste the following code into your IDE and upload it to the Raspberry Pi Pico WH.

from machine import Pin
from time import sleep

# Initialize the PIR sensor and LED
pir_sensor = Pin(15, Pin.IN)
led = Pin(14, Pin.OUT)

while True:
if pir_sensor.value() == 1:
print(“Motion detected!”)


This code sets up the PIR sensor and the LED. It continuously checks the PIR sensor’s output, and if motion is detected, it lights up the LED and prints “Motion detected!” to the console. Simple, but super effective!

**Testing and Troubleshooting**

Upload the code and move around in front of the sensor. The LED should light up, and you should see “Motion detected!” printed to the console. If it’s not working, double-check your connections and make sure the sensor is correctly placed. Verify that the code is correctly uploaded to the Raspberry Pi Pico WH. Still having trouble? Take a deep breath and check your setup again. You’re on the right track!

**Applications and Extensions**

Now that you’ve got the basics down, let’s get creative. Try using the PIR sensor to trigger other actions, like sending a notification or activating a camera. You could even create an automatic light that turns on when you enter a room. The possibilities are endless, and the PIR sensor is your key to creating responsive projects!

**Summary and Review**

Fantastic job! You’ve just mastered using the HC-SR501 PIR Motion Sensor with the Raspberry Pi Pico WH and MicroPython. You learned how to connect the sensor, detect motion, and control an LED based on the sensor’s output. These skills are crucial for building interactive and responsive projects. Keep experimenting, keep building, and most importantly, have fun!

Remember, the best way to learn is by doing, so keep exploring the amazing world of electronics. Happy building, and as always, stay curious!

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *