MyRoboPathOpen Robotics Lab
Basic Robots Track #4 of 7
Est. 4 Hours
Budget: $24 - $34
Beginner

Ultrasonic Obstacle Avoider

Construct a 2WD autonomous obstacle-avoiding rover that scans its environment using an HC-SR04 sonar transducer on an SG90 sweeping servo.

A pivotal milestone project in mobile robotics! This build equips the 2WD smart rover with an HC-SR04 ultrasonic distance scanner mounted on an SG90 micro servo. The robot continuously measures distances in front of it; when a barrier is encountered, it stops, pans its sonar eye 180° left and right, compares spatial clearance, and steers into the open path.

View Firmware on GitHub
Distance SensingHC-SR04 UltrasonicSG90 Mini ServoConditional Logic (if/else)Servo Sweeping

Bill of Materials (BOM) & Hardware Components

ComponentSpecificationsQtyApprox Cost
HC-SR04 Ultrasonic Distance Sensor2cm - 400cm sensing range, 40kHz sonar burst, 4-pin interface1$2.50
SG90 9g Micro Servo Motor & Horn Kit1.8 kg.cm torque, 0°-180° rotational angle control1$2.50
Ultrasonic Pan Bracket HolderCustom acrylic / 3D-printed mounting tower for HC-SR04 on SG901$1.50
2WD Smart Robot Chassis + MotorsAcrylic chassis, TT gearmotors, wheels, caster, hardware1$11.00
Arduino Uno / Nano + L298N DriverATmega328P microcontroller with dual H-bridge motor driver1$8.50

Electrical & System Architecture

HC-SR04 Trig connects to Arduino D12; Echo connects to D11. SG90 servo PWM signal connects to D9. L298N driver inputs connect to D5-D8. 7.4V battery pack powers motors and microcontroller with common ground.

Figure 4: Complete wiring schematic: HC-SR04 ultrasonic sensor, SG90 scanning servo, and L298N motor driver.
Figure 4: Complete wiring schematic: HC-SR04 ultrasonic sensor, SG90 scanning servo, and L298N motor driver.Wiring Schematic

Step-by-Step Assembly & Configuration Tutorial

Follow each milestone step with photos, wiring checks, and testing procedures.

1

Mounting SG90 Mini Servo & Ultrasonic Pan Bracket

Duration: 1 Hour

Mount the SG90 micro servo upright at the front center of the top chassis plate. Zero the servo horn to 90° and screw on the HC-SR04 sensor bracket.

Step 1: Installing the SG90 pan servo tower and HC-SR04 sensor bracket.
Step 1: Installing the SG90 pan servo tower and HC-SR04 sensor bracket.Step 1 Photo
Step Action Checklist:
  • Zero the servo by uploading a temporary sketch setting `servo.write(90)` before attaching the horn.
  • Press the acrylic bracket onto the servo output gear splines facing dead-center ahead (90°).
  • Fasten the center retention screw firmly into the servo output shaft.
  • Clip the HC-SR04 sensor into the dual circular eye cutouts on the bracket.
Engineering & Tuning Tip: Leave 5cm of slack in the sensor DuPont wires so they do not catch or disconnect when the servo pans 180° during scans.
2

Interfacing HC-SR04 & SG90 Servo to Microcontroller

Duration: 1 Hour

Wire the ultrasonic sensor Trigger/Echo pins and the servo PWM control lead to the Arduino digital GPIO headers.

Step 2: Connecting sensor Trigger/Echo pins and servo PWM signal to Arduino.
Step 2: Connecting sensor Trigger/Echo pins and servo PWM signal to Arduino.Step 2 Photo
Step Action Checklist:
  • Connect HC-SR04: VCC -> 5V, GND -> GND, Trig -> Pin 12, Echo -> Pin 11.
  • Connect SG90: Brown/Black wire -> GND, Red wire -> 5V, Orange/Yellow wire -> Pin 9.
  • Verify all power connections share a single common ground bus on the breadboard.
Engineering & Tuning Tip: If using an ESP32 (3.3V logic) instead of an Arduino Uno, use a 1kΩ/2kΩ voltage divider on the Echo pin to protect 3.3V GPIO inputs from 5V pulses.
3

Programming Sonar Math & Look-Ahead State Machine

Duration: 2 Hours

Implement the ultrasonic echo timing formula (`distance = duration * 0.0343 / 2`) and the look-ahead obstacle avoidance navigation algorithm in C++.

Step 3: Programming distance timing math and sweeping avoidance state machine.
Step 3: Programming distance timing math and sweeping avoidance state machine.Step 3 Photo
Step Action Checklist:
  • Trigger HC-SR04 with a 10µs HIGH pulse, then read duration using `pulseIn(echoPin, HIGH)`.
  • Implement main loop state machine: Drive Forward -> If distance < 25cm -> Stop -> Reverse 200ms -> Look Left (160°) -> Look Right (20°) -> Compare clearances -> Turn toward side with greatest distance -> Resume Forward.
  • Place obstacles (cardboard boxes, chair legs) in a room and test autonomous navigation.
Engineering & Tuning Tip: Adding a quick 200ms reverse pulse before turning prevents the front bumper from getting wedged against corners.