Project
Detecting Obstacles with HW-201 on Raspberry Pi Pico
Overview
Learn how to use the HW-201 infrared obstacle sensor with a Raspberry Pi Pico to detect nearby objects. This project is useful for obstacle-avoiding robots, line-following robots, and proximity alarms.
Instructions
Step by step
1. Connect the HW-201 VCC pin to 3.3V.
2. Connect the HW-201 GND pin to GND.
3. Connect the HW-201 OUT pin to GP19.
4. Run the code and open the Serial/console output.
5. Click or toggle the obstacle sensor in the simulator.
6. When an obstacle is detected, the output should print "Obstacle detected!".
7. When the path is clear, the output should print "Path clear".
Code
Source code
from machine import Pin
import utime
obstacle = Pin(19, Pin.IN)
print("HW-201 Obstacle Sensor Test")
while True:
if obstacle.value() == 0:
print("Obstacle detected!")
else:
print("Path clear")
utime.sleep(0.5)
Related