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.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications | Qty | Approx Cost |
|---|---|---|---|
| HC-SR04 Ultrasonic Distance Sensor | 2cm - 400cm sensing range, 40kHz sonar burst, 4-pin interface | 1 | $2.50 |
| SG90 9g Micro Servo Motor & Horn Kit | 1.8 kg.cm torque, 0°-180° rotational angle control | 1 | $2.50 |
| Ultrasonic Pan Bracket Holder | Custom acrylic / 3D-printed mounting tower for HC-SR04 on SG90 | 1 | $1.50 |
| 2WD Smart Robot Chassis + Motors | Acrylic chassis, TT gearmotors, wheels, caster, hardware | 1 | $11.00 |
| Arduino Uno / Nano + L298N Driver | ATmega328P microcontroller with dual H-bridge motor driver | 1 | $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.
Step-by-Step Assembly & Configuration Tutorial
Follow each milestone step with photos, wiring checks, and testing procedures.
Mounting SG90 Mini Servo & Ultrasonic Pan Bracket
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.
- 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.
Interfacing HC-SR04 & SG90 Servo to Microcontroller
Wire the ultrasonic sensor Trigger/Echo pins and the servo PWM control lead to the Arduino digital GPIO headers.
- 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.
Programming Sonar Math & Look-Ahead State Machine
Implement the ultrasonic echo timing formula (`distance = duration * 0.0343 / 2`) and the look-ahead obstacle avoidance navigation algorithm in C++.
- 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.