Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 08: IR Obstacle Avoidance Sensor Module
In this lesson, you’ll learn how to use the Raspberry Pi Pico W with an IR Obstacle Avoidance Sensor Module. We’ll walk you through setting up the sensor and writing a MicroPython script that continuously reads its value to detect obstacles. By monitoring changes in the sensor data, you’ll grasp how to use it for basic obstacle detection.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiring
Code
from machine import Pin
import time
# Initialize obstacle avoidance sensor connected to pin 16 as input
obstacle_avoidance_sensor = Pin(16, Pin.IN)
while True:
# Read and print the value of the obstacle avoidance sensor
print(obstacle_avoidance_sensor.value())
# Wait for 0.1 seconds before the next read
time.sleep(0.1)
Code Analysis
Importing Libraries
The
machinemodule is imported to interact with the GPIO pins, and thetimemodule is used for adding delays.from machine import Pin import time
Setting Up the Sensor
The obstacle avoidance sensor is set up as an input device on GPIO pin 16. The
Pin.INparameter configures the pin as an input.obstacle_avoidance_sensor = Pin(16, Pin.IN)
Reading Sensor Data in a Loop
The
while True:loop continuously checks the sensor’s output. If the sensor detects an obstacle, it returns0, which is printed out. Thetime.sleep(0.1)adds a small delay to make the readings more manageable.while True: print(obstacle_avoidance_sensor.value()) time.sleep(0.1)
Note
If the sensor is not working properly, adjust the IR transmitter and receiver to make them parallel. Additionally, you can adjust the detection range using the built-in potentiometer.