Obstacle Avoider Robot Using Arduino and IR Sensor

Obstacle Avoider Robot Using Arduino and IR Sensor

Summary

Discover how to create an Obstacle Avoidance Robot using Arduino and IR sensors in our latest blog! We take you through the step-by-step process, from building the robot and adding essential hardware to incorporating IR sensors for obstacle detection. Plus, don't miss the impressive demo video showcasing the robot in action. Ready to bring your robotics skills to the next level? We've even got the complete code for the project. Get started today and watch your robot maneuver through challenges flawlessly. Don't miss out on this exciting journey into the world of robotics!

Introduction

Obstacle avoider robots powered by Arduino and IR Sensor are the perfect introduction for those eager to learn robotics. Unlike other robotic systems, these sophisticated machines can navigate their environment autonomously using best-in-class infrared detection technology. As a result, they have an unprecedented ability to detect objects in front of them before making decisions about which direction is most suitable and safe from obstacles or any potential obstacles within range. This amazing feature makes it easier than ever before for users with little knowledge on how robots work—and who want something fun yet easy to use—to set up obstacle avoidance navigation tasks quickly and reliably without much fuss! With such powerful sensor technology integrated into these incredible devices, your possibilities with autonomous robotics become endless!

In this blog, we will learn to build an obstacle avoiding robot 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.

 

read more : How to use Buzzer with Arduino

Components required to make obstacle avoiding robot

  1. Arduino Uno (with cable)
  2. L298 Motor Driver 
  3. Range adjustable IR sensor (x3)
  4. Chassis (x1)
  5. B.O. DC Motors (x2)
  6. Battery container (x1)
  7. Wheels (x2)
  8. Castor wheel
  9. Jumper wires: male-to-male|male-to-female|female-to-female (x10)
  10. Switch (x1)
  11. Spacers (x4)
  12. M3*30 screw (x4)
  13. M3*8 screw (x15)
  14. M3 nut (x15)

 

Obstacle Avoider Robot

 

read more :  Arduino Sensor types and Applications

Also, read our blog on the Infrared Sensor detailing the working principle, applications, and different types of infrared sensors.

Step 1: Make the Robot itself

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.

Step 2: Add the hardware required to power robot

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.

Step 3: Add IR Sensor to detect obstacles

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.

read our blog explaining the infrared sensor application, which provides detailed information about infrared sensor, including their working principles, types, advantages, disadvantages, and various applications.

 

DEVELOPER DEMO VIDEO:

 


Code for the obstacle avoidance Robot


#include 

//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. 

read more : Smart Dustbin using Arduino

Conclusion:

Crafting an Obstacle Avoider Robot using Arduino and IR Sensor is a fascinating journey that merges technology with innovation. By seamlessly integrating the components detailed in the guide, from constructing the robot to incorporating essential hardware and the IR sensor for obstacle detection, you've unlocked the potential of autonomous navigation. Witness the brilliance of your creation in action through the developer's insightful demo video, showcasing the robot's prowess. Ready to delve into the coding realm? The provided obstacle avoidance code is your ticket to witnessing your creation smartly navigate its surroundings. Embrace the thrill of robotics and embark on this adventure today!

 

If you appreciate our work don't forget to share this post and leave your opinion in the comment box.

 

Please do check out other blog posts about Popular electronics

 

Make sure you check out our wide range of products and collections (we offer some exciting deals!) 

Components and Supplies

You may also like to read

Frequently Asked Questions

1. What is the IR sensor used for obstacle detection?

The IR sensor is a device that provides obstacle detection for robots, drones and other automated machines. It utilizes infrared radiation to detect obstacles in its path and alert the machine accordingly so it can avoid them. With this knowledge, these devices or vehicles are able to circumnavigate around objects as needed.
Objects which emit heat create an energy signature on electromagnetic spectrums including the near-infrared spectrum; when exposed to such frequencies of light―which our eyes cannot see due to their wavelength ―the sensors detect changes between what’s expected from open space with no barriers versus areas where blockages exist . The result allows machinery precision navigation capabilities without risk of collision damage or disruption of progress. This makes tasks much less time-consuming than traditional methods like manual labor while also allowing greater safety assurance since human error is eliminated altogether by relying solely on technology rather than people delivering results across high-scale projects requiring accurate measurements over long distances quickly & precisely. 

read more : What is the microcontroller used in Arduino UNO?

2. How do you make an obstacle avoiding robot with IR sensor?

Making an obstacle-avoiding robot with an IR sensor is a fun and easy project. It involves programming to make your robot detect obstacles in its path, avoid them and move forward. Start by getting the necessary materials, including batteries for power supply, motor drivers to control the movement of motors connected directly to wheels or tracks of the robot, Arduino board along with other electronic components like Raspberry Pi4 etc., depending if AI based department is considered with several cables connecting parts together as per circuit diagram. Once all material has been gathered it is time for coding which requires some level of understanding of C/C++ language (or whichever one you prefer). The code should include functions that allow proper operation such as sending signals after interruptions from infrared sensors alerting about nearby objects while moving across space & accordingly commands must be deployed through wires so that needed motion changes can occur immediately without any harm caused due too sudden speed adjustments thus achieving desired purpose i.e safe navigation among fixed or mobile environment via integration between hardware & software technology!

read more : Top 10 Arduino Projects to Get Started With

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.

Components and Supplies

You may also like to read