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.

Raspberry Pi Pico Obstacle Avoidance Sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.
IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

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

The wiring diagram between Raspberry Pi and Pico IR Obstacle Avoidance Sensor

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

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 0 # The Raspberry Pi Pico pin GP0 connected to the OUT pin of IR obstacle avoidance sensor # Initialize the pin sensor = Pin(SENSOR_PIN, Pin.IN) # Main loop while True: state = sensor.value() # Read the state of the input pin if state == 0: print("The obstacle is present") else: print("The obstacle is NOT present") sleep(0.1) # Delay 100 ms

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is present The obstacle is present The obstacle is present The obstacle is present The obstacle is NOT present The obstacle is NOT present
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Raspberry Pi Pico code for detecting obstacle

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 0 # The Raspberry Pi Pico pin GP0 connected to the OUT pin of IR obstacle avoidance sensor # Initialize the pin sensor = Pin(SENSOR_PIN, Pin.IN) # Variables to store the previous and current obstacle states prev_obstacle_state = 1 # Start with HIGH (no obstacle) obstacle_state = 1 # Current reading from the input pin # Main loop while True: # Read the state of the input pin obstacle_state = sensor.value() if prev_obstacle_state == 1 and obstacle_state == 0: print("The obstacle is detected") elif prev_obstacle_state == 0 and obstacle_state == 1: print("The obstacle is cleared") # Save the last state prev_obstacle_state = obstacle_state sleep(0.1) # Delay 100 ms

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The obstacle is detected The obstacle is cleared
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!