How to Make an Obstacle Avoiding Robot?
by TahmidZubayer in Circuits > Robots
2894 Views, 16 Favorites, 0 Comments
How to Make an Obstacle Avoiding Robot?
Hello everybody. I am Zubayer Tahmid from Bangladesh. Today, in this tutorial I will try to cover everything you need to learn to make an obstacle avoiding robot using Arduino. I believe that if you read this tutorial from first to last thoroughly, then you will be able to make an obstacle avoiding robot very easily and will learn many things about Arduino robotics. But if you are not familiar with Arduino then you must read my previous tutorial on the basics of Arduino. I am providing you the link: https://www.instructables.com/id/Arduino-Basic-Tutorial/
So, let’s get started.
Step 1:
To make an obstacle avoiding robot, we need to understand a
couple of things. I have made a summarization here:
1) Controlling motors using a motor driver.
2) Using a sonar sensor HC-SR04 with Arduino.
3) Differential drive mechanism.
4) Working mechanism of an obstacle avoiding robot.
5) Power management system.
6) Making a chassis.
7) Programming our robot.
I will try my best to discuss each and every topic to make an obstacle avoiding robot using Arduino.
Step 2:
In this step we will learn how to use a motor driver IC with Arduino. There are many motor driver ICs like L298N, L293D etc. But to me, L293D motor driver IC is the best one to get started. So at first let us see the pin diagram of L293D motor driver IC.
The first pin of the IC is Enable 1. The left sided pins of the IC will not work unless we give a HIGH value in this pin. Remember, in the world of Arduino, generally HIGH value means +5v. And LOW value means GND or ground. Now we have to connect the two poles of our motor in the two pins, namely OUTPUT 1 and OUTPUT 2 respectively. It’s better if we connect the positive pole in OUTPUT 1 and the negative pole in OUTPUT 2. We will also connect another motor in the OUTPUT 3 and OUTPUT 4 in the same way. All the GND pins are to be connected with the ground pin of Arduino.
Then comes the VCC pin. By this pin we give power to the motors. We will connect +5v power in the two VCC pins. We will connect the 4 INPUT pins with Arduino. Here I am connecting the pins with the 13, 12, 11 and 10 no. digital pins of the Arduino respectively. Now let’s play with it.
If we give HIGH value in INPUT 1 and LOW value in INPUT 2, then the motor connected with OUTPUT 1 and OUTPUT 2 will rotate clock-wise. And if we give LOW value in INPUT 1 and HIGH value in INPUT 2 then the motor will rotate counter clock-wise. The same case will also be followed by INPUT 3 and INPUT 4. So, while programming Arduino, the code will look something like this:
//for rotating the left motor clock-wise
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
//for rotating the left motor counter clock-wise
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
Now try yourself to control the right motor. If I say you everything then you will not be able to learn the mechanism properly. Research with it. Play with it!
Step 3:
In this step we will learn the mechanism of HC-SR04 sonar sensor and how to use it with Arduino.
Let us see in the above photo how the sensor looks in the photo.
There are 4 pins in the sensor. VCC, GND, Trig and Echo. We will connect the VCC pin with the +5v power of the Arduino and the GND pin with the ground pin. Now let us understand the mechanism.
The Green part is the Trigger part and the Blue part is the Echo part.
When we give HIGH value in the Trigger pin then the trigger part starts to release Ultrasonic sound wave (Purple line in the figure). The black wall is an obstacle. If there is an obstacle then the ultrasonic wave gets back to the echo pin reflecting on the wall. When the Echo part receive the Ultrasonic wave, then the Echo pin gives a HIGH value.
Now the question is how we will calculate the distance? We know a formula that Distance = Speed x Time. The time period of the release of the Ultrasonic wave by the trigger part and to receive the Ultrasonic wave by the echo part is our required time.
Let’s see an example. Connect the Trig pin in the digital pin no. 9 and the echo pin in digital pin no. 8 of the Arduino. Now let us start to play with it.
We will now write a code to measure the distance.
void setup(){
pinMode(9, OUTPUT);
pinMode(8, INPUT);
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
Step 4:
In this step we will learn about the differential drive mechanism. When you are first making a robot, I would suggest you to use a two-wheeled robotic chassis. I will discuss about the chassis in the next step. But now let us learn about the differential drive.
There are two wheels in our robot. If both the wheels move in forward direction then the robot will also move forward. If the right wheel moves in forward direction and the left wheel remains stop then the robot will move left. Again if the right wheel remains stop and the left wheel moves in forward direction then the robot will move right. By this technique we will control our robot.
Step 5:
In this step I will discuss some topics which are to be kept in mind while designing a chassis.
While designing a chassis you should first imagine the robot in your mind. There should be a place for attaching an Arduino, a breadboard, the motor driver, the sonar sensor and the two motors with wheels. You can see a huge collection of robotic chassis in the google. Actually I do not support to tell how to make a chassis. Because then you will not be able to learn anything. You must explore your creativity. Always remember that robotics is one of the best medium to show your creativity to the entire world. And you must be very fluent in using the google search engine. This will be your best friend in your journey in the world of robotics.
Step 6:
In this step we will discuss about the mechanism of an obstacle avoiding robot.
Let us see the flow chart. At first the robot will see if the left distance is smaller than the right distance. If it is true, then the robot will move forward. And if it is false, then it will move left. Then again if it finds any obstacle within 30 cm, then it will stop. And will again take a reading. And will follow the previous logic. If you see the flow chart, then everything will come clear to you.
Step 7:
In this step I will discuss about the power management system.
The Arduino can be powered by 5v-12v. But I extremely suggest you to use rechargeable batteries. Because the robot will consume the power of the battery very fast. If you use normal batteries, then the robot will hardly run for 4-5 minutes. So it will not be convenient for you.
I am giving you the circuit diagram of the robot. The sonar sensor connection is not shown there. Connect it as I said in Step 3.
Step 8:
In this step we will program our robot.
At the first of the code, we will make the functions for the different movements of the robot.
//for moving backward
void moveBackward(){
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
}
//for moving forward
void moveForward(){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
}
//for moving right
void moveRight(){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
//for moving left
void moveLeft(){
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
}
//for stopping
void moveStop(){
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
Here goes the complete code:
/* Obstacle Avoiding Robot Tutorial by Zubayer Tahmid
* Basic Obstacle Avoiding Code
* Programmed By Zubayer Tahmid
*/
#include //Include NewPing Library
#define TRIGGER_PIN 8 //Trigger pin of Ultrasonic sensor connected to pin 6
#define ECHO_PIN 9 //Echo pin of Ultrasonic sensor connected to pin 7
#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic sensor object
unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back
int distance; //Variable to store the distance calculated from the sensor
int triggerDistance = 30; //The distance we want the robot to look for a new path
int fDistance; //Variable to store the distance in front of the robot
int lDistance; //Variable to store the distance on the left side of the robot
int rDistance; //Variable to store the distance on the right side of the robot
void setup()
{
pinMode(5, OUTPUT); // Declaring pin 5 as output
pinMode(6, OUTPUT); // Declaring pin 6 as output
pinMode(4, OUTPUT); // Declaring pin 4 as output
pinMode(7, OUTPUT); // Declaring pin 7 as output
}
void loop()
{
scan(); //Get the distance retrieved
fDistance = distance; //Set that distance to the front distance
if(fDistance < triggerDistance){ //If there is something closer than 30cm in front of us
moveBackward(); //Move Backward for a second
delay(500);
moveRight(); //Turn Right for half a second
delay(500);
moveStop(); //Stop
scan(); //Take a reading
rDistance = distance; //Store that to the distance on the right side
moveLeft();
delay(500); //Turn left for a second
moveStop(); //Stop
scan(); //Take a reading
lDistance = distance; //Store that to the distance on the left side
if(lDistance < rDistance){ //If the distance on the left is smaller than that of the right
moveRight(); //Move right for a second
delay(500);
moveForward(); //Then move forward
}
else{
moveForward(); //If the left side is larger than the right side move forward
}
}
else{
moveForward(); //If there is nothing infront of the robot move forward
}
}
void scan(){
time = sonar.ping(); //Send out a ping and store the time it took for it to come back in the time variable
distance = time / US_ROUNDTRIP_CM; //Convert that time into a distance
if(distance == 0){ //If no ping was recieved
distance = 100; //Set the distance to max
}
delay(10);
}
void moveBackward(){
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
}
void moveForward(){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
}
void moveRight(){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
void moveLeft(){
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
}
void moveStop(){
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
I have used the newping library to make the code easier. I have given the folder. Download it and extract it the *libraries* folder of the Arduino. Upload the code and your life’s first robot.
Conclusion:
I believe that you have been able to learn a lot of things from this tutorial. If you face any kind of problem while making the robot, just give me a mail or just comment it below the tutorial. I am verily active in these two social networks. I will try to be there for you.
Best of luck in the world of Robotics. I hope that you will work hard for the further development of your robot.
zubayertahmid2218@gmail.com