Introduction: Obstacle Avoiding Robot

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.

Why?

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.

https://www.youtube.com/RoyPeer1

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 !

Step 14: Materials Links

Comments

author
benhokh (author)2017-08-02

Hi I am Ben.

After I imported the code, I run the program. The pre-run failed. The words at the bottom said "

redefinition of 'void setup()'

exit status 1

redefinition of 'void setup()'

I don't know what to do. Please help.

Many Thanks

author
JwalaN (author)2017-07-06

i need all code of this robot

author
JwalaN (author)JwalaN2017-07-06

in the picture there is only half code seen so send me full code.

author
PhilipW63 (author)2017-06-05

Hey there, I'm almost finished with my build and everything works great, EXCEPT it only works when plugged into the USB port. Any idea why this might be? p.s. I'm pretty new to Arduino!

author
PhilipW63 (author)PhilipW632017-06-05

The LEDs on both the motor shield and the Arduino Uno stay lit green, but the motors stop running.

author
citin made it! (author)2016-05-25

hy my version runs only forward and stops at opstacle.

have the motorshield from

http://www.sainsmart.com/arduino/arduino-shields/s...

and arduino uno

dubelgearbox tamiay

http://www.amazon.com/Tamiya-70168-000-Gearbox-Ind...

tracked vehicle chassi

http://www.amazon.com/Tamiya-70108-Tracked-Vehicle-Chassis/dp/B00061HHTK

2 AA x4 batterieholder, 1 for aduino 1 for the 2 motors.

opstacle.jpgIMG_20160525_174639.jpg
author
zaeemn (author)citin2017-03-19

Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help

give me programming

author
eagleanurag (author)zaeemn2017-05-26

hey it is nothing just need a simple change in the code put your instruction in coding where he gave the instruction to move the motor in other direction , you have to stop both motor in that case by giving 0,0 &0,0 or 1,1 &1,1 by giving this binary motor shield will stop your motor

author
eagleanurag (author)eagleanurag2017-05-26

contact me on my mail eagleanurag@gmail.com for further query or doubts...

author
NeeleshT1 (author)2016-05-23

one of my wheel is moving in the opposite direction ..

So , my robot is rotating in circle ,,please help me...

author
GreavanB (author)NeeleshT12016-09-09

change the termanels of the motor

author
zaeemn (author)GreavanB2017-03-19

Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help

author
Praveen7728 (author)NeeleshT12016-05-28

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.

author
doodlebug4 (author)2017-01-23

Hello

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.

Doodlebug3

author
zaeemn (author)doodlebug42017-03-19

Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help

author
zaeemn (author)2017-03-19

Hi, i want to stop robot when the obstacle come, instead of turninng left/right.... plz help

author
ManikandanK3 (author)2017-02-17

My robot moves forward and suddenly moves backward...and my sensor is not working

author
SarangS6 (author)2016-10-24

should i provide power to arduino or motor shield .(I am new to using motor shields)

author
sunlitspaceship (author)SarangS62017-02-16

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.

author
sunlitspaceship made it! (author)2017-02-16

Hi Roy,

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.

Ta

photo (8).JPG
author
Roopak Sharma (author)2015-11-24

hi Roy,

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.

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.

author
mc0618 (author)Roopak Sharma2017-02-13

Same issue,were you able to figure it out?

author
UmamaA (author)Roopak Sharma2016-06-14

same issue were you able to figure it out?

author
kr18 (author)Roopak Sharma2016-03-02

my problem is same as you

author
HenryU9 (author)2016-12-01

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

author
Shehjar (author)2016-11-17

Hi,

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.

author
arnav_bahri (author)2016-08-17

please help me finding my fault

author
arnav_bahri (author)2016-08-17

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

author
بكري طه (author)2016-07-13

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

author
joshua. (author)بكري طه2016-07-23

You can get it in azon

author
joshua. (author)joshua.2016-07-23

Sorry amazon

author
DanyaelD made it! (author)2016-06-15

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 "Link" and she runs great! Thanks for the tutorial!

IMG_20160608_123654.jpg
author
sibasis malla (author)2016-06-02

I dont want it to rotate 180 degrees how to program it so that it would stop when obstacle comes ..plzz help me

author
RaphaelM33 (author)2016-06-01

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

author
chaitanya lakshmi (author)2016-05-16

can we use any other microcontroller like 8051 instead of Arduino

author
Shvitzer (author)2016-05-13

OK, the fix is to simply use pin 9 for the TrigPin instead of 12.

author
Shvitzer (author)2016-05-13

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 & M2) it works fine, however when I try to use all 4 (M1, M2, M3 & 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.

author
zingo1010. (author)2016-05-08

Ok I will try and explain how to use shield V2 stuff.

Above the void setup area put this:

#include <Wire.h>#include <Adafruit_MotorShield.h>

#include "utility/Adafruit_MS_PWMServoDriver.h"

// Create the motor shield object with the default I2C address

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

// Select which 'port' M1, M2, M3 or M4. In my case, M3 & M4

Adafruit_DCMotor *motorr = AFMS.getMotor(3);//my rightside motor

Adafruit_DCMotor *motorl = AFMS.getMotor(4);//my leftside motor

In void Setup()

Put :

Serial.begin(9600); // begin serial communitication

AFMS.begin();

motorl->setSpeed(x); //set the speed of the motors, between 0-255

motorr->setSpeed (x);// I used a variable x to alter speed on demand but you can put any number between 0...stopped--255 full speed

motorl->run(RELEASE);

// NOTICE there is no dot after motorl. Most dots are replaced by typing minus sign and greater than sign thus... " ->". this is because that sign is a pointer in the code. Don't know why they changed this as its not intuitive at all.

motorr->run(RELEASE);

Now Void loop()

Here is a couple of examples how I drive the thing.

I used switch case statements to use my IR keypad but you can just as easy use sensor inputs to control the thing.

switch (results.value) {

case but1:

motorl->setSpeed (x / 2);// see the -> again...he he

motorl->run(FORWARD); // Turn right

motorr->run (RELEASE);

but1 == 0;

break;

case but2:

motorl->run(RELEASE); // Turn left

motorr->setSpeed (x / 2);

motorr->run (FORWARD);

but2 == 0;

break;

case but3:

motorl->setSpeed (x);

motorr->setSpeed (x);

motorl->run(FORWARD); // Foreward.

motorr->run (FORWARD);

but3 == 0;

break;

and so on.

I hope this helps you.

author
zingo1010. (author)2016-05-07

I made this.

The only change was to use Adafruit Motor Shield V2.

So slight differences in the setup section etc.

Worked like a charm straight away.

Well done with the post.

Works like a charm right off the bat.

author
laia5 (author)zingo1010.2016-05-08

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

author
rrozzers made it! (author)2016-05-07

great instruction lots of detail and links.

temp_-148252351.jpg
author
KomalH1 (author)2016-03-09

hi!!

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!

author
Henry1oke (author)KomalH12016-04-27

Just swap your Moto wire to either port 3 or 4 n do same in ur code

author
SakshamG9 (author)KomalH12016-03-22

just swap the polarity of motor

author
SakshamG9 (author)SakshamG92016-03-22

it really works

author
Patriek_pancake (author)KomalH12016-03-12

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

author
yewdumthecreator (author)2016-04-03

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.

author

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

author
Henry1oke made it! (author)2016-04-19

hey Roy thanks alot, i was able to make my own robot and its working well.
I was able to solve some problems some of you encountered.
just mail me if u having any difficulty and i can try and help.

IMG_3655.JPG
author
DanishA22 (author)Henry1oke2016-04-25

i am using 9V battery and it doesnt last much. what should i do?