ArduinoTutorials

Obstacle Avoiding Robot Using Arduino

Obstacle Avoiding Robot

In the previous posts we learnt how to interface and use an Ultrasonic sensor using Arduino Nano and I introduced you to the Nano Robotic controller board. If you haven’t checked it out yet, I recommend you do.

  1. Nano Robot Controller Board
  2. HY-SRF05 Ultrasonic Sensor and Arduino
  3. Everything You Need To Know About Motor Drivers

In this article we will see how to combine our previous knowledge and make an Obstacle Avoiding Robot Using Arduino. So without any further delay let’s get right into it.

What is an Obstacle Avoiding Robot?

As the name suggests, an obstacle avoiding robot is an autonomous robot that uses a sensor to detect obstacles in the path and takes some actions to avoid it. Sensors like IR proximity, LIDAR, camera or ultrasonic can be used for sensing obstacles in the path of robot.

In this article we will be using an ultrasonic sensor to detect the obstacles in path and avoid them. I have previously explained how an ultrasonic sensor works. In short, there is a transmitter and a receiver in the sensor. The transmitter emits ultrasound waves which reflect back from a surface and the receiver senses it.

how ultrasonic sensor works

What We Need?

Here is a list of all the components that we will need to build this robot. along with best buy links.

Disclaimer: This blog contains Amazon Affiliate links. Buying product from those links helps us run this blog without any charges on you.

Note: I have used Keyestudio’s KeyBot robot chassis.

Robot Assembly.

This is what the robot I build looks like:

Ultrasonic obstacle avoiding robot using arduino nano and nano robot controller board

Here I used a foam board to place the robot controller on top. Beneath that I have placed the battery holder that holds four 3.7V Li-ion cells. I have attached a 9G servo and placed the Ultrasonic sensor on top.
I have used zip ties to hold everything in place.

If you want to build your own chassis from scratch, check out this ebook:
Mini WiFi Robot ebook.

Connections.

Now before making the connection, you must first understand the basics of Nano Robot Board. I recommend you to read this article which will give you a better understanding of how the board works.

bpi nano robot controller board

The connections are as follows:

  • Trig Pin of sensor attaches to A0 of Robot controller.
  • Echo Pin of sensor attaches to A1.
  • The VCC and GND pins are connected to +5V and GND respectively.
  • Finally the Servo is connected to pin D6.

Check out the following article to know more about using Motor controller and Servo.

Coding.

First download and open Arduino IDE. Next open library manager and type “NewPing” in search box. Install the NewPing library by Tim Eckel.

newping library

Copy and paste the following code in Arduino IDE.

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

//Motor control Pins
#define IN1 2
#define IN2 4
#define IN3 7
#define IN4 8

int16_t ENA = 3;
int16_t ENB = 5;

//Ultrasonic Sensor pins
#define trig A0
#define echo A1

#define max_distance 200
boolean Stop = false;
int16_t distance = 100;

NewPing sonar(trig, echo, max_distance);

Servo myServo;


void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

  myServo.attach(6);
  myServo.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  
}

void loop()
{
  int16_t distanceRight = 0;
  int16_t distanceLeft = 0;
  delay(50);

  if(distance <= 20)
  {
    hold();
    delay(100);
    backward();
    delay(500);
    hold();
    delay(100);
    distanceRight = checkRight();
    delay(200);
    distanceLeft = checkLeft();
    delay(200);
    
    if (distance >= distanceLeft)
    {
      left();
      delay(1000);
      hold();
    }
    else if(distance >= distanceRight)
    {
      right();
      delay(1000);
      hold();
    }
  }
  else
  {
    forward(); 
  }
    distance = readPing();
}

int checkRight()
{  
  myServo.write(60);
  delay(500);
  int distance = readPing();
  delay(100);
  myServo.write(120);
  return distance;
}

int checkLeft()
{
  myServo.write(179);
  delay(500);
  int distance = readPing();
  delay(100);
  myServo.write(120);
  return distance;
  delay(100);
}

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

void forward()
{
  analogWrite(ENA, 120);
  analogWrite(ENB, 120);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void backward()
{
  analogWrite(ENA, 120);
  analogWrite(ENB, 120);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void left()
{
  analogWrite(ENA, 80);
  analogWrite(ENB, 80);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void right()
{
  analogWrite(ENA, 80);
  analogWrite(ENB, 80);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void hold()
{
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Connect your arduino to the PC, select the correct arduino board (Arduino Nano) from tools. Select the port and hit upload. After the code is uploaded, disconnect the USB and power the nano controller board using Battery pack.

Let it loose on the floor and see how the robot avoids obstacles.

Conclusion.

The purpose of this article was to show you how to build a simple robot that can avoid obstacles. You can refer this article to implement an ultrasonic sensor into your robot to give it a sense of detecting objects in front of it.

I hope this tutorial was informative and you learnt something useful.
If you like my articles, please consider subscribing to get notified when I post an article.

If you are into robotics and want to build one from scratch, here is an amazing ebook that will help you design, build and program your own WiFi controlled robot.
Mini WiFi Robot

Leave a Reply

Your email address will not be published. Required fields are marked *