
The functionality of the robot car we describe today is similar to the functionality of the previous robot car article. This robot car can avoid obstacles. An ultrasonic sensor is mainly used for this purpose. We can get the distance through this sensor. Also, we can do this by calculating the obstacle distance range. Next, the servo motor is used to rotate the ultrasonic sensor left and right. This robot uses two toy cars and popsicle sticks for the motors, wheels, and chassis. Because I do this design at a low cost. So, all these components are controlled via the Arduino Uno board. For that, you can use any other Arduino board.
Ok, let’s see how does work this robot car.
This starts to move forward when the power is applied for the first time. After, the robot car then stops when the ultrasonic sensor reaches a distance of less than 20 cm. After even, through the servo motor, the ultrasonic sensor turns left and right and calculates the distance to the obstacles on either side. Then, turn to the long-distance and going forward. This process continues. Okay, now let’s talk about how to do this project step by step. Below are the components you need for this. Also, you can buy these components easily using the given links.
Components.
- Arduino Uno board x 1. — Amazon / Our Store
- Motor driver board x 1. — Banggood / Our Store
- Ultrasonic sensor x 1. — Banggood / Our Store
- Servo motor x 1. — Banggood / Amazon
- Toy cars x 2.
- Popsicle sticks.– Banggood / Amazon
- Jumper wires.— Amazon / Our Store
- Phone battery x 2.
Step 1








Step 2
First, remove the two toy cars and get the parts we need.



Step 2





Step 3

Step 4

Step 5


Step 6





Step 7

Step 7
#include <Servo.h>
Servo myservo;
const byte servostart = 72; //servo motor start point
int distanceleft = 0;
int distanceright = 0;
long t, cm;
void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, INPUT);
myservo.attach(11);
start();
rotete();
}
void loop() {
getdistance();
int leftdistance = 0;
int rightdistance = 0;
if (cm <= 20) {
robostop();
Serial.println("robo stop");
delay(100);
backward();
Serial.println("robo backward");
delay(300);
robostop();
Serial.println("robo stop");
delay(200);
leftdistance = leftsee();
Serial.println(leftdistance);
delay(200);
rightdistance = rightsee();
Serial.println(rightdistance);
if (leftdistance >= rightdistance) {
turnleft();
delay(500);
robostop();
Serial.println("turnleft");
} else {
turnright();
delay(500);
robostop();
Serial.println("turnright");
}
} else {
forward();
Serial.println("forward");
}
}
void start() {
//myservo.write(servostart);
delay(3000);
for (int a = 0; a < 4; a++) {
myservo.write(servostart);
delay(50);
myservo.write(40);
delay(50);
myservo.write(90);
delay(50);
myservo.write(servostart);
}
}
void rotete() {
delay(500);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(2000);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
void forward() {
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
}
void backward() {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
}
void robostop() {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
void turnleft() {
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
}
void turnright() {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
}
int leftsee() {
myservo.write(servostart);
delay(1000);
myservo.write(155);
delay(1000);
distanceleft = getdistance();
//Serial.println(distanceleft);
myservo.write(servostart);
return distanceleft;
}
int rightsee() {
myservo.write(servostart);
delay(1000);
myservo.write(5);
delay(1000);
distanceright = getdistance();
//Serial.println(distanceright);
myservo.write(servostart);
return distanceright;
}
int getdistance() {
digitalWrite(2, LOW);
delayMicroseconds(4);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
t = pulseIn(4, HIGH);
cm = t / 29 / 2;
//Serial.println(cm);
return cm;
}
Step 8


Step 9

Step 10


Ok, now power on your robot car and enjoy it. Watch the full video guide below.
Obstacle avoidance robot (L298N motor driver)
Try to do the project below. For that, use the video guide. The above code has been used for that.
So, we will meet in the next project. Have a nice day. Bye-bye.