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

Comments

author
nahiyan.ahmed.142 (author)2015-03-28

Can you pls tell me the wiringif iI use DC motors with two pins??? Just tell me the connection pin..

author
nahiyan.ahmed.142 (author)2015-03-24

Pls describe the pins of arduino that you are using for servos and ping sensor..Reply fast it is urgent

author

Take a look at the drawing. It is very clear and easy to follow.

author
Special FX (author)2015-02-01

Can I use ardiuno Uno?

author
Zoran1389 (author)Special FX2015-02-01

Yup, you can use an Arduino Uno.

author
KennyV1 (author)2014-11-07

how if i change the PING sensor with sharp GP2D12 IR Ranger sensor, is that work?

author
KeimaHane (author)2014-09-16

You answered many of my questions about Arduino and Servos. Thanks.

author
samern (author)2014-06-16

Is a servo necessary or can this be driven with a pair of brushless motors, such as you might find in a Tamiya gearbox?

author
Zoran1389 (author)samern2014-06-16

You can do it with brushless motors as well.

author
samern (author)Zoran13892014-06-16

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!

author
Zoran1389 (author)samern2014-06-16

Here: https://www.instructables.com/id/Arduino-obstacle-avoiding-robot-1/ is an instructable you can follow. Thanks.

author
LegitINSTRUCTABLES (author)2014-02-20

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!

author

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?

author

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.

author

When you go to arduino's website, download this: http://imgur.com/MDy1lfM . Try it out and let me know how it goes.

author
LegitINSTRUCTABLES (author)2014-02-07

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?

author

Hello, LegitINSTRUCTABLES. How about if you insert <Servo.h> after #include like I have shown here: http://imgur.com/JTTTXGl ?

author
wafhakelspawn (author)2014-02-04

can I use HCdSR04 as my sensor??can't find available PING sensor here...

author
Zoran1389 (author)wafhakelspawn2014-02-07

That will work, although you might have to change some of the code in the program to correspond to the pins of that sensor.

author
yvesyuzo (author)2014-01-20

The code would work on other arduinos right? I've just started to learn the language, thank you.

author
Zoran1389 (author)yvesyuzo2014-01-20

Yes, the code will work with other Arduino's.

author
Zoran1389 (author)2014-01-20

Yes, this project will work with other Arduino's.

author
carlos66ba (author)2014-01-16

Some time ago, my son and I built something with this functionality with a LEGO Mindstorms NXT. One thing you may want to add: scanning right and left (when it sees an obstacle ahead) to decide which way to move (L or R).

author
Zoran1389 (author)carlos66ba2014-01-16

Yup. I am actually working on that right now. This version was intended for someone on a tighter budget. Servo's aren't exactly cheap :).

author
carlos66ba (author)Zoran13892014-01-17

What we did was to (take d = distance detected by sensor in cm)

a) Move forward while d > 30, store d_forward

b) Rotate robot 90 degrees left, store d_left.

c) Rotate robot 180 degrees right, store d_right

d) Find biggest of the three and move in that direction.

Not very sophisticated but the graphical language of LEGO is a pain to make complex programs :) I bet one could do better with arrays and many angles using Arduino or C.

author
Zoran1389 (author)carlos66ba2014-01-17

I am doing something similar for my robot. I should be done within the next few days. Come back and check out the code when I am done

About This Instructable

14,323views

89favorites

License:

Bio: I am an electrical engineering and physics student and I love to tinker with things.
More by Zoran1389:Obstacle Avoiding Robot V2Obstacle Avoiding Robot74LS Series Digital Logic Tester
Add instructable to: