Introduction: Obstacle Avoiding Robot
I have seen many obstacle avoiding robot builds online including here at instructables, so I decided to do an instructable myself and provide as much visual and written detail as possible. Many of the obstacle avoiding robot projects that I have seen utilize a third servo upon which the PING))) sensor is mounted. This way, when the robot reaches its threshold distance to the obstacle in front of it, this third servo pans left and right then the robot turns to the route which has a “clearer” path and goes on. I on the other hand am just going to show you how to do it without the third panning servo. This is a better alternative for those on a tighter budget. Continue onto the next step to see the parts list.
Step 1: Gather the Parts
To build this robot you are going to need the following parts. All of these parts can be gotten separately or in a bundle (you won't need all of the parts in the bundle) at http://www.parallax.com/.
Hardware:
The following hardware you can get anywhere in various prices.
• Arduino Nano (any 5v Arduino of your choosing will work).
• Four AA batteries
• AA battery holder
• Breadboard (a mini breadboard was perfect in size).
• Wire
• PING))) Ultrasonic Distance Sensor
Hardware from Parallax:
The following hardware you can get anywhere in various prices.
Stock # Quantity Product Name
700-00009 1 Tail Wheel Ball
700-00022 1 Boe-Bot Aluminum Chassis
700-00023 1 Cotter Pin-1/16" Diameter
700-00025 1 Rubber Grommet-13/32" Hole Diameter
721-00001 2 Wheel, Plastic, 2.58 Dia, .3 W
721-00002 2 Rubber Band Tire for 721-00001
900-00008 2 Continuous Rotation Servo
Software:
• Arduino IDE (my version is 1.0.5).
Miscellaneous:
• Phillips head screw driver
• Computer
• Screws and nuts
Step 2: Building the Circuit
As you can see, the circuit is very simple. Just make sure that you have the signal wire of each of the servos going to the correct pins. They are not interchangeable in regards to the code that will be running the robot. The sensor that you see there in the schematic is NOT the PING))) sensor. The software that I used to create the schematic just doesn't have the PING))) sensor in its inventory, so I used the next closes one. The connections are the same (red=power, black=ground, yellow=SIG). I believe the circuit is self-explanatory, but if you have any questions, don't hesitate to ask. Continue onto the next step for the programming part.
Step 3: Programming
The basic concept of the code is for the robot to always be monitoring for objects in front of it while moving. This is done by the PING))) sensor where it sends out a 40kHz chirp which is not audible, and retrieves the echo. The program then gets the duration it takes for the pulse to get back and converts it into distance. Once the robot detects an object where the distance forward is greater than the danger threshold - the path is clear so our program tells the robot to move forward. Otherwise the robot will turn and proceed. The code just loops in this fashion.
THE CODE:
/*
> Design an obstacle avoiding robot using an Arduino NANO and Parallax PING))) Ultrasonic Distance Sensor
> Zoran M.
*/
#include //include Servo library
const int RForward = 0;
const int RBackward = 100;
const int LForward = RBackward;
const int LBackward = RForward;
const int pingPin = 7;
const int dangerThresh = 15; //threshold for obstacles (in cm)
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal
void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
}
void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LBackward);
rightMotor.write(RForward);
delay(1000);
}
}
long ping()
{
// Send out PING))) signal pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//Convert duration into distance
return duration / 29 / 2;
}
Continue onto the last step for additional pictures and conclusion.
Step 4: Additional Pictures and Conclusion
Everything went pretty well while putting this project together. The program had no trouble whatsoever; the only thing I had some trouble with was the PING))) sensor. As I declared the threshold to be 15 cm it was picking up distances sometimes further than what I set it to be and sometimes closer than what I set it to be. A suggestion for future improvement would be to have a random method in the program where when the robot approached an object I would have no control whether it would turn left or right and by how many degrees. With this random method, both the the direction and degree of turn would be totally random; making it easier for the robot to get out of corners. Don't hesitate to post up your versions, even if you did it like I did. I would love to see them!
01001000 01100001 01110110 01100101 00100000 01100110 01110101 01101110 00100001

Participated in the
Supercharged Contest

