Distance sensing and decisions

Arduino Obstacle-Avoiding Robot: Build Guide

An obstacle-avoiding robot combines distance measurement with a movement decision. This reference build uses an Arduino UNO Rev3, Arduino Motor Shield Rev3 and a standard 5 V HC-SR04. It moves forward on a valid clear reading, makes a short right turn near an obstacle and stops on an invalid echo. It is a supervised floor-level learning demonstration; a front ultrasonic sensor cannot protect stairs, rear space or every obstacle. No physical test results are claimed here.

By Robotic Sir · Learning guide

What you need

  • Arduino UNO Rev3 and Arduino Motor Shield Rev3 with its brake connections intact
  • Standard 5 V HC-SR04 ultrasonic module, mounted facing forward
  • Two compatible geared DC motors, wheels, a chassis and castor
  • A switched motor supply checked against the motor and shield ratings
  • USB data cable, Arduino IDE, wiring and an accessible power switch
  • A clear floor-level practice area and a large flat target

1. Connect the sensor and motor base

With power disconnected, fit the shield to the UNO and connect the left motor to channel A and the right motor to channel B. Secure the sensor so its transducers face the target and cables cannot enter the wheels. Follow the shield's motor-power and VIN Connect guidance before connecting USB and an external supply.

This reference uses UNO Rev3 5 V logic. The standard HC-SR04's Echo is a 5 V signal. For a 3.3 V board such as an ESP32, use an appropriate level-shifting arrangement and a separately checked pin map; do not connect that Echo signal directly to its GPIO.

1. Connect the sensor and motor base: reference table
ConnectionReference wiring
HC-SR04 VCC / GNDUNO 5V / GND, with a common ground for the control signals
HC-SR04 Trig / EchoD6 / D7
Left motor on shield channel ADirection D12, PWM D3, brake D9
Right motor on shield channel BDirection D13, PWM D11, brake D8
Motor powerUse the shield's documented supply arrangement, compatible with your motors

2. Upload the sketch and check the decision sequence

Save the download in a folder named uno-obstacle-avoider, open it in Arduino IDE, select UNO and the correct port, then verify and upload. Open Serial Monitor at 115200 baud. G starts the demonstration; S stops it. Keep the wheels raised until direction, stopping and sensor behaviour have been checked.

The starting settings are a 25 cm clearance threshold, a 70 ms measurement interval and a 120 ms right turn. These are values to evaluate on your build, not measured stopping distances or universal settings. Use the physical switch to cut power when needed; software commands are not a substitute for that switch.

Download UNO obstacle-avoidance sketch (.ino)
Movement decision (pseudocode)
wait for a start command
measure the echo with a bounded timeout
if the reading is invalid: stop and require a restart
if the obstacle is within the clearance threshold:
  make a short right turn, stop, then measure again
otherwise: drive forward
accept the stop command between control steps

3. Check valid readings and timeout behaviour

The HC-SR04 measures the echo's round-trip time. The code generates a 10 microsecond trigger and estimates centimetres from the pulse duration divided by 58. A missing pulse or a result outside the selected module's nominal 2–400 cm range is treated as invalid, rather than as an empty path.

Use a broad flat target at known distances before trying movement. Then check angled and less reflective targets to observe the limits. If no echo returns, the sketch stops and requires inspection and G to restart. It deliberately does not reverse into space that the front sensor cannot observe.

  • Check the labelled pins and common ground if every reading times out.
  • Verify both wheel directions with the base raised before floor operation.
  • With the motor supply disconnected, inspect the trigger, echo and timeout code if behaviour differs from the expected sequence.
  • Use a clear area away from edges; keep people, loose items and pets out of the demonstration space.

4. Tune from observations rather than a guessed safe distance

Begin with low motor commands and ample open space. Measure where the robot actually stops or changes direction relative to the target. Account for the sampling interval, the echo timeout, wheel motion and mechanical response when choosing a clearance threshold.

The turn is a timed demonstration step with one wheel stopped; it is not mapping or path planning. If it repeatedly turns without finding a valid clear path, stop and reposition the build. Record changes in motor commands, turn duration, target material and battery condition.

5. Troubleshoot the input before changing the drive code

5. Troubleshoot the input before changing the drive code: reference table
SymptomNext check
Robot stops immediately after GCheck sensor power, Trig/Echo order and whether the target returns a valid echo.
Robot keeps turning near the same objectInspect available turning room and the target angle; stop and reposition rather than assuming a route exists.
Obstacle is missedTest target size, material and angle; a single ultrasonic sensor does not detect every object.
Controller resets when movingInspect motor-power delivery, grounding, loose connections and the shield power plan.
An ESP32 input is connected to EchoDisconnect power and check the required 5 V-to-3.3 V signal conversion before using that board.

References and further learning

Project questions

Frequently asked questions

Practical answers to help you choose the next step.

This reference uses a distance threshold and timed movement decisions, so it does not need AI. Mapping, object recognition and more advanced navigation are different projects with additional requirements.
Treat a missing echo as an invalid reading, not proof that the route is clear. This reference stops and requires inspection and a new start command.
A standard HC-SR04 has a 5 V Echo output. An ESP32 GPIO needs an appropriate compatible input level, so check the exact sensor and board documentation and use a suitable level-shifting arrangement.

Build your understanding

Keep learning with another project

Choosing a project

Choose a Robotics Project: Parts & Skills

Choose a school or beginner robotics project by learning goal, parts and experience. Compare LED, line-follower, obstacle-avoider and soccer-robot builds.

Read the guide

Sensors and motor control

Arduino Line Follower Robot: Build & Calibrate

Build a reference Arduino line follower with a Motor Shield Rev3 and analog reflectance array. Includes wiring, downloadable code and calibration checks.

Read the guide

Choosing hardware

Arduino vs ESP32 for Beginner Robotics

Compare Arduino UNO Rev3 and classic ESP32-DevKitC for beginner robotics: logic levels, wireless features, coding, motor drivers and project fit.

Read the guide
Browse all learning guides