Skip to content
RoboLearn
Not completed

Obstacle-avoiding robot

What you’ll build

A robot car that drives forward, stops when something is ahead, looks for the clearest way and turns that way. It’s a complete sense–think–act robot.

Parts

Part Qty Notes
2WD robot car chassis with 2 TT gear motors and wheels 1 Includes a caster
Arduino UNO (or compatible) 1
L298N or TB6612FNG motor driver 1
6 × AA battery holder with switch 1 About 9 V with alkalines
Jumper wires (male–male and male–female) ~20
HC-SR04 ultrasonic sensor 1 Front mounted
SG90 servo (optional) 1 To turn the sensor and “look” left and right

Wiring

Motors as in the table below; HC-SR04: VCC → 5V, GND → GND, TRIG → 3, ECHO → 2. Optional servo signal → pin 11, powered from 5V (or a separate 5 V supply).

Driver pin Arduino pin Purpose
ENA 5 (PWM) Left motor speed
IN1 / IN2 7 / 8 Left motor direction
ENB 6 (PWM) Right motor speed
IN3 / IN4 9 / 10 Right motor direction
12V (VM) Battery + (through the switch) Motor power
GND Battery − and Arduino GND Shared ground
5V Arduino VIN is fed from the battery instead Leave the driver’s 5V output unused, or check your board’s jumper

How it decides

  1. Measure the distance ahead.
  2. If it’s more than 25 cm, drive forward.
  3. Otherwise stop, reverse briefly, then look left and right (turning the servo, or turning the robot) and pick the side with more space.
  4. Repeat.

Tips

  • Mount the sensor level and at the very front; a low angle makes it see the floor.
  • Narrow obstacles (chair legs) can slip past one sensor: a servo sweep or two angled sensors helps.
  • Reverse a little before turning, so the robot doesn’t clip the obstacle while spinning.

Finished this lesson?