This instructable is the written version of my "Arduino: How To Build An Obstacle Avoiding Robot" Youtube video that I've uploaded recently. I strongly recommend you to check it out.

So, without further ado, Let's get started !

Step 1: A Chassis !

the first step and the base of any robot is a chassis. i got mine from ebay, and i'll leave links for that. if you prefer, you can also make your own using cardboard and DC/Servo motors. Either way, the chassis has to include a body, two motors and sometimes even a battery holder and a switch.

Step 2: Brain

Now we need something that will orchestrate our motors, a micro-controller.

from the title of this instructable you probably have already understood that we are going to use Arduino.


well, the answer is simple. Arduino (or, specifically, Arduino Uno) is a compact, comfortable and relatively cheap micro-controller. In addition, it is very common and you can get it literally everywhere online.

Step 3: Attaching the Arduino to the Chassis

Now simply use some screws to attach the Arduino to the chassis. If you've made your own chassis/ can't find any screws, you can use a small amount of double-sided tape. If you do choose to use double-sided tape then search for an area under the arduino that doesn't have many pins/solder above it.

Step 4: A Sensor

In Order to make a robot that can maneuver by its own we need some sort of an input, so it can "know" or "judge" what to do. a very comfortable sensor that fits our goal (avoiding obstacles) is the Hc-sr04 ultrasonic sensor.

Scientific Explanation Of the sensor's action:

What the sensor does it is calculating the distance from an object by sending bursts of ultrasound towards it and measuring the time it takes to the sound waves to get back (Distance = Velocity * Time). we can use this information in order to determine whether there's a close "obstacle" near the robot and then- avoid it !

So now you know how an ultrasonic sensor work, Scientifically. Now, Let's use it !

Step 5: Attaching the Sensor to the Chassis

all we have to do next is to actually attach the sensor to the body. To make life easier, i recommend sticking a mini bread-board as well for easier wiring, although you don't have to.

Step 6: A Milestone

If it seems like the robot is staring at you, then you are on the correct way.

Step 7: Connecting the Sensor to the Brain

Now gather some wires, and connect the sensor to the arduino according to this diagram i made.

Pay attention that your ping sensor may have a different pin layout (e.g. 5 pins), but it should have a voltage pin, ground pin, trig pin and an echo pin.

Step 8: Connecting and Controlling the Motors With the Arduino

arduino boards can't control dc motors by their own, because the currents they are generating are too low. Moreover, the currents they are generating cannot be reversed, so you can't change the direction of the motor. To solve that we will use a motor driver, which helps the arduino control dc motors. The most comfortable way of using motor drivers is through shields. Motor shields are very cheap and i will leave links for where you can buy one online.

Step 9: Attaching the Motor Shield to the Ardunio

Take your motor shield and simply plug it into the arduino with the sensor's wires crunching inside.

Step 10: Connecting the Motors to the Shield

Every Motor Shield has (at least) two channels, one for the motors, and one for a power source. Locate the channels and plug your chassis' motors to the motors' channel, and whatever power source you are using ( e.g. AA batteries, Lithium Battery) to the power source's channel.

Step 11: Programming the Robot

After You've made your motor shield connections, From a hardware side, believe it or not, Your robot is Done. All you have to do next is to plug your arduino to your computer.

Now open this link and download the sketch i wrote: http://www.mediafire.com/download/dql27gnuljjpurr/...

Then, open it. Choose whatever board you're using and the com port of your arduino (my happens to be three). Finally, upload the sketch, and after a few seconds you should see a message stating that the upload is complete.

Note: If the code doesn't work, you may need to download the motor shield library from: https://learn.adafruit.com/adafruit-motor-shield/l...

you might need to download other libraries, depends on what motor shield you are using.

Step 12: Playing With the Robot and Code

After you've uploaded the code, unplug the arduino, connect your batteries and watch as the robot speeds forward and avoid obstacles. then, go over the code, examine it, play with it, improve it and modify it to fit your needs !

Step 13: About Me + What's Next ?

First of all, I would like to thank you for reading this guide ! I hope it helped you build your own robot and improved your knowledge about robotics and arduino.

I think now it's a good time to introduce myself. My name is Roy and I'm a 14.5 years old teenager who likes Math, Science, Computers, Robotics and programming. {Actually in case of Math, I don't really like It, But I'm pretty good at it ;)} . I've just started a youtube channel, hoping to help people, share some of my knowledge and learn myself.