Participated in the
123D Circuits Contest
26 Comments
8 years ago
Can you pls tell me the wiringif iI use DC motors with two pins??? Just tell me the connection pin..
8 years ago
Pls describe the pins of arduino that you are using for servos and ping sensor..Reply fast it is urgent
Reply 8 years ago
Take a look at the drawing. It is very clear and easy to follow.
8 years ago on Introduction
Can I use ardiuno Uno?
Reply 8 years ago on Introduction
Yup, you can use an Arduino Uno.
9 years ago on Step 4
how if i change the PING sensor with sharp GP2D12 IR Ranger sensor, is that work?
9 years ago on Introduction
You answered many of my questions about Arduino and Servos. Thanks.
9 years ago on Introduction
Is a servo necessary or can this be driven with a pair of brushless motors, such as you might find in a Tamiya gearbox?
Reply 9 years ago on Introduction
You can do it with brushless motors as well.
Reply 9 years ago on Introduction
Is it a direct connect? What is the connection to control the motor? In other words, if I have a 2 wire motor where do I connect which wire?
Thanks Zoran!
Reply 9 years ago on Introduction
Here: https://www.instructables.com/id/Arduino-obstacle-avoiding-robot-1/ is an instructable you can follow. Thanks.
9 years ago on Introduction
thank you. But the error messages:
Arduino: 1.5.5-r2 (Windows 7), Board: "Arduino Uno"
sketch_jan28a.ino: In function 'void loop()':
sketch_jan28a:27: error: expected initializer before 'rightMotor'
sketch_jan28a.ino: In function 'void loop()':
sketch_jan28a:31: error: redefinition of 'void loop()'
sketch_jan28a:6: error: 'void loop()' previously defined here
sketch_jan28a:34: error: 'dangerThresh' was not declared in this scope
sketch_jan28a:36: error: 'leftMotor' was not declared in this scope
sketch_jan28a:36: error: 'LForward' was not declared in this scope
sketch_jan28a:37: error: 'rightMotor' was not declared in this scope
sketch_jan28a:37: error: 'RForward' was not declared in this scope
sketch_jan28a:41: error: 'leftMotor' was not declared in this scope
sketch_jan28a:41: error: 'LBackward' was not declared in this scope
sketch_jan28a:42: error: 'rightMotor' was not declared in this scope
sketch_jan28a:42: error: 'RForward' was not declared in this scope
sketch_jan28a.ino: In function 'long int ping()':
sketch_jan28a:50: error: 'pingPin' was not declared in this scope
sketch_jan28a:59: error: 'duration' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
still come up. I have a week left to do this project so it would really help if you could help me quickly. Thank you!
Reply 9 years ago on Introduction
Use this code: https://gist.github.com/anonymous/9b140e31f95be145a08a . When you hit 'verify', what do you get? Any errors, or nothing? By the way, what version of Arduino evironment are you using?
Reply 9 years ago on Introduction
thank you. i am using 1.5.5-r2. But with the new code the error messages:
Arduino: 1.5.5-r2 (Windows 7), Board: "Arduino Uno"
sketch_feb25a.ino: In function 'void setup()':
sketch_feb25a:27: error: redefinition of 'void setup()'
sketch_feb25a:1: error: 'void setup()' previously defined here
sketch_feb25a.ino: In function 'void loop()':
sketch_feb25a:33: error: redefinition of 'void loop()'
sketch_feb25a:6: error: 'void loop()' previously defined here
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
appear with the new code. Please respond as soon as possible. Thank you.
Reply 9 years ago on Introduction
When you go to arduino's website, download this: http://imgur.com/MDy1lfM . Try it out and let me know how it goes.
9 years ago on Introduction
I am in the process of making this robot. I am a student that only has a little robotic experience but my teacher knows quite a bit. The Arduino software does not recognize the word Servo and I don't know what to do. Could someone help me?
Reply 9 years ago on Introduction
Hello, LegitINSTRUCTABLES. How about if you insert <Servo.h> after #include like I have shown here: http://imgur.com/JTTTXGl ?
9 years ago on Introduction
can I use HCdSR04 as my sensor??can't find available PING sensor here...
Reply 9 years ago on Introduction
That will work, although you might have to change some of the code in the program to correspond to the pins of that sensor.
9 years ago on Introduction
The code would work on other arduinos right? I've just started to learn the language, thank you.