OBSTACLE AVOIDANCE ROBOT - ARDUINO PROJECT

Link for buying components :


Arduino UNO - https://goo.gl/Rqc5w2

L298N H-Bridge Motor deiver - https://goo.gl/Ucvx6J

HC-SR04 Ultra sonic sensor - https://goo.gl/F0m7hN

SG90 Servo motor - https://goo.gl/iywd5p

DC motor wheel - https://goo.gl/iDy7ep

Car chassis - https://goo.gl/Y8Ch7A




Arduino Library for this project : https://goo.gl/Qe6XU6






Circuit Diagram :  https://goo.gl/PDBwqe




Check out the video : 






About the project : 


This is an OBSTACLE AVOIDANCE ROBOT, Obstacle avoidance is one of the most important aspects of mobile robotics. Without it robot movement would be very restrictive and fragile. This tutorial explains obstacle avoidance using ultrasonic sensors. This project also presents a dynamic steering algorithm which ensures that the robot doesn't have to stop in front of an obstacle.


Before going to working of the project, it is important to understand how the ultrasonic sensor works. The basic principle behind the working of ultrasonic sensor is as follows:
Using an external trigger signal, the Trig pin on ultrasonic sensor is made logic high for at least 10µs. A sonic burst from the transmitter module is sent. This consists of 8 pulses of 40KHz.
The signals return back after hitting a surface and the receiver detects this signal. The Echo pin is high from the time of sending the signal and receiving it. This time can be converted to distance using appropriate calculations.
The aim of this project is to implement an obstacle avoiding robot using ultrasonic sensor and Arduino. All the connections are made as per the circuit diagram. The working of the project is explained below.
When the robot is powered on, both the motors of the robot will run normally and the robot moves forward. During this time, the ultrasonic sensor continuously calculate the distance between the robot and the reflective surface.
This information is processed by the Arduino. If the distance between the robot and the obstacle is less than 15cm, the Robot stops and scans in left and right directions for new distance using Servo Motor and Ultrasonic Sensor. If the distance towards the left side is more than that of the right side, the robot will prepare for a left turn. But first, it backs up a little bit and then activates the Left Wheel Motor in reversed in direction. 
Similarly, if the right distance is more than that of the left distance, the Robot prepares right rotation.  This process continues forever and the robot keeps on moving without hitting any obstacle.
NOTE
  • As the project is based on Arduino, the programming is very easy and can be easily modified.
  • Doesn’t require the Arduino Motor Shield.
  • When using a 9V battery, at least 2 such batteries are needed to power the robot. It is better to use 2 9V batteries (one for Arduino, Ultrasonic sensor, Servo Motor and the other one for L293D and motors).
  • The Ultrasonic sensor should not be connected directly to power supply as it might affect the normal performance.
  • Instead of ultrasonic sensor, an IR transmitter – receiver pair can also be used.

Code for the project :


#include <Servo.h>        // Include Servo Library
#include <NewPing.h>      // Include Newping Library

// L298N Control Pins
const int LeftMotorForward = 4;
const int LeftMotorBackward = 5;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;

#define TRIGGER_PIN  A1  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     A2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.

Servo servo_motor;  // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

boolean goesForward = false;
int distance = 100;

void setup()
{
  // Set L298N Control Pins as Output
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(10);   // Attachs the servo on pin 9 to servo object.
  servo_motor.write(115);   // Set at 115 degrees.
  delay(2000);              // Wait for 2s.
  distance = readPing();    // Get Ping Distance.
  delay(100);               // Wait for 100ms.
  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 (distanceRight >= distanceLeft)
    {
      turnRight();
      delay(300);
      moveStop();
    }
    else
    {
      turnLeft();
      delay(300);
      moveStop();
    }

  }
  else
  {
    moveForward();
  }

    distance = readPing();
}

int lookRight()     // Look Right Function for Servo Motor
{
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft()      // Look Left Function for Servo Motor
{
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int readPing()      // Read Ping Function for Ultrasonic Sensor.
{
  delay(100);                 // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  int cm = sonar.ping_cm();   //Send ping, get ping distance in centimeters (cm).
  if (cm==0)
  {
    cm=250;
  }
  return cm;
}

void moveStop()       // Move Stop Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward()    // Move Forward Function for Motor Driver.
{
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(RightMotorBackward, LOW);
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
}

void moveBackward()   // Move Backward Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
}

void turnRight()      // Turn Right Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
}

void turnLeft()       // Turn Left Function for Motor Driver.
{
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
}



Applications :

  • Obstacle avoiding robots can be used in almost all mobile robot navigation systems.
  • They can be used for household work like automatic vacuum cleaning.
  • They can also be used in dangerous environments, where human penetration could be fatal.
SUBSCRIBE OUR CHANNEL FOR MORE VIDEOS -  https://www.youtube.com/channel/UCk7q2iANcHsIOk_Lfu93q4g?view_as=subscriber


THANK YOU




Comments

Popular Posts