How to Build an Obstacle Avoidance Robot Car Using Arduino – Step-by-Step Guide
Introduction
In this tutorial, you’ll learn how to create an Arduino-based obstacle avoidance robot that:
- Detects obstacles within 45 cm
- Stops, reverses briefly
- Scans left and right using a servo-mounted ultrasonic sensor
- Chooses the best direction and moves forward again
Perfect for intermediate learners and ideal for STEM robotics projects!
Full Video Tutorial – Watch Before You Build
Both of these videos explain how to build an Arduino obstacle avoidance car. The first video was uploaded during the early days of my YouTube channel, and the code and diagram provided are compatible with that version as well. The second video is an improved version, created without the flaws of the original.
This video shows the complete process of building the Arduino obstacle avoidance robot car. It includes step-by-step assembly, wiring instructions, code uploading, and a working demonstration.
Don’t forget to subscribe to our Robot LK channel for more exciting robotics tutorials!
Components Required
- Arduino Uno…………………………………………………AliExpress / Amazon
- 4-wheel chassis kit with DC motors……………….AliExpress / Amazon
- L298N motor driver module………………………….AliExpress / Amazon
- Ultrasonic sensor (HC-SR04)…………………………AliExpress / Amazon
- SG90 servo motor………………………………………..AliExpress / Amazon
- Jumper wires………………………………………………AliExpress / Amazon
- Two 3.7V 18650 Rechargeable Batteries……….AliExpress / Amazon
- 18650 Li-ion Battery Holder 2-Way……………….AliExpress / Amazon
- Toggle Switch……………………………………………..AliExpress / Amazon
- USB cable for programming the Arduino
- Screwdriver and assorted screws
- Glue Gun
Disclosure: Some of the links on this website are affiliate links, which means we may earn a commission if you purchase through them. This comes at no extra cost to you.
Assemble the Chassis
For the Obstacle Avoidance Robot, we are using a 4-wheel chassis kit. However, you can also use a 2-wheel chassis kit if needed. That said, the 4-wheel chassis kit offers several advantages most notably, it provides more space for components and wiring. Here’s how to assemble the 4-wheel chassis kit: 👉 Read the Full Tutorial
Circuit Diagram
Wiring Connections
According to the circuit diagram, connect the modules with proper wiring. For the L298N motor driver, use two micro jumpers to connect the ENA and ENB pins to the 5V output—keep them in place as shown.
For this project, power the system using two 3.7V 18650 Li-ion batteries or any power source that provides more than 7V but less than 10V. Using a standard 9V battery is not recommended, as it often fails to provide sufficient current to run all four motors properly.
Before powering on the Obstacle Avoidance Robot, double-check the wiring to ensure everything is correctly connected. Below, you will find the proper wiring layout between the modules.
L298N Motor Driver to Motors
- The two red wires of the two motors on the right → OUT 4 connector of the L298N motor drive
- The two black wires of the two motors on the right → OUT 3 connector of the L298N motor drive
- The two black wires of the two motors on the left → OUT 2 connector of the L298N motor drive
- The two red wires of the two motors on the left → OUT 1 connector of the L298N motor drive
L298N Motor Driver to Arduino UNO
- Arduino D4 → L298N Driver IN1
- Arduino D5 → L298N Driver IN2
- Arduino D6 → L298N Driver IN3
- Arduino D7 → L298N Driver IN4
Servo Motor to Arduino UNO
- Arduino D10 → The orange wire (Data) of the servo motor
- Arduino 5V → The red wire (VCC) of the servo motor
- Arduino GND → The brown wire (GND) of the servo motor
Ultrasonic Sensor to Arduino UNO
- Arduino 5V → Ultrasonic Sensor VCC
- Arduino GND → Ultrasonic Sensor GND
- Arduino A1 → Ultrasonic Sensor TRIG
- Arduino A2 → Ultrasonic Sensor ECHO
Arduino Code
Section-wise Explanation
Library Imports and Declarations
#include <Servo.h>
#include <NewPing.h>
Servo.h – Built-in library used to control the servo motor for rotating the ultrasonic sensor.
NewPing.h – External library used for efficient ultrasonic sensor readings. You must install this library via the Library Manager.
Motor Pin Definitions
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
These pins control the direction of the robot’s motors via the L298N motor driver.
Ultrasonic Sensor Pins and Setup
#define trig_pin A1
#define echo_pin A2
#define maximum_distance 200
Ultrasonic sensor is connected to analog pins A1 and A2. The maximum detectable distance is defined as 200 cm.
Setup Function
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(10); //our servo pin
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
- Attaches servo motor to pin 10.
- Positions servo at front (115°), then takes multiple distance readings for calibration.
Main Loop Function
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 45){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
Continuously checks for obstacles using distance.
- If an obstacle is closer than 45 cm: Stop and reverse briefly. Scan right and left. Turn in the direction with more space.
- If clear, move forward.
Sensor Scanning Functions
int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
lookRight() Rotates servo to 50° to scan right. lookLeft() Rotates servo to 170° to scan left. Both return measured distances.
Motion Control Functions
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
// Additional functions for moveBackward, turnRight, turnLeft, moveStop are included in the full code below.
Full Arduino Code
#include <Servo.h>
#include <NewPing.h>
//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
//sensor pins
#define trig_pin A1
#define echo_pin A2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor;
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(10);
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 45){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
Upload Arduino code
If you haven’t installed the Arduino IDE or uploaded any code before, click here to read the full guide I wrote on that topic. – click here
Before uploading the code, download the New Ping Library and add it to the Arduino IDE. The NewPing library should be added to the Library of the Arduino IDE. Otherwise, the code cannot be uploaded. Download it from below.
In the Arduino IDE, navigate to Sketch > Include Library > Add. ZIP Library. At the top of the drop-down list, select the option to “Add . ZIP Library”.
After that, select the board and port and upload the code. If this is difficult to do, watch a tutorial video.
Conclusion
Congratulations! You have successfully built an Arduino obstacle avoidance robot car using the L298N motor driver and ultrasonic sensor. This project is a great foundation for learning robotics, sensor integration, and motor control. You can now enhance it further by adding Bluetooth control, line-following capabilities, or even a camera module.