Obstacle avoidance robot (L298N motor driver)

    Hello guys, welcome back to my SrTtu Hobby blog. Today we are going to learn how to make an obstacle avoidance robot at a low cost. Do you remember we created an obstacle avoidance robot in the last article? For this, we used a motor shield with an L293D IC. Through this motor drive shield, we were able to control four motors. Click this link and read it. But today we will use the L298N motor driver board for this robot car. We can only control two motors through this motor driver board. Click this link for more information.

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.

Ok, let’s do this project step by step.

Step 1

Let’s identify these components.
Arduino Uno board.
You can use any other Arduino board.
Motor driver board.
Click this link for more information.
Ultrasonic sensor

 

This component is used to get the distance. Click this link for more information.
Servo motor
This component is used to rotate the Ultrasonic sensor. Click this link for more information.
Toy cars
These toy cars used to get motors and wheels for the robot.
Popsicle sticks
This material is used to make the robot car chassis.
Jumper wires
Use these jumper wires to connect these components.
Phone battery
You can use any other power source. But I’m using a low-cost method. Also, this is good for this project.

Step 2

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

Step 2

After, set up the robot car chassis using popsicle sticks.

 

 

 

 

Step 3

Next, attach the Arduino Uno board to the top of the chassis.

Step 4

Then, attach the motor driver board near to the Arduino board. Attach as you like

Step 5

After, attach the servo motor and the ultrasonic sensor as shown in the pictures below.

 

Step 6

Next, connect these components to the Arduino board. To do this, use the circuit diagram below.

 

 

 

Step 7

Okay, now we’re done with the hardware side of the robot car. Then, connect this robot car to the computer to upload the program.

Step 7

Ok, Now let’s see the code.
#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;
}

Download this code — Click me.

For the first time, the servo motor library in the Arduino IDE is included in the code. After, created a servo object. It is called a “myservo”.  Next, create a byte variable to define the servo start point. The other code lines are very simple. Please read it carefully and use it for your own creations.

Step 8

Now select the correct board and port.

 

Step 9

Finally, upload this code.

Step 10

Ok, now connect your power source to the robot car. For that, I use two phone batteries. But you can use any other power source. You must remember it’s 7.2 to 12V DC.

 

Ok, now power on your robot car and enjoy it. Watch the full video guide below.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *