Instructables
Picture of How To Make an Obstacle Avoiding Arduino Robot
Hello all! In this Instructable I'll be showing you how to make a robot similar to the "Mobile Arduino Experimental Platform" (MAEP) that I made. It is equipped with two motors that can steer the robot and the ability to see obstacles in front of it with a PING))) ultrasonic sensor. 

With the attached breadboard, you can do more electronics experiments, fool around with different sensors, etc. This project can teach you about electronics, programming, and robotics. It is also a fun toy to entertain younger siblings and pets (just be sure they don't break it!).

Here is a video of what MAEP will do when it is done:
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts

Picture of Parts
Here's everything you need to make MAEP. I tried to provide links to where you can buy these parts from YourDuino.com, which is the official shop of letsmakerobots.com. EDIT: YourDuino and LMR are now separated and YourDuino is no longer giving money to LMR, however they still have pretty good prices so feel free to shop there.

- Arduino compatible microcontroller. I used an Arduino Uno, so I recommend one if you wish to follow closely along with this tutorial. There are also some Arduino based controllers that are designed specifically for robotics that may be helpful, but you’ll have to find your own method of mounting those. You can buy a copy of the Uno from YourDuino which I believe is completely compatible: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=5

-  Breadboard. Make sure it’s not too big, or else it won’t fit. However, we want it to be as big as possible so we have more room for electronics. This is good: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=168

- A standard old 9 volt alkaline battery (which you can probably find at home), as well as a barrel jack connector to hook it up to your Uno easily (not required, but again, easier):  http://arduino-direct.com/sunshop/index.php?l=product_detail&p=119

- A power supply for the motors. I don’t believe the power supply I chose is the cheapest or best option, so I’m not going to recommend it to you. I used a 4.8 volt rechargeable NiCad battery, but it’s probably easier to just use a 4 AA battery holder (no need to buy a charger that way)

- A bunch of Boe-Bot hardware for the chassis. Unfortunately, you can’t get this from YourDuino, so look at Parallax, the manufacturer’s website. You can choose either to buy this: http://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/304/Default.aspx?txtSearch=boe+bot+chassis which comes with tons of extra components you don’t need (although may be useful someday), or you can buy all the components in the following list from Parallax and no, I’m not gonna provide a link for each one:



Stock # Quantity              Product Name

700-00002            8             Panhead screw, 4/40, 3/8

700-00003            8              4/40 x 3/8" Nut

700-00009            1             Tail Wheel Ball

700-00015            2              #4 Nylon Washer

700-00022            1              Boe-Bot Aluminum Chassis

700-00023            1              Cotter Pin-1/16" Diameter

700-00025            1              Rubber Grommet-13/32" Hole Diameter

700-00028            4              Panhead screw, 4/40, 1/4

700-00060            2              Standoff, threaded aluminum, round 4-40

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



- Wire to hook things up will be necessary. Some male-to-male jumpers will do the trick: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=94

- Finally, the PING))) sensor and mounting bracket, so that your robot doesn't kill itself: http://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/563/Default.aspx?txtSearch=ping+sensor+mount


As for tools, all you need is a phillips head screwdriver, a computer, and one of those standard USB printer cables. The Arduino Uno I provided a link for has a cable included. 

Now lets get building!
1-40 of 118Next »
Jelmer83 days ago

Hello,

Is there someone who has the same code but with an 4 pin HC-SR04 Sensor?

Thanks

Jelmer

mintesinot10 months ago

First thank you all of you,now i am doing fire fighting robot project however i am facing some problem.which is we have to use the photo diode and IR LED rather than ultrasonic sensor for obstacle and distance sensor because of the availability.so please help me if is there a change in any one of the program and the circuit or if you have any idea which you think is you can tell me what i have to do

Is the photodiode and IRLed in one sensor? Like a Sharp IR Sensor (http://www.acroname.com/articles/sharp.html) ?

If so then it is very comparable to the Ultrasonic sensor, but the target area is very narrow and you may want a sweeping motion in your code that takes multiple readings then move the robot in the longest duration direction. There are many tutorials on the specifics of that sensor. Here are some links:

http://www.instructables.com/id/Arduino-based-robo...

http://communityofrobots.com/tutorial/kawal/how-ma...

http://www.societyofrobots.com/sensors_sharpirrang...

Hope that helps.

aarias57 months ago

i made it but it kills the battery too fast can someone tell me what can i do to make the battery last longer

Joxman2k aarias52 months ago

You will need batteries that have a higher mAh (milli-Amp-hour) rating. Basically same voltage but bigger charge. OR You could add more batteries in parallel. Basically add two or more of the same batteries that you have by connecting positive to positive and negative to negative. This will double the charge but keep the same voltage. Look online for your battery type.

awusum_harsh8 months ago

hey... i am new here..... can anyone tell me about the servo under the ultrasonic sensor.... plzzzz fast...

It seems to be an ordinary un-modified servo. It is used in conjunction with the ultrasonic servo to determine distance in three different angles (left, right and center) Your code will move the robot towards the angle that has the longest PING time, which has more open area.

I believe that the author did not include this third servo in the materials list. Six months isn't fast but I hope it helps.

:P

reply fast guys ....... its urgent !!!!!!!!!

itchylibido10 months ago

bot with ping added :)

Thanks again

rymi itchylibido2 months ago

hello , its a very nice robot ,
we have a project and we are new to almost every thing :)
but am gathering information any way
our robot going to hold objects , so i want it to not turn around when it reach it , instead , to stop , and hold it using magnatic or sth ..
can you please help me with the code
and how to stop when it reach a certain distance from the object , can we do that ? thanks a lot

AVISHKAR_2 months ago

can i replace servo using dc geared motors??

sangelek1 year ago
Hi,
Could you help me with the problem I'm having with my robot PLEASE.
Basically I've used the code you have posted, but it didnt work so I made bit change but still it didnt work, the problem is my the servo with ultrasonic sensor on it keep on spinning left and right constantly even tho there is no obstacle in front.
And also the robot goes few steps forward and again spins the servo with the ultrasonic sensor.
By the way i'm using ultrasonic sensor with 4 pins (HC-SR04)
I would really appreciate, if u could correct it
Cheers




/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/
#include //include Servo library

const int RForward = 0;
const int RBackward = 180;
const int LForward = 0;
const int LBackward = 180;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int trigpin = 7;
const int echopin = 3;
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}

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(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping(); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping(); //scan to the left
delay(500);
panMotor.write(90); //return to center
delay(100);
compareDistance();
}
}

void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}

long ping()
{
// Send out PING))) signal pulse
pinMode(trigpin, OUTPUT);
pinMode(echopin,INPUT);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echopin, HIGH);

//Convert duration into distance
return duration / 29 / 2;
}
Joxman2k sangelek7 months ago

Are you using a continuous rotation servo for the PING sensor? You need a normal servo to get proper degrees for that servo. Continuous rotation servos merely spin cockwise, counter-clockwise, and stop using the values 180, 0, and 90 respectively.

The code looks fine and wouldn't cause the symptoms you describe.

:P

generalgeek314 (author)  sangelek1 year ago
Hi, this seems like it should work. Google how to use the Arduino serial port if you don't know how, then print out the duration variable in a loop. Test different distances, and see if the sensor is returning the right distance from the obstacle.
Hello all
I was thinking about making this awesome robot. But after reading the whole article and the code i got confused with the operation of the two servos that drive the wheels.
As far as i know, a contineous rotating servo has lost its control over the amount of degree to rotate. Then how can the wheel servo rotate 90º and 180º?

With continuous rotation servos it acts more like a speed control. The servo commands execute until the potentiometer in the servo reaches a certain resistance that equals that degree then stops. The servo will never reach that resistance because the potentiometer is essentially disabled. So 90 degrees is center, 180 degrees is turn right (I think) and 0 is turn left.

I think the solutions for degrees of movement of the robot involve time and/or light sensors like photoresistors, on the the wheels to calculate degrees.

Hi, im trying to do this exact project and in addition to this i would like to add sound source tracking. could you anyone help?

Experiment with two microphones on Arduino analog pins and compare the two inputs similar to photo-resistors for light.

ramjetrth10 months ago

I notice you don't use a motor shield or diode protection on the breadboard. Isn't a safeguard needed against an inductance induced power spike when the servos are stopped? Also wouldn't the load of the two servos be very close to the maximum the Arduino can handle without being damaged?

le servo-moteur coute vers 10 euro en tant que le moteur simple coute 3 euro , tu sais pourquoi ?
parce que le servo-moteur est déjà équipé d'un moteur shield et protection complete dedans c'est pour cela il y a aucun risque a se montage :)

Reykaturse9 months ago

HI! :) mine is working pretty good thanks to you. and in fact i made some changes and it was perfectly working to..

Is it possible to make this with the Dagu Rover 5 Tracked Robot Chassis?

Jonny1249 months ago
Hi love the idea and clear instuctions but just to be clear could you send a revised vertion of the code to me?
Thanks so much
John
itchylibido made it!10 months ago

Hi i was able to take the code you and sangelek so kindly provided and upgrade my Parallax arduino shield bot from IR Sensors to PING Sensor with very minor adjustments to the code..the shield i have has connections for 4 servo,s ,,,im only using 3 servos,, it has 5 aa battery's under the hood,, my ping sensor has 4 connections.. thanks to you guys i was able to figure it out.. here is a quick video of my bot with IR sensors. ---- ---- . i will put up a new video today with my ping sensors on it.. and ill be sure to mention you guys .. thank you generalgeek314 & sangelek

cheers Robbie

/*MAEP 2.0 Navigation

by Noah Moroze, aka GeneralGeek

This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/

PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping

*/

#include <Servo.h>//include Servo library

const int RForward = 0;

const int RBackward = 180;

const int LForward = 180;

const int LBackward = 0;

const int RNeutral = 90;

const int LNeutral = 90; //constants for motor speed

const int trigpin = 7;

const int echopin = 3;

const int dangerThresh = 10; //threshold for obstacles (in cm)

int leftDistance, rightDistance; //distances on either side

Servo panMotor;

Servo leftMotor;

Servo rightMotor; //declare motors

long duration; //time it takes to recieve PING))) signal

void setup()

{

rightMotor.attach(12);

leftMotor.attach(13);

panMotor.attach(11); //attach motors to proper pins

panMotor.write(90); //set PING))) pan to center

}

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(LNeutral);

rightMotor.write(RNeutral);

panMotor.write(0);

delay(500);

rightDistance = ping(); //scan to the right

delay(500);

panMotor.write(180);

delay(700);

leftDistance = ping(); //scan to the left

delay(500);

panMotor.write(90); //return to center

delay(100);

compareDistance();

}

}

void compareDistance()

{

if (leftDistance>rightDistance) //if left is less obstructed

{

leftMotor.write(LBackward);

rightMotor.write(RForward); //turn left

delay(500);

}

else if (rightDistance>leftDistance) //if right is less obstructed

{

leftMotor.write(LForward);

rightMotor.write(RBackward); //turn right

delay(500);

}

else //if they are equally obstructed

{

leftMotor.write(LForward);

rightMotor.write(RBackward); //turn 180 degrees

delay(1000);

}

}

long ping()

{

// Send out PING))) signal pulse

pinMode(trigpin, OUTPUT);

pinMode(echopin,INPUT);

digitalWrite(trigpin, LOW);

delayMicroseconds(2);

digitalWrite(trigpin, HIGH);

delayMicroseconds(10);

digitalWrite(trigpin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echopin, HIGH);

//Convert duration into distance

return duration / 29 / 2;

}

20140206_124119.jpg20140206_124138.jpg
dilanka12311 months ago
Hi I'm new to here. I already make that without any problems. Thanks all . So now i need to remove this servos and put motors here. So how can i do that?
Anoop Surya11 months ago
can i use Roboduino ATMega8 (Based on Arduino Diecimila) instead of arduino uno......?
or can u suggest me a cheaper one
A4Aninketh11 months ago
can i use a readymade chassis because i cant get the aluminium chassis you used.
How can we connect 3 connections to a single 5v.
How to connect the left and right servo and the servo panning servo.
skumar1311 year ago
hi,,i made this using two IR sensors in robotic controller M1,will u plz send me the code ,,,i made it using flowcode AVR ,but got some errors and cant find how,,,so plz help
Thank you for the first code. I figured out how to open the center servo code And built the robot. Instead of using an arduino UNO I used an arduino nano and put it in an altoids tin. You can check it out at YouTube here: http://m.youtube.com/watch?v=KbMGGeGTqso
can you also put the center servo code
//Center Servos
#include

Servo servo;

void setup()
{
servo.attach(11);
}

void loop()
{
servo.write(90);
}

westyn1231 year ago
What would I need to change in order to use a 4-pin sensor? I have the HC-SR04. Thanks!!
generalgeek314 (author)  westyn1231 year ago
I think some of the comments below have addressed this concern already. Good luck with your robot!
dppanos1 year ago
thanks
generalgeek314 (author)  dppanos1 year ago
You're welcome!
hey can you put the code in the comments because I'm having a hard time opening the code attachment
Sorry it took me so long to reply

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include <Servo.h> //include Servo library

const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor; 
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

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(LNeutral);
    rightMotor.write(RNeutral);
    panMotor.write(0);
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
 
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward); //turn left
    delay(500);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    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;
}
Beachley1 year ago
I just got my robot up and running, it works great! I did have to make one adjustment, the 6v "AA" battery holder wasn't supplying enough power to the motors so I used a 9v battery instead.

Thanks for the great instructable
1-40 of 118Next »