Obstacle Avoiding Robot using Arduino, Servo Motors and Ultrasonic Sensor

Published  February 25, 2022   0
Obstacle Avoiding Robot

An Obstacle Avoidance Robot is an intelligent robot, which can automatically sense and overcome obstacles on its path. It contains a Arduino to process the data, and Ultrasonic sensors to detect the obstacles on its path. Obstacle avoidance is one of the most important aspects of mobile robotics. Without it, robot movement would be very restrictive and fragile. This project also presents a dynamic steering algorithm that ensures that the robot doesn't have to stop in front of an obstacle which allows the robot to navigate smoothly in an unknown environment, avoiding collisions. Almost all navigation robot demands some sort of obstacle detection, hence obstacle avoidance strategy is of most importance. Obstacle Avoidance Robot has a vast field of application. They can be used as services robots, for the purpose of household work and so many other indoor applications. Equally, they have great importance in scientific exploration and emergency rescue, there may be places that are dangerous for humans or even impossible for humans to reach directly, then we should use robots to help us. In those challenging environments, the robots need to gather information about their surroundings to avoid obstacles. Nowadays, even in ordinary environments, people require that robots to detect and avoid obstacles. For example, an industrial robot in a factory is expected to avoid workers so that it won’t hurt them. In conclusion, obstacle avoidance is widely researched and applied in the world, and it is probable that most robots in the future should have an obstacle avoidance function. Thanks for reading my idea.

Component Required for Obstacle Avoiding Robot

Project Used Hardware

  • Arduino UNO,
  • motor driver,
  • ultrasonic sensor,
  • gear motor,
  • servo motor,
  • 9v battery,
  • wheels,
  • jumper wires.

Project Used Software

  • Arduino IDE

Project Hardware Software Selection

Arduino Uno - It is a microcontroller that gives commands to the robot.

Motor driver - It helps to control the motor attached to the robot, it is connected with Arduino.

Ultrasonic sensor - It senses any obstacle in front of the robot.

Servo motor - It turns left and right to the ultrasonic sensor or we can say that it helps the robot to sense obstacles in the left and right direction.

Gear motor - It helps the robot to move in all directions (forward, backward, left, right).

Wheels - It is connected to a gear motor.

9v battery - It gives power to the robot.

Jumper wire - It connects all hardware with each other to give signal or current.

Arduino IDE - It is software that helps to program the microcontroller (Arduino Uno).

Circuit Diagram

 

Obstacle Avoiding Robot Circuit Diagram

Arduino UNO, motor driver, ultrasonic sensor, gear motor, servo motor, 9v battery, wheels, jumper wires all these hardware are connected with each other.

Also check other obstacle avoiding robots project:

Code
#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins

const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name

void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  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 (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward();
  }
    distance = readPing();
}

int lookRight(){ 
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}


int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}


int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}


void moveStop(){
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}


void moveForward(){
  if(!goesForward){
    goesForward=true;

    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}


void moveBackward(){
  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
}


void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}


void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

}

Video

Have any question realated to this Article?

Ask Our Community Members