My next Youtube (and Instructables) tutorials will deal with different methods of controlling electronics (Smartphone-Bluetooth control, TV remote control and much more) and specifically robots. There will also be videos about home automation (controlling every day appliances with your smartphone or computer using arduino/raspberry pi). so make sure you are a subscriber so you won't miss them, and I will see you next time.

Bye !

<p>hy my version runs only forward and stops at opstacle. </p><p>have the motorshield from </p><p><a href="http://www.sainsmart.com/arduino/arduino-shields/sainsmart-l293d-motor-drive-shield-for-arduino-duemilanove-mega-uno-r3-avr-atmel.html#customer-reviews" rel="nofollow">http://www.sainsmart.com/arduino/arduino-shields/s...</a></p><p>and arduino uno</p><p>dubelgearbox tamiay</p><p><a href="http://www.amazon.com/Tamiya-70168-000-Gearbox-Independ-4-speed/dp/B001Q13BIU/ref=pd_bxgy_21_img_2?ie=UTF8&refRID=0XCT9N57ATE6WYSV4SBT" rel="nofollow">http://www.amazon.com/Tamiya-70168-000-Gearbox-Ind...</a></p><p>tracked vehicle chassi</p><p><a href="http://www.amazon.com/Tamiya-70108-Tracked-Vehicle-Chassis/dp/B00061HHTK" rel="nofollow">http://www.amazon.com/Tamiya-70108-Tracked-Vehicle-Chassis/dp/B00061HHTK</a></p><p>2 AA x4 batterieholder, 1 for aduino 1 for the 2 motors.</p>
<p>Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help</p><p>give me programming</p>
<p>one of my wheel is moving in the opposite direction ..</p><p>So , my robot is rotating in circle ,,please help me...</p>
<p>change the termanels of the motor</p>
<p>Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help</p>
<p>Try running the motor in opposite direction as the other one. So if one of them is going FORWARD then at the same time make other one go BACKWARD. Also you can try to change the connection of one of the motors. Reverse the connection of one of the motors.</p>
<p>Hello</p><p>You don't need to force 2 wires into one digital pin. You can use the analogue pins as digital pins. In the motor shield there are breakout holes for analogue pins. You can bring the analogue connections to the top of the shield by soldering pins to the breakout holes. You can also bring a row of voltage pins and ground pins to the top in the same way.</p><p>Doodlebug3 </p>
<p>Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help</p>
<p>Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help</p>
My robot moves forward and suddenly moves backward...and my sensor is not working
<p>should i provide power to arduino or motor shield .(I am new to using motor shields) </p>
<p>You should only need to provide power to the motor shield (by connecting the battery red / black wires to the external power jumpers on the motor shield). Since it's plugged into the Arduino, power only needs to be provided in one place. </p>
<p>Hi Roy, </p><p>Big thanks for the excellent tutorial. My 6 year old son and I followed it and everything worked quite easily. Studying how you wrote the Arduino Sketch was quite instructive for me. You're obviously a talented person, thanks once again for teaching this to us and keep up the great work. </p><p>Ta</p>
<p>hi Roy,</p><p>i have made the circuit but and upload the programming but when i power my robot only the motor shield LED light upload there no response in the other are.</p><p>So, what could be the problem, is my motor shield is damaged or any thing else.please answer it fast i am making it for my school science exhibution.</p>
<p>Same issue,were you able to figure it out?</p>
<p>same issue were you able to figure it out?</p>
<p>my problem is same as you</p>
<p>How do you do the actual wiring for motors, power supply and arduino? The only wiring I see is for the Sensor. Wouldn't it damage the arduino to plug in the motor drive when there are already wires in some of the pins? This skips a lot of steps and leaves open a bunch of questions</p>
<p>Hi,</p><p>Dear Roy. I am having a problem with the wiring. This is getting so tricky.Everytime I try to do the wiring. Please help me. That will relieve me.</p>
<p>please help me finding my fault</p>
<p>hi, i have successfully built obstacle avoiding robot, my code is verified and i am using l293d chip with geared motor having independent supply. when i switched it on the ultrasonic sensors doesn't stop the robot but after a gap of few microseconds after hitting the wall it reversed back. it never stop before the wall</p>
Hello .. am a student in the last semester of electric engineering , now am in a vication in abudhabi and am looking for any place which i can get arduino , car kit , servo motor, lights , breadboard .... etc coz my graduation project is obstacle avoider ... please if any body can help my do it as fast as possi le
You can get it in azon
Sorry amazon
<p>Roy, great instructions, easy to follow. I adapted it since I didn't have a nice little chassis and I used your plans to run my robot that I'm building from scrap electronics and junk :) This is &quot;Link&quot; and she runs great! Thanks for the tutorial!</p>
I dont want it to rotate 180 degrees how to program it so that it would stop when obstacle comes ..plzz help me <br>
<p>If anyone is having problems his diagram for the ultrasound sensor wiring is extrememly wrong it didnt work for me and probably wont work for you because his wiring is putting the positive output of the arduino to the positive or vcc connector of the sensor vice versa with the negative charge switch the wires and you should be good to go otherwise you will fry your circuit and the robot wont work along with some possible damage to you self</p>
<p>can we use any other microcontroller like 8051 instead of Arduino </p>
<p>OK, the fix is to simply use pin 9 for the TrigPin instead of 12.</p>
<p>I think that there is a problem here. I have a chassis with 4 motors. When I use only 2 of the motors, i.e. both driven by the same bridge (M1 &amp; M2) it works fine, however when I try to use all 4 (M1, M2, M3 &amp; M4) all motors run in the same direction - the sensor stops detecting. This is probably because the sensor uses the same pin as the motor shield's 74HCT595, i.e. pin 12. </p>
<p>Ok I will try and explain how to use shield V2 stuff.</p><p>Above the void setup area put this:</p><p>#include &lt;Wire.h&gt;#include &lt;Adafruit_MotorShield.h&gt;</p><p>#include &quot;utility/Adafruit_MS_PWMServoDriver.h&quot;</p><p>// Create the motor shield object with the default I2C address</p><p>Adafruit_MotorShield AFMS = Adafruit_MotorShield();</p><p>// Select which 'port' M1, M2, M3 or M4. In my case, M3 &amp; M4</p><p>Adafruit_DCMotor *motorr = AFMS.getMotor(3);//my rightside motor</p><p>Adafruit_DCMotor *motorl = AFMS.getMotor(4);//my leftside motor</p><p>In void Setup()</p><p>Put :</p><p>Serial.begin(9600); // begin serial communitication</p><p> AFMS.begin();</p><p> motorl-&gt;setSpeed(x); //set the speed of the motors, between 0-255</p><p> motorr-&gt;setSpeed (x);// I used a variable x to alter speed on demand but you can put any number between 0...stopped--255 full speed</p><p> motorl-&gt;run(RELEASE); </p><p>// NOTICE there is no dot after motorl. Most dots are replaced by typing minus sign and greater than sign thus... &quot; -&gt;&quot;. this is because that sign is a pointer in the code. Don't know why they changed this as its not intuitive at all.</p><p> motorr-&gt;run(RELEASE);</p><p>Now Void loop()</p><p>Here is a couple of examples how I drive the thing.</p><p>I used switch case statements to use my IR keypad but you can just as easy use sensor inputs to control the thing.</p><p>switch (results.value) {</p><p> case but1:</p><p> motorl-&gt;setSpeed (x / 2);// see the -&gt; again...he he</p><p> motorl-&gt;run(FORWARD); // Turn right</p><p> motorr-&gt;run (RELEASE);</p><p> but1 == 0;</p><p> break;</p><p> case but2:</p><p> motorl-&gt;run(RELEASE); // Turn left</p><p> motorr-&gt;setSpeed (x / 2);</p><p> motorr-&gt;run (FORWARD);</p><p> but2 == 0;</p><p> break;</p><p> case but3:</p><p> motorl-&gt;setSpeed (x);</p><p> motorr-&gt;setSpeed (x);</p><p> motorl-&gt;run(FORWARD); // Foreward.</p><p> motorr-&gt;run (FORWARD);</p><p> but3 == 0;</p><p> break;</p><p>and so on.</p><p>I hope this helps you.</p>
<p>I made this.</p><p>The only change was to use Adafruit Motor Shield V2. </p><p>So slight differences in the setup section etc.</p><p>Worked like a charm straight away. </p><p>Well done with the post.</p><p>Works like a charm right off the bat.</p>
<p>Hi. How did you change the code so you can use it on the Adafruit Motor Shield V2? I am using that as well and I am having problems getting mine to work</p>
great instruction lots of detail and links.
<p>hi!!</p><p>I have done all d connection as per the instructions bt still my robot is moving around in circle its nt moving forward plz help me i have vry less time!</p>
Just swap your Moto wire to either port 3 or 4 n do same in ur code
<p>just swap the polarity of motor</p>
<p>it really works</p>
Might be ur one motor is not working properly ....plz check the connections once again or also check is ur both motors are of same rpm or not
<p>why do one of my motors, specifically motor number 1, always go at the same speed, regardless of when i change the code? I always upload and verify each time I change the code, and yes I am using the correct port type and all the correct settings. For example, I changed the speed of motor 1 to 0, just to test it out, and the motor still kept going at the same speed, which is a very fast speed, which causes the robot to go in circles. I also changed the speed of the second motor to the max allowed (255), but the robot still keeps going in circles and the sensor does not work then. I tried using both 9v batteries and the regular batter pack, neither of which work. Please help, thank you.</p>
I think one of your Moto shield port is bad you should try changing the wire from 1 and 2 to maybe 3 and four.. N do same in the codes
hey Roy thanks alot, i was able to make my own robot and its working well.<br>I was able to solve some problems some of you encountered.<br>just mail me if u having any difficulty and i can try and help.
<p>i am using 9V battery and it doesnt last much. what should i do?</p>
<p>hllo sir, everything is verified and circuit is fine but my wheels are turning opposite?any soltn cud b very helpful...ur instructable was very simple and clear..thank you</p>
<p>swap the polarity of ur motors, and plz add pics</p>
<p>How did you get the motor shield and the sensor's wires to fit together? Mine won't fit, please help.</p>
<p>nice instructable. i want to know whether i can use L293D instead of motor shield. if yes, then how? plz reply ASAP </p>
<p>I just had the problem of the robot just going round in circles, found that using 4 x AA rechargeable batteries doesn't quite give enough voltage to power everything.</p><p>Tried Non rechargeable AAs worked perfectly.</p><p>Hope this helps.</p>
made it, great explanation, made it using L298 kit.
<p>hi everyone.... i am trying to make obstacle avoiding robot with arduino uno r3 and l293d chip. my both motors are running only in one direction. please help me in finding my fault. i am using the following code</p><p>#define echopin<br> 8 // echo pin</p><p>#define trigpin 9 //<br>Trigger pin</p><p>int maximumRange = 30;</p><p>long duration,<br>distance;</p><p>void setup() {</p><p> Serial.begin<br>(9600);</p><p> pinMode<br>(trigpin, OUTPUT);</p><p> pinMode<br>(echopin, INPUT );</p><p> pinMode (2,<br>OUTPUT);</p><p> pinMode (3,<br>OUTPUT);</p><p> pinMode (4,<br>OUTPUT);</p><p> pinMode (5,<br>OUTPUT);</p><p>}</p><p>void loop ()</p><p>{</p><p> {</p><p> <br>digitalWrite(trigpin,LOW);</p><p> <br>delayMicroseconds(2);</p><p> <br>digitalWrite(trigpin,HIGH);</p><p> <br>delayMicroseconds(10);</p><p> <br>duration=pulseIn (echopin,HIGH);</p><p> <br>distance= duration/58.2;</p><p> delay<br>(50);</p><p> <br>Serial.println(distance);</p><p> }</p><p> if (distance<br>&gt;= 30 ){</p><p> <br>digitalWrite(2,HIGH);</p><p> <br>digitalWrite(3,LOW);</p><p> <br>digitalWrite(4,HIGH);</p><p> <br>digitalWrite(5,LOW);</p><p> delay<br>(200);</p><p> }</p><p> else if<br>(distance &gt;=15 &amp;&amp; distance &lt;= 25) {</p><p> <br>digitalWrite (2,HIGH);</p><p> <br>digitalWrite (3,LOW);</p><p> <br>digitalWrite (4,LOW);</p><p> <br>digitalWrite (5,LOW);</p><p> delay<br>(1000);</p><p> }</p><p> else if<br>(distance &lt; 15){</p><p> <br> digitalWrite (2, LOW);</p><p> <br> digitalWrite (3, HIGH);</p><p> digitalWrite<br>(4, LOW);</p><p> <br> digitalWrite (5, HIGH);</p><p> delay<br>(1000);</p><p> <br> digitalWrite (2,LOW);</p><p> <br> digitalWrite (3,LOW);</p><p> <br> digitalWrite (4,HIGH);</p><p> <br> digitalWrite (5,LOW);</p><p> delay<br>(1000); </p><p> }</p><p>}</p>

About This Instructable




More by RoyPeer:PC Controlled Bluetooth Robot (With a GUI !) Obstacle Avoiding Robot  DIY fan powered boat 
Add instructable to: