Loading...

Obstacle avoiding robot using arduino and l293d | Arduino Geek


Hello everyone! Welcome to Arduino Geek. Today we will discuss about an Arduino based Obstacle avoiding robot. Basically an Obstacle avoiding robot consist of some set of sensors like ultrasonic sensor (distance measurement sensor), which measure the distance of an object from a particular distance and then it returns the distance as an input to the microcontroller arduino. Arduino will further process the input for executing some set of instruction. Before making the robot you have to know about some basic important things. Obstacle avoiding robot is a basic example of automation projects, where we are building a car by the help of arduino uno and ultrasonic sensor. The robot will automatically control itself by avoiding the obstacles coming in front of it. The robot will use the data of ultrasonic sensor and find its own path by avoiding the obstacles.

In today’s session we will cover all the important things, which we have to keep in mind while making an Obstacle avoiding robot. Also we will show you all the necessary steps that we have to follow for making an Obstacle avoiding robot.

Components –

  1.  Arduino Uno 
  2.  L293d Motor Driver 
  3.  Servo SG90 
  4.  Ultrasonic Sensor 
  5.  DC Geared Motor
  6. Wheels
  7.  Jumper Wire 
  8.  Lipo Battery

Frame –

First of all take the DC geared motor and soldier the wires in the both terminals of the motor. Then the next thing is to build the chassis for holding the DC geared motors. For this purpose take a wooden board of suitable size. Then fix all the Dc geared motors at each corner of the wooden board. While fixing the dc motors you have to check the alignment of the motors. The alignment should be perfect for a better result. Otherwise your robot will face problem at performing time.
Once you fixed all the motors with proper alignment, you should connect the wheels with the motors. But, before connecting the wheels you should use some couplings for optimal performance of your robot. Finally connect the wheels to the DC geared motors and use the screw to fix them properly. Now the basic structure of the robot is ready and now you can move forward to the circuit connection. 
 

Circuit Connection –

Circuit connection is the most important thing in automation projects. Because a simple fault in circuit may cause a big error in the performance of the robot. So it is advised to keep focused while making the circuit. In the circuit we use the arduino uno as the main processing unit of the robot. To avoid the problems of wiring we use L293d motor driver shield. This shield is fully designed to put up on arduino uno. It has also some other benefits. It can run four dc motors at a time. So connect the dc motors with the motor driver one by one. If you see the motors are running in the opposite direction at the run time, then you should interchange the connection of the motors. Also it has an inbuilt servo pins for connecting the servo. As a result we don’t need any other circuit to connect our servo. Now the next thing is to connect the servo in the L293d motor driver shield.

After connecting the servo, you need to take one plastic piece. You have to attach the plastic piece in the rotating arm of the servo. This plastic piece will provide the support to the ultrasonic sensor. Now take the ultrasonic sensor and fix it in the top of the servo. Connect the pins of the ultrasonic sensor with the pins of arduino.

Connect the vcc and ground pin with the +5 volt and ground pin of arduino respectively. Then connect the trigger pin with the A0 pin of arduino. Also connect the echo pin with the A1 pin of arduino. Now the circuit connection is finished. For better understanding of the circuit we provide the diagram of the circuit below – 

 

Arduino Code –

Now come to the coding part of the robot, which is the most important thing of any arduino project. You have to write the code according to your robot configuration. However we will provide a sample code for better understand. Now to do the coding, you have to visit the Arduino IDE software. Then you have to write down the code without any error. Then you have to compile the code to check, whether any error is present or not. But before compilation you have to save the code. Once the code is compiled, you have to open the tools section. There you have to select the board type which is Arduino uno. Also you have to choose the port from the tools section. Once you have done all this things, click on upload button to upload the code. The sample code for obstacle avoiding robot is given below – 

 //OBSTACLE AVOIDING CAR//
#include <AFMotor.h>  
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A5
#define ECHO_PIN A4
#define MAX_DISTANCE 200 
#define MAX_SPEED 150 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;  
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
  myservo.attach(10);  
  myservo.write(90); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}
void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 if(distance<=100)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}
int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}
int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}
int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}
void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  }  
void moveForward() {
 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
   }
  }
}
void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
  }

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);     
  delay(500);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);      
}
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);  
  motor3.run(FORWARD);
  motor4.run(FORWARD);   
  delay(500);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}  

Now we come to the end of this project. The last and final thing that you have to do is battery connection. For this we used a 7.4 volt Lipo battery. But you can also use a 12 volt Lipo battery. It totally depends upon you and it does not affect the performance of the robot. After connecting the battery switch on the circuit. Now your robot might be start working. You can place your robot on a smooth surface and observe the performance of robot. You can put some additional obstacles to check the efficiency of the robot. However if it is not working properly, you should recheck your circuit and the code. This may solve your problems. And this is all about the obstacle avoiding robot.

 
close