Introduction: How to Make an Obstacle Avoiding Arduino Robot

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:

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, which is the official shop of 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:

-  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:

- 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):

- 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: 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:

- Finally, the PING))) sensor and mounting bracket, so that your robot doesn't kill itself:

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!

Step 2: Centering Servos

Picture of Centering Servos

First, we need to calibrate the servos so that our robot stops when we want it too. 

Actually, before you do that, you need to set up the Arduino with your computer! Go do this quick little tutorial here:, and then come back (I'll be waiting).

Finished? Good! You should now be able to get your Arduino blinking an LED. Now, you're going to get it to center your servo motors. Download the attached Arduino file (.ino) and open it with the Arduino IDE. Next hook up your servo motors like in the picture. The black wire should be connected to ground on your Arduino (labeled "GND"),  the red one to 5 volts (labeled "5V"), and the white one to pin 11 (labeled "~11"). Make the connections from the Arduino pins to the servo with some male-to-male jumpers. 

Now, upload the program to your Arduino board. Once it runs, one of two things will happen:
1) Your motor won't spin: You're done! Now go back and repeat for the other motor.
2) Your motor will spin. Adjust the potentiometer (see picture 2) with a phillips head screwdriver to stop it from spinning. Slowly turn the screwdriver in either direction until you find the setting that makes it stop. Remember to be gentle and turn slowly... otherwise you'll probably overshoot and have to turn the screwdriver the other way. 

Step 3: Attach the Arduino to the Chassis

Picture of Attach the Arduino to the Chassis

To attach the brains of the robot, you're gonna need the four panhead screws, spacers, and nylon washers. Make a sandwich like in the picture, with this order from top to bottom: screw head, washer, Arduino board, standoff. Attach the standoffs on the two mounting holes on the right side of the Arduino (the side opposite the USB connector and barrel jack). 

Once you have the standoffs firmly attached to your Arduino, line up the bottom one with the bottom of the backmost mounting strip (um... just look at the picture). Attach that into place, then line up the front standoff with the next mounting strip, and screw that in place. Yippee, you're done! 

Note: There's a zip-tie in the picture because I did step 4 before this one... then realized that it's easier to do this step first.

Step 4: Attach the Motor Battery

Picture of Attach the Motor Battery

I gave you a lot of freedom in choosing a battery to power the motors (just make sure it's from 4.8-6 volts), so I don't have much to say here. There are various ways you can attach it/them, I'll name some:

- If you're using a 4 AA battery pack, use the screw holes on the chassis
- I used a zip-tie through some of the mounting slots to attach the NiCad battery I used
- Double sided tape
- Velcro
- Used chewing gum

Also, insert the little black rubber grommet into the hole in the center of the chassis and route the battery wire through that.

Step 5: Motors!!!!!

Picture of Motors!!!!!

Now it's time to make put the M in MAEP (M=Mobile, remember?). Your servos should come with black "servo horns" attached to them already. Unscrew them and pop them off. You don't need them anymore, but be sure to save the screws! Next, put a rubber band "tire" on each wheel. This is really, really difficult, but keep trying. 

Using the eight 3/8 inch panhead screws and nuts, attach the two continuous rotation servo motors to the chassis. Insert them into the cut-outs from the inside of the chassis, and then secure them firmly in place with the motor shaft towards the back of the robot. Use the pictures as a reference.

Once you have that done, thread the wires from the motors up through the hole in the middle of the chassis. Pop a wheel onto each motor shaft, and using the screw that attached the servo horn, attach the wheels to the motors. 

Step 6: Tail Wheel

Picture of Tail Wheel

As of now, your robot is probably awkwardly leaning backwards... that's why it is time to insert the tail wheel! Take the little black ball, and line the holes in its side up with the holes on the back of the robot (see pictures). Then, insert the cotter pin through all these holes, and fold up the two ends so that it can't come out. Now you're robot should stand level, and have a low friction caster for turning. 

Step 7: De-blinding the Robot

Picture of De-blinding the Robot

Okay, here's something else I'm not going to walk you through. Take your Parallax PING))) (yes, it is spelled with those three parentheses) sensor and mounting bracket kit and build it according to the instructions. The only thing you don't need to do is attach the included cable (well, you can, but I'm doing something different and more efficient). Attach the whole shabaam onto the robot like they tell you too, and you're done with this step. 

Note: In the pictures you'll see something attached to the bottom of the motor that my PING))) sensor is mounted on. Ignore it, it's from another project I did with this robot.

