How to make an Obstacle avoidance robot (L298N motor driver)

How to make an 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 go 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.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Let’s identify these components.

Step 2

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

Step 3

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

Step 4

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

Step 5

How to make an Obstacle avoidance robot (L298N motor driver)

Step 6

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

Step 7

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

How to make an Obstacle avoidance robot (L298N motor driver)

Step 8

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.

How to make an Obstacle avoidance robot (L298N motor driver)

Step 9

Ok, Now let’s see the code.

  • The complete program of this project – Download
#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;
}

For the first time, the servo motor library in the Arduino IDE is included in the code. After, create 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 creations.

Step 10

Now select the correct board and port. After, click the upload button.

Step 11

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. The full video guide is below. So, we will meet in the next project. Have a good day.

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.

Obstacle avoidance boat

Similar Posts

Leave a Reply

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