Obstacle Avoiding Robot using Arduino

Welcome to the tutorial for the Obstacle Avoiding Robot Car using Arduino, Ultrasonic sensor and L298N Motor Driver.

In case you haven’t watched the YouTube video, I highly recommend that you do, as it will give you a better idea of how the robot might look after it’s done and why you might need a robot like this.

https://youtu.be/zIMCAwsV3VY

https://youtu.be/zIMCAwsV3VY

Parts Required:

Now that you have all the components and tools that you need, we can begin the connections. Please make the connection as per this diagram.

The connections may seem hard and difficult, but just follow the connections for each component at a time. It gets easier.
It’s easier to lay all the components on the chassis first with double sided tape and then make the connections.

Note:

CIRCUIT DIAGRAM CORRECTION: THE POWER SUPPLY FROM THE LI-PO BATTERY IS WRONG IN THE PICTURE. THE RED WIRE (POSITIVE) GOES TO THE 12V TERMINAL IN THE MOTOR DRIVER AND THE BLACK ONE (NEGATIVE) GOES TO THE GROUND TERMINAL OF THE DRIVER.

The same connections will apply for 5V motors. Just change the 12V Li-Po battery for standard 9V batteries. You can buy it from any electrical shop. Anyway here’s a link: https://amzn.to/2HFov4F

If you only want to use 2 motors, then the same connections will apply. Just add a fly wheel at the front center and connect the motors from the motor driver. Just don’t short the two side motors, since there aren’t any. Here’s a link for the fly wheel: https://amzn.to/2UoPtin

If any side motors are rotating in the opposite direction, just reverse those motor connection terminals in the L298 Motor Driver.

Check the video to see how I arranged all the boards and circuits. You can do your’s like that or however else you like.

Programming:

In order to use the ultrasonic sensor and calculate the distance between the sensor and a potential object, we will need a particular arduino library file. You can download that here. Just go ahead and download the latest version. Once downloaded, open the RAR file. Click on the NewPing folder and click extract to. Next you have to extract it to your library folder in the arduino file directory. For me, that was “C:\Program Files (x86)\Arduino\libraries“.

Now that you have the necessary libraries (the libraries for the other components are built into the arduino IDE), you can download this arduino file.

If the link doesn’t work just copy the below code and paste into the arduino IDE sketch: (read the “//” comments for better clarification).

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

Now just compile and upload the code to the Arduino.

It should work perfectly. But if it doesn’t, just comment down here and I’ll clarify it as soon as possible.

Please consider subscribing to my YouTube channel, I worked really hard on this project (building the robot, programming, video, website tutorial, custom parts links, etc). I have a lot more blogs like this and other fascinating topics on this website, so please stick around and check a few of them out. Please do follow if you like them.

Thank You .

One thought on “Obstacle Avoiding Robot using Arduino

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