Step 8: Breadboard

Picture of Breadboard

Now, to once again use the letter analogy... time to put the E in MAEP! With a solderless breadboard, you can quickly make and de-make circuits to try out new things and not have to break out the soldering iron each time. Most breadboards of this size come with an adhesive backing, in which case you can just stick it on. I used a rubber band, which you can feel free to do. 


Picture of MORE POWER!

Okay, time to attach the battery that powers the logic for the electronics. Take some masking tape, or whatever you want to use to attach it. Loop the tape, and attach the battery. Take the 9 volt barrel jack adapter thingy and put it on the battery. You're done. *clicks Staples button* "that was easy!". 

Step 10: Wiring and Completion!

Picture of Wiring and Completion!

Now, time to wire everything up, download the program, and test her out! 

I'll tell you how everything is connected one component at a time. Refer to the pictures to get a better idea of how it should look (if it's a rats nest of wires... you're on the right track). 

Motor battery: Hook up positive and negative to the positive and negative power rails on the far side of the breadboard (see picture 2).
Grounds: Take a jumper wire and attach the grounds of both power rails together (see picture 3). IMPORTANT, DON'T FORGET
5 volt power supply: Attach a jumper from 5V on the Arduino to the power rail on the breaboard closest to the Arduino.
Servo motors: Attach the left motor with the red and black wires going to the 5 volt power rail and ground respectively. Then, attach the white wire of the left servo to pin 10, the right one to pin 11, and the PING))) sensor panning servo to pin 6.
PING))) sensor: Either use the included cable and attach male-to-male jumpers to that, or use separate male-to-female jumpers to make the following connections: connect the pin on the PING))) that says 5V to the 5 volt rail... connect the pin that is labelled GND to either ground rail... and finally connect the one labelled "SIG" to pin 7 on the Arduino. 

I think that's everything... well, if it doesn't work you can yell at me in the comments because I probably forgot something. The wiring will look like a total rats nest, if you want it to look a little neater you can use male to male header pins like the setup I used in the video.
UPDATE: I have made a mistake, thanks to faroos7 for pointing it out! Also, in the code you'll notice a variable named irPin, which is set to 0. That's part of another project, and you can feel free to delete that variable. 

To get her going, download the attached Arduino program, run it, and it should have behavior similar to that in the video at the beginning of this Instructable. If it works, congratulations, you're done! If not, tell me the problem in the comments and I'll do my best to help you. 

If you want to do more experiments with Arduino on the breadboard, check out sciguy14's Arduino tutorials on Youtube! They're very well done, and they're how I learned originally.

I hope my Instructable helped you, this is my first one and so I appreciate any feedback you can give. Please vote for me in the Arduino contest as well! Bye!


joudsamer (author)2017-09-08

code please!

hetun (author)2017-08-25

where can i buy cheap materials not only for this but many other projects are pending because of no chaep available products

biswa12345 (author)2017-08-07

I just do that and i am facing a problem that the (goesForward). Can u explain me that one plz

COLOR ME RAD (author)2017-01-30

you can get a sensor from literally ebay or amzon anywhere really

RN13 (author)2017-01-05

well this is cool

arduinonovice (author)2016-12-14

Where can you get a sensor from

SudamC (author)2016-07-31

Hi..I Want to Kno How Can I Made this using two parallel sides.

Jeyson19 (author)2016-06-01

Need help.. I finished the build but the robot isn't moving however when I remove one of the ground pins then the bot begins to move but only moves in a circle... Anyone know why??

AbdullahA104 (author)2016-05-05

not working waste of time

SubinR (author)2016-04-27

Please Sent me this schematic

SubinR (author)2016-04-26

Is there is any youtube video for this project

Jai garg (author)2016-03-13


SakshamG9 (author)Jai garg2016-03-23

look at this link :

Jai garg (author)2016-03-13

Help me my ping has 4 pins what to do

SakshamG9 (author)Jai garg2016-03-23

look at this link :

Jai garg (author)2016-03-13

I the video the ping sensor also has a servo attached to it

Jai garg (author)2016-03-13

Good tutorial

kennethm27 (author)2016-03-06

do you have a fritzing parts placement for this? tnx in advance

rh3d made it! (author)2016-01-08

Nice tutorial, thanks! Go everything working and now to add the IR sensors.

AldiT (author)2015-12-22

please help me.

KrazyCuban. (author)2015-11-21

Hey is it okay if I use a Futoba Servo for my ping sensor. The only difference between the Futoba and my regular servos is that the Futoba doesn't go all the way around. It only goes halfway and then stops

Arduin125 (author)2015-11-04

You get a lot of problems in the comments but I would just like to say I love this instructable and enjoy you robots!

Jelmer8 (author)2014-12-15


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



kavinmk (author)Jelmer82015-02-05

Even I want a code for a 4 pin !

maggiemcfee (author)kavinmk2015-08-23

Connect the Trig and Echo pins from your 4-pin sensor both to the same pin (pin 7). The code switches the pin mode between input and output on the fly so it can use one wire for both. The HC-SR04 doesn't care.


If you don't want to do that, you need to connect Trig to pin 7 and connect Echo to a different pin and change the code to listen for input on that pin instead.

Untested, but basically:

* Plug echo into pin 8
* inthe code after "const int pingPin = 7;" add

const int echoPin = 8;

* change the code after "//Get duration it takes to receive echo" like so:

//Get duration it takes to receive echo
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

(you could also move the pin mode declarations up top since they're not changing modes any more, but you don't have to)

master.s.blader (author)Jelmer82015-02-02

hi i m surya just connect it to a breadboard after that to the arduino digipin

kavinmk (author)master.s.blader2015-02-05

Hey surya, Dint get you, Can you please explain it ?

gasconmarvinlogico (author)2013-01-28

hello im just wondering. my left and right servo won't spin 360degrees but it only spin to 180 degrees.. how to make it spin to 360...

A bit late but if you're still confused, you probably do not have a continuous rotation servo. Most servos cannot spin in a full circle and those that can are specially titled as continuous rotation servo.

wmada (author)2015-05-06

if you need help n code check here

321patrick123 (author)2015-03-12

Hello, i made this robot, made all the connections, uploaded the code to the arduino, but nothing happens. I unplugged the arduino, applied power from the 9v battery (via the Vin port thing), put the robot an an open space yet nothing happens. However, if i put my ear up close to it, i can hear a low, humming noise that i /think/ is comming from the servos

ToBi009 (author)2015-01-23

Is there someone who has code for wall follwer in which left turn has higher priority than right using 3 ultrasonic sensors. Plz reply fast. It would be the great help

mintesinot (author)2014-02-06

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 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

Joxman2k (author)mintesinot2014-10-18

Is the photodiode and IRLed in one sensor? Like a Sharp IR Sensor ( ?

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:

Hope that helps.

aarias5 (author)2014-04-27

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 (author)aarias52014-10-18

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_harsh (author)2014-03-25

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

Joxman2k (author)awusum_harsh2014-10-18

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.


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

itchylibido (author)2014-02-06

bot with ping added :)

Thanks again

rymi (author)itchylibido2014-09-22

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

Mayoogh_Girish (author)2014-09-22

can i replace servo using dc geared motors??

sangelek (author)2013-02-19

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

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at
PING))) code by David A. Mellis and Tom Igoe
#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()
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
rightMotor.write(RForward); //move forward
else //if path is blocked
rightDistance = ping(); //scan to the right
leftDistance = ping(); //scan to the left
panMotor.write(90); //return to center

void compareDistance()
if (leftDistance>rightDistance) //if left is less obstructed
rightMotor.write(RForward); //turn left
else if (rightDistance>leftDistance) //if right is less obstructed
rightMotor.write(RBackward); //turn right
else //if they are equally obstructed
rightMotor.write(RBackward); //turn 180 degrees

long ping()
// Send out PING))) signal pulse
pinMode(trigpin, OUTPUT);
digitalWrite(trigpin, LOW);
digitalWrite(trigpin, HIGH);
digitalWrite(trigpin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echopin, HIGH);

//Convert duration into distance
return duration / 29 / 2;

Joxman2k (author)sangelek2014-05-08

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.


generalgeek314 (author)sangelek2013-02-19

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.

xxxTESLAxxx (author)2013-06-18

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º?

Joxman2k (author)xxxTESLAxxx2014-05-08

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.

Nikh_m_thunder (author)2014-03-05

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?

Joxman2k (author)Nikh_m_thunder2014-05-08

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

ramjetrth (author)2014-02-09

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?

About This Instructable




Bio: A kid who is passionate about robotics, electronics, and computer programming.
More by generalgeek314:How To Make an Obstacle Avoiding Arduino Robot
Add instructable to: