Raspberry Pi Pico - Obstacle Avoidance Sensor
In this guide, we will learn how to use the Raspberry Pi Pico and the infrared obstacle avoidance sensor to detect obstacles.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of IR Obstacle Avoidance Sensor
The infrared (IR) obstacle sensor uses an infrared signal to detect obstacles ahead. It can identify objects that are between 2cm and 30cm away. You can adjust the detection range using a tool included in it, known as a potentiometer.
Pinout
The IR obstacle avoidance sensor comes with three pins:
- GND pin: Connect this pin to GND (0 volts).
- VCC pin: Connect this pin to VCC (5 volts or 3.3 volts).
- OUT pin: This is an output pin. It shows LOW when there is an obstacle and HIGH when there is no obstacle. Connect this pin to the input pin of the Raspberry Pi Pico.
How It Works
An infrared obstacle sensor module has an IR transmitter and an IR receiver. The IR transmitter emits an IR signal. The IR receiver picks up this signal when it reflects off an object, to see if there's an obstacle. The presence of an obstacle is indicated through the OUT pin.
- When an obstacle blocks the sensor, its OUT pin becomes LOW.
- When no obstacle is in front of the sensor, the OUT pin is HIGH.
※ NOTE THAT:
During shipping, the sensor can get bent, which might stop it from working properly. If the sensor isn’t working correctly, line up the infrared transmitter and receiver parallel to each other.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
Here are examples of how to use the obstacle sensor:
- Act or wait, based on if there is a barrier.
- Act or wait, when you notice a barrier or when it disappears.
Raspberry Pi Pico code for checking if the obstacle is present
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Wire the obstacle avoidance sensor to the Raspberry Pi Pico according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Put an obstacle in front of the sensor for a while, then remove it.
- Check out the message in the Shell at the bottom of Thonny.
Raspberry Pi Pico code for detecting obstacle
Detailed Instructions
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your Raspberry Pi Pico.
- Click the green Run button (or press F5) to execute the script.
- Put something in front of the sensor briefly, then remove it.
- Check out the message in the Shell at the bottom of Thonny.