Friday, May 1, 2020

Obstacle Avoiding Robot using Arduino

There are many types of mobile robot navigation techniques like path planning, self – localization and map interpreting. An Obstacle Avoiding Robot is a type of autonomous mobile robot that avoids collision with unexpected obstacles.
In this project, an Obstacle Avoiding Robot is designed. It is an Arduino based robot that uses Ultrasonic range finder sensors to avoid collisions. 
ARDUINO UNO



SERVOMOTOR
ULTRASONIC SOUND SENSOR


CIRCUIT DIAGRAM





Design of Obstacle Avoiding Robot using Arduino

Arduino is the main processing unit of the robot. Out of the 14 available digital I/O pins, 7 pins are used in this project design.
The ultrasonic sensor has 4 pins: Vcc, Trig, Echo and Gnd. Vcc and Gnd are connected to the +5v and GND pins of the Arduino. Trig (Trigger) is connected to the 9th pin and Echo is connected to 8th pin of the Arduino UNO respectively. 
A Servo Motor is used to rotate the Ultrasonic Sensor to scan for obstacles. It has three pins namely Control, VCC and GND. The Servo Control Pin is connected to pin 11 of Arduino while the VCC and GND are connected to +5V and GND.  
L293D is a 16 pin IC. Pins 1 and 9 are the enable pins. These pins are connected to +5V.  Pins 2 and 7 are control inputs from microcontroller for first motor. They are connected to pins 6 and 7 of Arduino respectively.
Similarly, pins 10 and 15 are control inputs from microcontroller for second motor. They are connected to pins 5 and 4 of Arduino. Pins 4, 5, 12 and 13 of L293D are ground pins and are connected to Gnd.
First motor (consider this as the motor for left wheel) is connected across the pins 3 and 6 of L293D. The second motor, which acts as the right wheel motor, is connected to 11 and 14 pins of L293D.
The 16th pin of L293D is Vcc1. This is connected to +5V. The 8th pins is Vcc2. This is the motor supply voltage. This can be connected anywhere between 4.7V and 36V. In this project, pin 8 if L293D is connected to +5V supply. 
NOTE: The power supply to the Motor Driver i.e. Pins 1 (enable 1), 8 (VCC2), 9 (enable 2) and 16 (VCC1) should be given a seperate power supply.
Motor Driver boards are available with on–board 5V voltage regulator. A similar one is used in the project. 
If the above Circuit Diagram of the Obstacle Avoiding Robot is unclear, the following image might be helpful. 


CODE:
#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);
}

No comments:

Post a Comment