how to make obstacle avoidance robot using arduino

Obstacle avoidance robot using Arduino, In this project Obstacle avoidance robot, is designed using Arduino Uno R3. Obstacle avoidance robot is work with self-intelligence if there is any hurdle or obstacle in its way. It turns its direction automatically in case of any obstacle in its way. the ultrasonic sensor is used to sense the obstacle. The basic purpose of the ultrasonic sensor is to measure the distance from any obstacle in front of it. This distance measurement concept is used to change the direction of the robot when the robot is at some distance close to handling or obstacle. HC-SR04 Ultrasonic sensor is used in this project.  I recommend you to read arduino tutorials, if you are a beginner in arduino programming.

HC-SR04 ultrasonic sensor for distance measurement

the ultrasonic sensor is distance measurement sensor which used ultrasonic waves to measure sensor. HC-SR04 ultrasonic sensor has ultrasonic waves transmitter and receiver which is used to measured distance with the help of reflection time of ultrasonic waves from a transmitter to back into the receiver. I have posted a separate article on Ultrasonic sensor interfacing with Arduino. I highly recommend you to read the following article first. If you don’t know about ultrasonic HC-SR04 sensor interfacing with Arduino Uno R3. because it is the main part of Obstacle avoidance robot project. Check this article:

Ultrasonic sensor interfacing with Arduino and distance measurement

I hope that after reading above article, you know that how to measure distance using ultrasonic sensor and Arduino Uno R3. Now let’s proceed to next important part of this project that is how to interface two dc motors with Arduino Uno R3. It is very easy to interface Two DC motors with Arduino using L293D dc motor driver. Circuit diagram below shows how to connect dc motor and L293 motor driver with Arduino.

Circuit diagram of obstacle avoidance robot using Arduino

Circuit diagram of obstacle avoidance robot using Arduino is given below:

You should note that ultrasonic sensor has four pins voltage pin, a ground pin, trigger pin and echo pin. L298 is a dc motor driver which is used to rotate motors in a clock wise or anti clock wise direction. It is used to turn robot right, left, reverse and straight. Followings are main components of this obstacle avoidance robot using Arduino Uno R3.obstacle avoidance robot using arduino circuit diagram

Hardware components of  obstacle avoidance  robot

Video

Code of  obstacle avoidance  project

Code for this project is written using Ardino IDE.

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 100
#define MAX_SPEED 80 // 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, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ);

Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

myservo.attach(9); 
 myservo.write(115); 
 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<=30)
 {
 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+MAX_SPEED_OFFSET);
 motor3.setSpeed(speedSet);
 motor4.setSpeed(speedSet+MAX_SPEED_OFFSET);
 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+MAX_SPEED_OFFSET);
 motor3.setSpeed(speedSet);
 motor4.setSpeed(speedSet+MAX_SPEED_OFFSET);
 delay(5);
 }
}

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

 

If you have any issue with  obstacle avoidance  robot project. Feel free to comment on this post.

16 thoughts on “how to make obstacle avoidance robot using arduino”

  1. How it can be so Simple? And you mentioned resistors in hardware components but not in Circuit Diagram ?

  2. Can someone give me the code for my arduino robot ..it has arduino uno R3 , L293D motor driver , HC-SR04 ultrasonic , 2 dc motors , and IR remote with receiver module ?

  3. How can i connect a L293d Motor driver instead of motor driver shield. Where i need to connect the Control signal pins od l293d ic?. Is there any change in the coding.?

Leave a Comment

shares