Arduino MicroPython Obstacle Avoidance Sensor

This guide will teach you how to use an infrared obstacle avoidance sensor with an Arduino and MicroPython to detect obstacles. You will learn:

Arduino MicroPython Obstacle Avoidance Sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

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 detects objects by emitting an infrared signal. It can sense obstacles within a range of 2 cm to 30 cm. The detection range can be adjusted with an onboard potentiometer.

Pinout

The IR obstacle avoidance sensor has three pins:

  • GND pin: Connect to ground (0 volts).
  • VCC pin: Connect to a voltage supply (5 volts or 3.3 volts).
  • OUT pin: This output pin goes LOW when an obstacle is detected and HIGH when no obstacle is present. Connect it to an input pin on the Arduino.
IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

How It Works

An infrared obstacle sensor module includes an IR transmitter and an IR receiver. The IR transmitter emits an infrared signal, and the IR receiver detects the reflected signal to determine if an object is present. The OUT pin indicates the presence of an obstacle:

  • The OUT pin goes LOW if an obstacle is in front of the sensor.
  • The OUT pin stays HIGH if no obstacle is in front of the sensor.

※ NOTE THAT:

The sensor may get deformed during shipping, causing it to malfunction. If the sensor is not working properly, adjust the IR transmitter and receiver so that they are aligned parallel to each other.

Wiring Diagram

The wiring diagram between Arduino MicroPython IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

There are two approaches to programming an obstacle avoidance application:

  • Perform or skip an action based on whether the obstacle is present or absent.
  • Perform or skip an action when the obstacle is detected or cleared.

Arduino MicroPython code for checking if the obstacle is present

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 'D2' # The Arduino Giga R1 WiFi pin 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

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Wire the obstacle avoidance sensor to the Arduino according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Arduino MicroPython code for detecting obstacle

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 'D2' # The Arduino Giga R1 WiFi pin 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 Arduino MicroPython code and paste it into Thonny's editor.
  • Save the code to your Arduino.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!