OBSTACLE AVOIDER ROBOT USING ARDUINO AND IR SENSOR

In this blog, we will learn to build an obstacle avoiding bot based on Infra-Red sensors. The primary purpose of this bot is to traverse forward autonomously and change directions if it encounters any significant obstacle in its path. The bot will avoid any obstacle big enough to be detected by the Infra-Red sensor mounted on the front.

Components required to make the bot:

Obstacle Avoider Robot


The first step is to make the bot itself, which comprises two driven wheels and a caster wheel at the back. The two front wheels are driven by 5V-12V B.O. Motors. The caster wheel is attached at the rear of the
bot. This caster wheel helps the rear of the bot move freely as well as provides stability to the entire bot.

The next step is to add the hardware required to power this bot and drive the wheels. We will be using an 
L298 motor driver to drive the wheels and the entire system will be powered by a 3S (11.1V) LiPo battery. This dual bidirectional motor driver is based on the very popular L298 Dual H-Bridge Motor Driver IC. This module will allow you to control the two motors in both directions easily and independently. We are also using a switch which will make turning on and off the bot easier.

Now, moving on to the obstacle avoiding capabilities of the bot, we will be using a combination of 3 IR sensors to detect any obstacles and the sensor data is processed by an 
Arduino Uno R3 to drive the motors accordingly. The code is fairly simple and can be broken down into 3 sections.

  1. Declaration of all the variables and functions that we will be using in the program.
  2. Setup function: In this, we declare pins 6, 9, 10 and 11 as output pins, these pins have PWM functionality which means we can control the speed of the bot. These pins are connected to the input pins of the L298 motor driver. Additionally, two more GPIO pins have been made high to provide 5V to the IR sensors mounted on the front.
  3. In the loop function, we keep reading the analog sensor value from the IR sensors, a flag is raised if an obstacle is detected by the bot. The potentiometer can be used to tune to the sensor to detect an obstacle at a certain distance. Once an obstacle is detected by the IR sensors, a flag is raised and the program enters a certain loop, in this loop, the bot decides to turn left or right depending on which side the obstacle has been detected. In case an obstacle has been detected by the left IR sensor, the bot will move backwards briefly and them turn right. If the obstacle has been detected by the right IR sensor, the bot will briefly move backwards and then turn left. The bot moves back briefly and turns away from the obstacle and starts moving forward again. By default, the bot keeps moving forward until an obstacle is detected.

 

DEVELOPER & DEMO VIDEO: 

 


 

The code for the obstacle avoidance bot is given below:

#include <Arduino.h>

//Declare all the variables being used here:
int centerIR;
int leftIR;
int rightIR;

int leftFlag;
int centerFlag;
int rightFlag;

//Declare all the functions being used here:
void deviateRight();
void deviateLeft();
void forward();
void reverse();
void setup()
{
  // put your setup code here, to run once:
 pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);

 digitalWrite(7, HIGH);
 digitalWrite(8, HIGH);

  Serial.begin(115200);
}

void loop()
{
  // put your main code here, to run repeatedly:
  centerIR = analogRead(A0);
  leftIR = analogRead(A1);
  rightIR = analogRead(A2);

  if (leftIR < 200)
  {
    leftFlag = 1;
  }
  else
  {
    leftFlag = 0;
  }

 if (rightIR < 200)
  {
    rightFlag = 1;
  }
   else
  {
    rightFlag = 0;
  }

  if (centerIR < 200)
  {
    centerFlag = 1;
  }
  else
  {
    centerFlag = 0;
  }

  if ((leftFlag == 1) || (rightFlag == 1) || (centerFlag == 1))
  {

    if (centerFlag == 1)
    {
      if (leftFlag == 1)
      {
        deviateRight();
      }

      elseif (rightFlag == 1)
        deviateLeft();
      else
        deviateLeft();
    }
    elseif (leftFlag == 1)
    {
      deviateRight();
    }
    elseif (rightFlag == 1)
    {
      deviateLeft();
    }
    else
    {
      deviateLeft();
    }
  }

 else
  {
    forward();
  }
}

//Function definitions go here:
void reverse()
{
  Serial.println("Backing up");
  digitalWrite(9, LOW);
  analogWrite(6, 75);
  analogWrite(11, 75);
  digitalWrite(10, LOW);
  delay(1000);
}

void deviateRight()
{
  Serial.println("Right");
  reverse();
  digitalWrite(6, LOW);
  digitalWrite(9, LOW);
  digitalWrite(11, LOW);
  analogWrite(10, 350);
  delay(300);
}

void deviateLeft()
{
  Serial.println("Left");
  reverse();
  analogWrite(9, 350);
  digitalWrite(6, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(300);
}

void forward()
{
  Serial.println("Forward");
  analogWrite(9, 75);
  digitalWrite(6, LOW);
  digitalWrite(11, LOW);
  analogWrite(10, 75);
}

 

Once the code has been uploaded on the Arduino Uno, power on the bot. Before placing it on the ground, please ensure that the bot is behaving in the way it is intended to, you can do this by waving your hand in front of the IR sensors in the front. This will ensure that the bot does not get damaged by any obstacles. 

 

Leave a comment

Please note, comments must be approved before they are published