Obstacle Avoiding Line Follower Robot Using Arduino

In previous projects, we talked about various Arduino basis robotics projects. Now we are going to build a two-in-one Arduino robot which is an Obstacle Avoiding Line Follower Robot with some simple components. This is basically a black line follower robot i.e capable of stopping in front of any object or wall.

So let’s get started…

Must Read Line Follower Robot

Obstacle Avoiding Robot

Circuit Diagrams

obstacle avoiding line follower

Components Required

  • Arduino Nano
  • L298N Motor Driver
  • HC-SR04 Ultrasonic Sensor Module
  • 2x IR Sensor Module
  • 9V Battery
  • 2x Geared Motor
  • 2x Wheels
  • Wires

Working of Obstacle Avoiding Line Follower Robot

According to the above circuit diagram, we notice that a microcontroller (atmega328p) is used to communicate with the whole system for processing the robotics functions.

First, we need to connect both Vcc and ground pins of the Arduino Nano with a 9V power supply. As we know this is the main source for operating the whole system.

After connecting the above connection, let’s connect the L298M motor driver to the Arduino respectively (D2-IN1, D3-IN2, D4-IN3, D5-IN4). Also, connect the Vcc and ground of the motor driver to the 9V power supply and connect the middle pin (5V) of the motor driver to both IR Sensors and HC-SR04 Ultrasonic Sensor Module’s Vcc pin. Two geared motors are connected to the outer terminal of the motor driver as shown in the circuit diagram. This is a powerful motor driver that can drive four 12V motors easily.

After that, let’s connect the right IR sensor’s data pin to the A0 pin and the left IR sensor’s data pin to the A1 pin of the Arduino. It helps to detect the black line according to the robot’s path and let it rotate to the correct direction.

Next, we need to connect an Ultrasonic sensor for detecting obstacles in front of its path. Then we need to connect the “Echo” and “Trig” pins to the A4 and A5 pins of the Arduino respectively. It collects the signal by throwing an ultrasound to any obstacles in front of it and then sends this signal by the “Trig” pin.

Thus the Obstacle Avoiding Line Follower Robot works.

Application of Obstacle Avoiding Line Follower Robot

It is in use for the automation processes in industries, military operations, etc.

They are very useful as they can work without any supervision i.e. they work as automatic guided vehicles.

With additional features like obstacle avoidance and other security measures, obstacle avoiding line follower robots can be used in a driverless car.

Install NewPing Library

Click here to install the NewPing Library file.

After finishing the download open Arduino IDE software.

Sketch>> Include Library>> Add .ZIP Library>> Select NewPing ZIP which you download recently>> Open

Arduino Code

//Arduino Line Follower Obstacle Avoidance Robot
//https://circuitdiagrams.in/obstacle-avoiding-line-follower-robot
  
//Created By Electro Gadget

#include<NewPing.h>

int ENA = 3;  //ENA connected to Digital pin D3
int ENB = 9;  //ENB connected to Digital pin D9
int MOTOR_A1 = 2; // MOTOR_A1 connected to Digital pin D2
int MOTOR_A2 = 3; // MOTOR_A2 connected to Digital pin D3
int MOTOR_B1 = 4; // MOTOR_B1 connected to Digital pin D4
int MOTOR_B2 = 5; // MOTOR_B2 connected to Digital pin D5

int RIGHT = A0; // RIGHT sensor connected to Analog pin A0
int LEFT = A1;  // LEFT sensor connected to Analog pin A1

#define TRIG A5 // TRIG PIN connected to Analog pin A5
#define ECHO A4 // ECHO PIN connected to Analog pin A4
#define MAX_DISTANCE 100 // Define Maximum Distance

NewPing sonar(TRIG, ECHO, MAX_DISTANCE); 

void setup() {
  // put your setup code here, to run once:
  
pinMode(ENA, OUTPUT); // initialize ENA pin as an output
pinMode(ENB, OUTPUT); // initialize ENB pin as an output
pinMode(MOTOR_A1, OUTPUT); // initialize MOTOR_A1 pin as an output
pinMode(MOTOR_A2, OUTPUT); // initialize MOTOR_A2 pin as an output
pinMode(MOTOR_B1, OUTPUT); // initialize MOTOR_B1 pin as an output
pinMode(MOTOR_B2, OUTPUT); // initialize MOTOR_B2 pin as an output
pinMode(RIGHT, INPUT); // initialize RIGHT pin as an input
pinMode(LEFT, INPUT);  // initialize LEFT pin as an input

}

void loop()

{
  
delay(70);
int distance = sonar.ping_cm();
if (distance == 0) {
  distance = 30;
}
if(distance <=15) {
  Stop();
  delay(100);
  turnRight();
  delay(350);
  moveForward();
  delay(500);
  turnLeft();
  delay(350);
  moveForward();
  delay(700);
  turnLeft();
  delay(300);
  moveForward();
  delay(400);
  turnRight();
  delay(400);
}
if (analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  digitalWrite(MOTOR_B1, HIGH);
  digitalWrite(MOTOR_B2, LOW);
  
}else if (analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, HIGH);
  digitalWrite(MOTOR_A2, LOW);
  digitalWrite(MOTOR_B1, HIGH);
  digitalWrite(MOTOR_B2, LOW);

}else if (!analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  digitalWrite(MOTOR_B1, LOW);
  digitalWrite(MOTOR_B2, HIGH);
  
}else if (!analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) {
 
  Stop();
}

}
void Stop() {
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
  digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, LOW);
  digitalWrite(MOTOR_B1, LOW);
  digitalWrite(MOTOR_B2, LOW);
}

void turnRight() {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  digitalWrite(MOTOR_B1, LOW);
  digitalWrite(MOTOR_B2, HIGH);
  
}

void turnLeft() {
  analogWrite(ENA,100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, HIGH);
  digitalWrite(MOTOR_A2, LOW);
  digitalWrite(MOTOR_B1, HIGH);
  digitalWrite(MOTOR_B2, LOW);
}

void moveForward() {
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  digitalWrite(MOTOR_B1, HIGH);
  digitalWrite(MOTOR_B2, LOW);
}

/*void turnAround() 
  {
  digitalWrite(MOTOR_B1, HIGH);
  digitalWrite(MOTOR_B2, LOW);

  for (int fadeValue = 70; fadeValue <=200; fadeValue +=10) {
    analogWrite(ENA, fadeValue);
    digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  }

   for (int fadeValue = 200; fadeValue >=70; fadeValue -=10) {
    analogWrite(ENB, fadeValue);
    digitalWrite(MOTOR_A1, LOW);
  digitalWrite(MOTOR_A2, HIGH);
  }
}*/