What does this project do?
We build an autonomous robot car that detects obstacles in its way and changes direction by itself. The robot keeps driving forward; when the ultrasonic sensor sees an obstacle closer than 20 cm, it stops, backs up a little, turns right or left and carries on. It’s one of the most popular first projects in robotics.
- Age group: 12 and up
- Level: Advanced
- Time: About 2 hours (including assembling the chassis)
- What you will learn: using a DC motor driver (L298N), controlling motor direction and speed, autonomous behaviour that makes decisions based on sensor data
Parts list
| Part | Qty |
|---|---|
| Arduino Uno (or compatible) | 1 |
| 2WD robot car chassis (2 DC motors, 2 wheels, caster wheel) | 1 |
| L298N motor driver board | 1 |
| HC-SR04 ultrasonic distance sensor | 1 |
| 2x 18650 battery holder + 2 18650 batteries (or a 6x AA battery holder) | 1 |
| On/off switch | 1 |
| Mini breadboard | 1 |
| Male-to-male and male-to-female jumper wires | ~20 |
| Screws, nuts, double-sided tape | – |
Wiring

Power connections
| From | To |
|---|---|
| Battery + (through the switch) | L298N 12V input and Arduino VIN |
| Battery – | L298N GND and Arduino GND (the GNDs must be connected together) |
Motor driver and sensor connections
| Component | Component pin | Arduino pin |
|---|---|---|
| L298N | ENA | D5 |
| L298N | IN1 | D7 |
| L298N | IN2 | D8 |
| L298N | IN3 | D9 |
| L298N | IN4 | D10 |
| L298N | ENB | D6 |
| L298N | OUT1 / OUT2 | Left motor |
| L298N | OUT3 / OUT4 | Right motor |
| HC-SR04 | VCC | 5V |
| HC-SR04 | Trig | D2 |
| HC-SR04 | Echo | D3 |
| HC-SR04 | GND | GND |
Remove the small black jumpers on the ENA and ENB pins; we will control the speed from the Arduino.
Code
// Arduino Obstacle Avoiding Robot - ozandamci.com
// Motor pins
const int ENA = 5; // left motor speed
const int IN1 = 7;
const int IN2 = 8;
const int IN3 = 9;
const int IN4 = 10;
const int ENB = 6; // right motor speed
// Sensor pins
const int trigPin = 2;
const int echoPin = 3;
const int speed = 180; // 0-255
const int obstacleDistance = 20; // cm
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
randomSeed(analogRead(A0));
delay(2000); // time to put the robot on the floor after switching on
}
void motors(int leftDir, int rightDir) {
// direction: 1 = forward, -1 = backward, 0 = stop
digitalWrite(IN1, leftDir == 1);
digitalWrite(IN2, leftDir == -1);
digitalWrite(IN3, rightDir == 1);
digitalWrite(IN4, rightDir == -1);
analogWrite(ENA, leftDir == 0 ? 0 : speed);
analogWrite(ENB, rightDir == 0 ? 0 : speed);
}
void forward() { motors(1, 1); }
void backward() { motors(-1, -1); }
void turnRight() { motors(1, -1); }
void turnLeft() { motors(-1, 1); }
void stopCar() { motors(0, 0); }
int measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) return 999; // the way is clear
return duration * 0.034 / 2;
}
void loop() {
int distance = measureDistance();
if (distance < obstacleDistance) {
stopCar();
delay(200);
backward();
delay(400);
// Turn right or left at random
if (random(2) == 0) {
turnRight();
} else {
turnLeft();
}
delay(450);
stopCar();
delay(100);
} else {
forward();
}
delay(50);
}
Step-by-step instructions
- Assemble the chassis. Screw the motors to the chassis, attach the wheels and fix the caster wheel to the front or back. Solder the motor wires, or use motors with screw terminals.
- Mount the boards. Screw or tape the L298N to the middle of the chassis and the Arduino on top of it or behind it. Put the battery holder underneath.
- Connect the motors. Connect the left motor to the L298N’s OUT1/OUT2 terminals and the right motor to OUT3/OUT4.
- Connect the power. Connect the battery’s + wire through the switch to the L298N’s 12V input and the Arduino’s VIN pin, and the – wire to the L298N GND and the Arduino GND.
- Connect the control wires. Connect the ENA, IN1–IN4 and ENB pins to the Arduino as shown in the table.
- Mount the sensor. Fix the HC-SR04 to the front of the robot, facing straight ahead, and connect it as shown in the table.
- Upload the code. Lift the robot’s wheels off the table and upload the code over USB. First check that the wheels turn forwards.
- Test it. Unplug the USB cable, put the robot on the floor and turn on the switch. The robot should drive forward; when you put your hand or a box in front of it, it should back up and turn.
Troubleshooting
- One wheel turns backwards: Swap the two wires of that motor in the L298N terminal.
- The robot doesn’t move at all, but the Arduino is on: Make sure you removed the ENA/ENB jumpers and connected those pins to D5/D6, and that the GNDs are connected together. Check that the batteries are charged.
- The robot spins all the way round instead of turning: Lower the
delay(450)value afterturnRight()/turnLeft()in the code. - The robot doesn’t drive straight: The motors may not turn at exactly the same speed; lower the
analogWritevalue for the faster side a little.
Try it yourself
- Mount the sensor on a servo motor so the robot first “looks” right and left, then turns towards the side with more space.
- Add an HC-05 Bluetooth module so you can also control the robot from your phone.
For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.





“Arduino Obstacle Avoiding Robot Car: Step-by-Step Build” için 2 yorum