Skip to main content

Build. Learn.
Innovate.

Basic Obstacle Avoidance Robot

Written By:

Published on:

Project Overview

This project guides students through building a simple robot that avoids obstacles using an ultrasonic sensor and two DC motors controlled by an L298N motor driver. The ESP32 microcontroller acts as the robot’s brain, detecting obstacles and adjusting movement accordingly.

Educational Goals

  • Learn how ultrasonic sensors detect distance to obstacles
  • Understand motor driver control for forward, reverse, and turning
  • Write logic to handle autonomous navigation
  • Apply real-time decision-making with sensor feedback

Detailed Parts List

  • 1x ESP32 Dev Board (36-pin)
  • 1x HC-SR04 Ultrasonic Sensor
  • 1x L298N Motor Driver Module
  • 2x DC Motors with wheels
  • 1x Chassis with caster wheel
  • 1x Battery pack (e.g., 4x AA or 7.4V Li-ion)
  • Jumper wires
  • Breadboard or screw terminals (optional)

Circuit Diagram Description

  • Ultrasonic Sensor:
    • Trig → GPIO 5
    • Echo → GPIO 18 (via voltage divider)
  • Motor Driver:
    • IN1 → GPIO 26
    • IN2 → GPIO 25
    • IN3 → GPIO 33
    • IN4 → GPIO 32
    • ENA (PWM) → GPIO 14
    • ENB (PWM) → GPIO 12
    • OUT1/OUT2 → Motor A, OUT3/OUT4 → Motor B

Software Functionality

  • Continuously checks for obstacles ahead
  • Moves forward when path is clear
  • Stops and turns when an obstacle is detected within threshold distance
  • Uses PWM to control motor speed

Web Interface Features

  • Remote control override
  • Display distance value and motor state
  • Visualization of robot’s movement in a virtual space

Implementation Steps

  1. Assemble robot chassis and mount components
  2. Wire motors, driver, and ultrasonic sensor to ESP32
  3. Flash MicroPython firmware to ESP32
  4. Upload “** and run the robot**
  5. (Optional) Enhance with OLED or dashboard

Extensions and Challenges

  • Add LED or buzzer when obstacle detected
  • Improve turning logic using random angles
  • Integrate IR sensors for side detection
  • Use servo to sweep ultrasonic sensor

Troubleshooting Guide

  • Robot doesn’t move: Check motor driver wiring and power supply
  • Constant turning or jittering: Check echo signal and distance readings
  • Motors only spin in one direction: Review IN1–IN4 logic
  • ESP32 crashes: Check sensor voltage divider and ground connections

Project Code

from machine import Pin, PWM, time_pulse_us
from time import sleep

# Motor pins
in1 = Pin(26, Pin.OUT)
in2 = Pin(25, Pin.OUT)
in3 = Pin(33, Pin.OUT)
in4 = Pin(32, Pin.OUT)

ena = PWM(Pin(14), freq=1000)
enb = PWM(Pin(12), freq=1000)

# Ultrasonic sensor pins
trig = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

# Movement functions
def stop():
    in1.off(); in2.off(); in3.off(); in4.off()
    ena.duty(0); enb.duty(0)

def forward(speed=800):
    in1.on(); in2.off(); in3.on(); in4.off()
    ena.duty(speed); enb.duty(speed)

def backward(speed=800):
    in1.off(); in2.on(); in3.off(); in4.on()
    ena.duty(speed); enb.duty(speed)

def turn_left(speed=800):
    in1.off(); in2.on(); in3.on(); in4.off()
    ena.duty(speed); enb.duty(speed)

def turn_right(speed=800):
    in1.on(); in2.off(); in3.off(); in4.on()
    ena.duty(speed); enb.duty(speed)

# Distance measurement
def get_distance():
    trig.off()
    sleep(0.002)
    trig.on()
    sleep(0.00001)
    trig.off()
    duration = time_pulse_us(echo, 1, 30000)
    distance = (duration * 0.0343) / 2
    return round(distance, 2)

# Main loop
while True:
    dist = get_distance()
    print("Distance:", dist, "cm")
    if dist > 20:
        forward()
    else:
        stop()
        sleep(0.5)
        turn_left()
        sleep(0.5)
        stop()
    sleep(0.1)

STEM Benefits

  • Science: Understand the behavior of sound waves through air using ultrasonic sensing.
  • Technology: Learn how sensors and microcontrollers are used in autonomous robotics.
  • Engineering: Build an integrated robotic system that responds to its environment.
  • Math: Use timing and distance calculations to implement avoidance logic.

Project Code

Projects
ShowCase

Real-World Projects
with Code & Hardware

Insights, Ideas
& How-Tos

Help, Support, and
Common Questions

What types of projects can I find on your website?

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.