Alright — let’s walk through building a DIY Arduino Obstacle Avoiding Car step-by-step.
I’ll give you the parts list, wiring diagram explanation, Arduino code, and how it works so you can make it at home.
1. Parts You’ll Need
| Component | Quantity | Purpose |
|---|---|---|
| Arduino UNO (or Nano) | 1 | Main controller |
| Ultrasonic Sensor (HC-SR04) | 1 | Distance measurement |
| Servo Motor (SG90) | 1 | Rotates the sensor for scanning |
| L298N Motor Driver Module | 1 | Controls DC motors |
| DC Motors with Wheels | 2–4 | Movement |
| Caster wheel | 1 | Support |
| Chassis (DIY or ready-made) | 1 | Body frame |
| Jumper wires | — | Connections |
| Battery pack (6V–12V) | 1 | Power supply |
2. Wiring Connections
Ultrasonic Sensor (HC-SR04) → Arduino
- VCC → 5V
- GND → GND
- TRIG → D9
- ECHO → D10
Servo Motor → Arduino
- Signal → D11
- VCC → 5V
- GND → GND
L298N Motor Driver → Arduino
- IN1 → D2
- IN2 → D3
- IN3 → D4
- IN4 → D5
- ENA → 5V (or PWM pin if speed control)
- ENB → 5V
- Motor A → Left motor
- Motor B → Right motor
- L298N VCC → Battery + (6–12V)
- L298N GND → Battery – & Arduino GND


3. Arduino Code
#include <Servo.h>
Servo myServo;
#define trigPin 9
#define echoPin 10
#define servoPin 11
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
long distance;
long duration;
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert to cm
}
void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void left() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void right() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void stopCar() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void loop() {
myServo.write(90); // Forward facing
delay(200);
distance = getDistance();
if (distance > 20) {
forward();
} else {
stopCar();
delay(300);
myServo.write(0); // Scan left
delay(500);
long leftDist = getDistance();
myServo.write(180); // Scan right
delay(500);
long rightDist = getDistance();
myServo.write(90); // Reset center
if (leftDist > rightDist) {
left();
delay(500);
} else {
right();
delay(500);
}
}
}
4. How It Works
- The servo motor rotates the ultrasonic sensor to scan front, left, and right.
- The Arduino measures distances using the HC-SR04 sensor.
- If the path is clear, the car moves forward.
- If there’s an obstacle, it stops, looks left and right, then chooses the side with more space.
- Motor driver powers the DC motors based on Arduino signals.
5. Tips
- Use a rechargeable Li-ion battery for better performance.
- Reduce servo movement delays for faster reaction.
- Add LEDs to indicate movement direction for fun.

