How to make an obstacle avoidance robot car using three ultrasonic sensors

Hello and welcome back. In this project, we will learn how to make an obstacle-avoidance robot car using three ultrasonic sensors. I have mainly used the Arduino Nano board and the L293D motor driver IC to control this car. The chassis of the robot car is crafted from a useless material, allowing for easy modifications to suit your preferences. I designed a PCB specifically for this project, making it easier to install all the components without messy wires. If you want to create your PCBs for projects like this, I recommend using JLCPCB for reliable manufacturing services. Also, you can change the power source to a 9v battery. I have used two Li-ion batteries. Feel free to adjust the design and components to meet your requirements and preferences.
OK, let’s do this project step by step. The required components are given below.
- Arduino Nano board — Our store / Amazon
- Ultrasonic sensor x 3 — Our Store / Amazon
- L293D IC x 1 — Our store / Amazon
- 16 Pin IC socket x 1 — Our store / Amazon
- Gear motor x 2 — Our Store / Amazon
- Robot wheel x 2 — Our Store / Amazon
- Li-ion battery x 2 — Amazon / Our store
- Caster wheel x 1 — Our Store / Amazon
- Battery holder x 1 — Our Store / Amazon
- Two Pin terminal x 3 — Our store / Amazon
- Female header x 2 — Our store / Amazon
Step 1
Firstly, identify these components.











Step 2
Secondly, let’s order PCBs for this robot car. Follow the instructions below for that.
- First, go to the JLCPCB official website and log in to your account. If you are a new member of this, please use this link. Then, you can get a 60$ new user coupon for orders to PCBs. Also, you can order 6 Layer PCBs using the following links.
- Mid-Year Mega Sale $250 Coupon & More Surprises: Click on me
- Limited Time Offer: Get $20 OFF on JLCPCB 6-Layer PCBs! — Click on me


- Now, click the instant quote button and upload the Gerber file. You can download it using the link below.
- Gerber file — Download
- I have ordered 5 PCBs with black color. Then, select the build time and shipping. Finally, click the save to cart button.





Step 3
Third, when the package is received, unbox your PCB box and check it.




Step 4
Next, solder the IC socket, wire terminals, and female headers to the PCB.





Step 5
Now, let’s make the robot car chassis. For that, I used a piece of rigifoam. You can use any other suitable material and please follow the picture below.















Step 6
Install the L293D motor driver IC, ultrasonic sensors, and Arduino Nano board on the PCB.



Step 7
Next, install this PCB on the robot car chassis. Then, connect the robot wheels to the motors and install them on the chassis.






Step 8
After that, install the caster wheel in the rear center of this chassis.



Step 9
Next, connect the gear motors to the Motor 1 and Motor 2 terminals.





Step 10
Now, install the battery holder and connect the power wires to the power terminal.



Step 11
Let’s upload the Program to this robot car. For that, copy and paste the following program to the Arduino IDE.
- Program and Gerber file — Download
/*Obstacle avoidance robot with three ultrasonic sensors
Home Page
*/
//Define the sensor pins
#define S1Trig A0
#define S2Trig A1
#define S3Trig A2
#define S1Echo A3
#define S2Echo A4
#define S3Echo A5
//Set the speed of the motors
#define Speed 140
//Motor driver pins
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define ENA 9
#define ENB 10
void setup() {
Serial.begin(9600);
//Set the Trig pins as output pins
pinMode(S1Trig, OUTPUT);
pinMode(S2Trig, OUTPUT);
pinMode(S3Trig, OUTPUT);
//Set the Echo pins as input pins
pinMode(S1Echo, INPUT);
pinMode(S2Echo, INPUT);
pinMode(S3Echo, INPUT);
//Set the motor pins as output pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
}
void loop() {
int centerSensor = sensorTwo();
int rightSensor = sensorOne();
int leftSensor = sensorThree();
// Check the distance using the IF condition
if (9 >= centerSensor) {
backward();
delay(50);
Stop();
Serial.println("Stop");
delay(1000);
if (sensorOne() > sensorThree()) {
right();
Serial.println("Right");
delay(300);
} else if(sensorOne() < sensorThree()) {
left();
Serial.println("Left");
delay(300);
}
}
Serial.println("Forward");
forward();
}
//Get the sensor values
int sensorOne() {
//pulse output
digitalWrite(S1Trig, LOW);
delayMicroseconds(4);
digitalWrite(S1Trig, HIGH);
delayMicroseconds(10);
digitalWrite(S1Trig, LOW);
long t = pulseIn(S1Echo, HIGH);//Get the pulse
int cm = t / 29 / 2; //Convert time to the distance
return cm; // Return the values from the sensor
}
//Get the sensor values
int sensorTwo() {
//pulse output
digitalWrite(S2Trig, LOW);
delayMicroseconds(4);
digitalWrite(S2Trig, HIGH);
delayMicroseconds(10);
digitalWrite(S2Trig, LOW);
long t = pulseIn(S2Echo, HIGH);//Get the pulse
int cm = t / 29 / 2; //Convert time to the distance
return cm; // Return the values from the sensor
}
//Get the sensor values
int sensorThree() {
//pulse output
digitalWrite(S3Trig, LOW);
delayMicroseconds(4);
digitalWrite(S3Trig, HIGH);
delayMicroseconds(10);
digitalWrite(S3Trig, LOW);
long t = pulseIn(S3Echo, HIGH);//Get the pulse
int cm = t / 29 / 2; //Convert time to the distance
return cm; // Return the values from the sensor
}
/*******************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 right() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void left() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void Stop() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
- Now, select the board and port. After, click the upload button.




Step 12
Finally, put the batteries into the battery holder and test your robot car.



OK, enjoy this project. The full video guide is below. So, we hope to see you in the next project. Have a good day.
Troubleshooting
- Check the Motor wires.
- Select the correct Arduino board and port.
- Confirm the motor IC and sensor health.
- Check the battery voltage.
How to make an obstacle avoidance robot car using three ultrasonic sensors