How to make an Obstacle Avoidance Robot using an Ultrasonic Sensor

Hello and welcome back. In this project, we will learn how to make an obstacle avoidance robot using an ultrasonic sensor. This robot can detect obstacles in front of it and automatically change its direction. For this project, I used the Arduino Nano board because it is small and easy to use. Also, I have not used a servo motor for this project. The ultrasonic sensor is fixed in one position, but the robot can detect obstacles and move left or right based on the distance values.
I used N20 gear motors for movement. These motors are small but powerful, and therefore, we can get a cute shape for the robot. Also, I designed the chassis using a PCB. Therefore, we don’t need an additional chassis for this robot. All the components, like the Arduino Nano, motor driver, ultrasonic sensor, and batteries, can be mounted directly on the PCB. To power up this robot, I used two Li-ion batteries. These batteries provide enough voltage and current to run the motors and the Arduino smoothly. If you want to know how an ultrasonic sensor works or have basic knowledge, please visit our previous tutorials and projects.
Ok, let’s do this project step by step. The required components are given below.
- Arduino Nano board x 1 — Our Store / Amazon
- Ultrasonic sensor x 1 — Our Store / Amazon
- L293D motor driver IC x 1 — Our store / Amazon
- IC base x 1 — Our store / Amazon
- Two-pin terminal x 2 — Our store / Amazon
- Female header x 1 — Our Store / Amazon
- Barrel jack socket x 1 — Our store / Amazon
- N20 gear motor x 2 — Our store / Amazon
- N20 motor wheel x 2 — Our store / Amazon
- Li-ion battery x 2 — Our store / Amazon
- Battery holder x 1– Our store / Amazon
- Caster wheel x 1 — Our store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.










Step 2
Secondly, let’s order the PCB step by step.




- Click the “Instant Quote” button and upload the Gerber file, which you can download from the link below.
- Gerber file – Download
- For this project, I ordered five Yellow PCBs. Next, select the build time and shipping method. Finally, click “Save to Cart” and complete the payment.






Step 3
Thirdly, let’s unbox the PCB package.





Step 4
Next, let’s solder the components onto the PCB.



Step 6
Afterward, let’s connect the N20 motors to the brackets and install them on the chassis.





Step 7
Next, let’s connect the motor wires to the terminals. Then, install the ball caster wheel and N20 motor wheels.






Step 8
Now, let’s install the battery holder and connect it to the DC power port.



Step 9
Afterward, let’s connect the L293D IC, Arduino Nano board, and ultrasonic sensor to the robot PCB. Then, connect the Arduino Nano board to the computer.



Step 10
Next, copy and paste the following program into the Arduino IDE.
- Program — Download
#define trig 2
#define echo 3
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 10
int Speed = 160; // base speed
long duration;
int distance;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
randomSeed(analogRead(A0)); // for random direction
}
void loop() {
distance = getDistance();
Serial.println(distance);
if (distance > 20) {
forward();
}
else {
stopMotors();
delay(200);
backward();
delay(300);
stopMotors();
delay(200);
// 🎲 Random turn
int turn = random(0, 2); // 0 or 1
if (turn == 0) {
turnLeft();
} else {
turnRight();
}
delay(400);
}
}
// ===== Distance Function =====
int getDistance() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
// ===== Motor Functions =====
void forward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void backward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnRight() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnLeft() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}- Now, select the board and port. After, click the upload button.



Step 11
Finally, let’s remove the USB cable and place the batteries into the battery holder. Now you can test your robot. The full video guide is below. So we hope to see you in the next project. Have a great day.



How to make an Obstacle Avoidance Robot using an Ultrasonic Sensor