Robotics is an interesting and rapidly growing sector in this era. As a branch of engineering, the applications of robotics are numerous.  It is Increasing with the advancement of technology at a decent pace. Previously we made Line Follower Robot and Surveillance Robot. Obstacle avoidance refers to the ability of a robot to detect obstacles in its way and to find its own way to move without collision. So in this tutorial, we are going to make obstacle avoiding robot with an L298N motor driver module using Arduino and ultrasonic sensors.  It is an Arduino-based robot that uses Ultrasonic sensors to avoid collisions. So let's build the project.

 

Ultrasonic Sensor Pinout

HC-SR04 Pinout

The sensor has 4 pins. 5volts is supplied to the VCC pin and GND is connected to the GND of Arduino. There are two other pins called TRIG and ECHO pins. The Trig pin transmits an ultrasonic wave and the ECHO pin receives the reflected signal from the object.

Ultrasonic Sensor Datasheet

Model No.

HC-SR04

Working Voltage

5v

Working Current

15mA

Working Frequency

40Hz

Max Range

4m

Min Range

2cm

Measuring Angle

15 degree

 

Ultrasonic Sensor Working

Ultrasonic Sensor

Ultrasonic sensors work on the principle that it emits waves at a higher frequency that cannot be heard by humans. Then sensor waits for the wave to be reflected and calculates the distance of how far is the object when in front of the sensor. The practical Distance that can be measured is 2cm to 80cm but theoretically, it is mentioned that the distance can be up to 400 cm.

 

Servo Motor Pinout

Servo Motor Pinout

The MG90S is a metal gear servo motor commonly used in RC applications.  This  servo motor is commonly used in RC applications which can rotate 90 degrees in both the direction or approximately 180 degrees in total.

 

L298N Pinout

 L298N Pinout

Datasheet

Motor controller

L298N, drives 2 DC motors

Operating Voltage

5- 35V

Logic voltage

4.5 – 7 V 

Max current

2A per channel

Voltage Regulator

78M05

Module dimensions

43 x 43 x 28 mm

Junction operating temperature

-25 to 130 o Celsius

 

Components Required

 

Software Required

  • Arduino IDE

 

Circuit Diagram

 Obstacle Avoiding Robot Circuit Diagram

Follow this table in order to make your connections.

L298N driver Pins

Arduino Pins

ENA

jumper

IN1

4

IN2

5

IN3

6

IN4

7

ENB

jumper

 

Ultrasonic Sensor Pins

Arduino Pins

Vcc

5v

GND

GND

TRIG

A1

ECHO

A2

 

Servo Pins

Arduino Pins

Vcc

5v

GND

GND

Signal

10

 

For L298N motor driver module, motors are connected to the motor terminals of the motor driver module. The motor driver has another 3 terminals, in which one is 12Volt connected to the battery. The GND terminal is connected to the battery’s negative terminal and it is also connected to the Arduino board GND pin and 5v is connected to “Vin” of the Arduino board to power the Arduino board.

Note: Install the following library into the library folder of the Arduino IDE : New ping library

 

Arduino Code

#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 = 5;

const int RightMotorBackward = 4;

//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 <= 35){

    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(250);

  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(250);

  digitalWrite(LeftMotorForward, HIGH);

  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);

  digitalWrite(RightMotorBackward, LOW);

}

Troubleshooting

Robot is moving too fast?

For this change value of motor speed in the code. Note that 1 is the minimum value and 255 is the maximum speed.

No power supply?

Try changing your batteries. Ensure that you must supply  12v to the L298 Motor Driver module. A Voltage rating less than this will not work.

Project not working?

Ensure that the circuit you have made is according to the circuit diagram. Check for loose connections. Cross-check for motor connections.

Motors are rotating in opposite direction?

For this, reverse the terminals of the motor connecting the Motor driver module.

Leave a comment

Please note, comments must be approved before they are published

Your cart

×