1. Introduction
In the world of robotics, obstacle avoidance is a fundamental behavior. This project demonstrates how to build a 4-wheel drive (4WD) robot that "sees" the world using an ultrasonic sensor. When it detects an object, it stops, scans its surroundings with a servo motor, and decides the best path to take—all without any human input.
2. Components
To build this robot, you will need:
Microcontroller: Arduino Uno R3
Motor Driver: L298N Module (Dual H-Bridge)
Sensor: HC-SR04 Ultrasonic Sensor
Actuator: SG90 Servo Motor
Motors: 4x DC Gear Motors (6V-9V)
Power: 2x 18650 Li-ion Batteries (7.4V total)
Chassis: 4WD Robot Car Kit
Misc: SPST Switch, Jumper Wires, Breadboard (optional)
3. Circuit and Connections
Motor Driver (L298N) to Arduino:
| Component | Pin | Arduino Connection |
| L298N IN1 / IN2 | D4 / D5 | Left Motors Control |
| L298N IN3 / IN4 | D6 / D7 | Right Motors Control |
| Servo Signal | D10 | Neck Movement |
| Ultrasonic Trig | A1 | Sound Output |
| Ultrasonic Echo | A2 | Sound Input |
Ultrasonic Trig: Arduino A1
Ultrasonic Echo: Arduino A2
Servo Signal (Orange): Arduino D10
VCC/GND: All connected to Arduino 5V and GND pins.
Ultrasonic Trig: Arduino A1
Ultrasonic Echo: Arduino A2
Servo Signal (Orange): Arduino D10
VCC/GND: All connected to Arduino 5V and GND pins.
Power Routing:
Battery (+): To Switch → L298N 12V Terminal.
Battery (-): To L298N GND Terminal.
Arduino Power: Connect L298N 5V out to Arduino 5V pin (Common GND is essential).
Battery (+): To Switch → L298N 12V Terminal.
Battery (-): To L298N GND Terminal.
Arduino Power: Connect L298N 5V out to Arduino 5V pin (Common GND is essential).
4. Circuit Working
The L298N Motor Driver acts as the high-power bridge between the batteries and the motors. Since the Arduino pins cannot provide enough current to spin four motors, the L298N takes logic signals (5V) from the Arduino and switches the higher battery voltage (7.4V) to the motors. The Ultrasonic Sensor acts as the eyes, and the Servo acts as the neck, allowing the "eyes" to rotate 180 degrees.
5. Code
Copy and paste this code into your Arduino IDE:
6. Code Working
The script uses a conditional logic flow:
Sensing: The
getDistance()function triggers the ultrasonic sensor.Comparison: If
distanceis less than 25cm, theloop()pauses the motors.Scanning: The
look()function moves the servo, takes a reading, and returns to center.Action: The
if (distRight >= distLeft)statement decides which side has more "room" and tells the motors to rotate in opposite directions to spin the robot toward the clear path.
7. Tips
Speed Control: If your robot moves too fast, you can use analogWrite() on the L298N Enable pins (ENA/ENB) to slow it down.
Sensor Noise: If the robot shakes or stops for no reason, check for loose wires on the Echo and Trig pins.
Turn Calibration: Adjust the delay(500) in the turnRight and turnLeft functions to ensure your robot turns exactly 90 degrees on your specific floor surface.
Speed Control: If your robot moves too fast, you can use analogWrite() on the L298N Enable pins (ENA/ENB) to slow it down.
Sensor Noise: If the robot shakes or stops for no reason, check for loose wires on the Echo and Trig pins.
Turn Calibration: Adjust the delay(500) in the turnRight and turnLeft functions to ensure your robot turns exactly 90 degrees on your specific floor surface.
8. Uses
Education: Great for learning about pulse-width modulation (PWM) and ultrasonic physics.
Commercial: Concepts used in robotic vacuum cleaners (like Roomba) and warehouse AGVs.
Development: A base platform for adding Bluetooth control or Wi-Fi cameras later.
Education: Great for learning about pulse-width modulation (PWM) and ultrasonic physics.
Commercial: Concepts used in robotic vacuum cleaners (like Roomba) and warehouse AGVs.
Development: A base platform for adding Bluetooth control or Wi-Fi cameras later.
9. Conclusion
The Arduino Obstacle Avoiding Robot is a milestone project for any maker. It combines motor control, distance sensing, and logic into one autonomous package. Once you master this, you can easily add infrared sensors for line following or a Wi-Fi module for IoT control.