How to Make a DIY Arduino Obstacle Avoiding Robot at Home

Hello Guys, In this Blog, you will make an obstacle-avoiding robot. This Instructable involves building a robot with an ultrasonic sensor that can detect nearby objects and change their direction to avoid these objects. The ultrasonic sensor will be attached to a servo motor which is constantly scanning left and right looking for objects in its way.

So, without further ado, Let’s get started!

Step 1: What You Need in This Project:

Here’s the Parts List:

1) Arduino Uno

2) Motor Driver Shield

3) Gear Motor, Frame and wheels set

4) Servo Motor

5) Ultrasonic Sensor

6) Li-ion Battery (2x)

7) Battery Holder

8) Male and Female Jumper wire

9) Soldering Iron

10) Charger

Working:

Before going to working on the project, it is important to understand how the ultrasonic sensor works. The basic principle behind the working of the ultrasonic sensor is as follows:

Using an external trigger signal, the Trig pin on the 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 calculates 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.

Step 3: Programming Arduino UNO

#include <AFMotor.h>

#include <NewPing.h>

#include <Servo.h>

# define TRIG_PIN A1

# define ECHO_PIN A0

# define MAX_DISTANCE 200

# define MAX_SPEED 255 // sets speed of DC  motors
# define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

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(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 <= 15) {
    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() {

  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void moveForward() {

  if (!goesForward) {
    goesForward = true;

    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
    {

      motor3.setSpeed(speedSet);
      motor4.setSpeed(speedSet);
      delay(5);
    }
  }
}

void moveBackward() {
  goesForward = false;

  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
  {

    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}

void turnRight() {

  motor3.run(FORWARD);
  motor4.run(BACKWARD);
  delay(500);

  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnLeft() {

  motor3.run(BACKWARD);
  motor4.run(FORWARD);
  delay(500);

  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

1) Download and Install the Arduino Desktop IDE

2) Download and paste the NewPing library (Ultrasonic sensor function library) file to the Arduino libraries folder.

  1. Download the NewPing.rar below
  2. Extract it to the path – C:\Arduino\libraries

3) Upload the code to the Arduino board via a USB cable

Download Code: https://github.com/alokm014/Obstacle-Avoiding-Robo…

Step 4: Great!

Now your robot is ready to avoid any obstacle…

I would be happy to answer any questions you have

Emailme: alokm014@gmail.com

Websitehttp://robopathshala.in/

Subscribe my YouTube Channel: https://www.youtube.com/channel/UCNOHaiZpf-HhyazeY…

Instagramhttps://www.instagram.com/alokm014/

Facebookhttps://www.facebook.com/robopathshala/

ThankYou 🙂