ESP32 MicroPython Obstacle Avoidance Sensor

In this guide, we will learn how to use the infrared obstacle avoidance sensor with the ESP32 and MicroPython to detect obstacles. In detail, we will learn:

ESP32 MicroPython Obstacle Avoidance Sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

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 obstacles using an infrared signal. It can identify objects located between 2 cm and 30 cm away. The detection range can be adjusted using 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 ESP32.
IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

How It Works

An infrared obstacle sensor module contains an IR transmitter and an IR receiver. The IR transmitter emits an infrared signal, while the IR receiver detects the reflected signal to determine whether an object is present. The presence of an obstacle is indicated by the OUT pin:

  • If an obstacle is in front of the sensor, the OUT pin is LOW.
  • If no obstacle is in front of the sensor, the OUT pin is HIGH.

※ NOTE THAT:

The sensor may become deformed during shipment, leading to malfunction. If the sensor is not functioning correctly, adjust the IR transmitter and receiver to ensure they are aligned parallel to each other.

Wiring Diagram

  • How to connect ESP32 and obstacle avoidance sensor using breadboard
The wiring diagram between ESP32 MicroPython IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and obstacle avoidance sensor

ESP32 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.

ESP32 MicroPython code for checking if the obstacle is present

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 18 # The ESP32 pin GPIO18 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 set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Wire the obstacle avoidance sensor to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • 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 (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

ESP32 MicroPython code for detecting obstacle

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-obstacle-avoidance-sensor """ from machine import Pin from time import sleep # Define pin SENSOR_PIN = 18 # The ESP32 pin GPIO18 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 ESP32.
  • 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 (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

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!