How to make your first robot using arduino
Need to get started with robotics ? Heres a step by step tutorial on how to make your first robot using arduino,an open source development platform.
P.S :The method used for chassis construction is just an example and a quick way for making a robot.You can use any design for the robot and also any materials for mounting the parts.Your imagination is the limit.
Now lets get started
Materials needed
1 x Arduino uno/duemilanove.
The Arduino Uno is a microcontroller board based on the ATmega328.You can read more details here.

1 x Standard servo
Servos are basically Dc motors with position feedback that means you can tell the microcontroller through your code to move the servo to the desired position.In arduino a standard servo can be moved between 0 to 180 degree and 90 is the servo center(makes sense).Most of the hobby servos run happily between 4.8V to 6V.Supplying more than 6V may damage your servo.
Servo has three wires i.e.Brown,Red,Yellow or the color maybe Black,Red,White.
Brown or black wire is to be connected to ground of the microcontroller.
Red wire is to be connected to Vcc(4.8V to 6V).
Yellow or white wire is to be connected to the digital output of the microcontroller and is called signal wire.

1 x Breadboard
Half sized breadboard is enough for this robot

Gauge wires or male to male jumper wires
For making connections on the breadboard you will need either gauge wire or male to male jumper wires.
Your best deal would be gauge wire as it is available in local electronics/hobby shop.Male to male jumper wires are sometimes hard to find.

1 x L293D motor driver
L293D is a 16 pin chip and is a popular motor driver which can be used for small motors that have low output current.

2 X Plastic gear motors
Plastic gear motors are very lightweight,small in size and easy to mount.The motors are available in many RPM(revolutions per minute).You can buy the motor between 60 to 100 RPM for this robot.The lower the RPM ,the greater will be the torque(doesn't matter much for this robot).

2 x Wheels compatible with plastic gear motors
You will get two screws with these wheels.Those two screws are used for attaching the wheels to the plastic gear motors.

1 x Castor wheel

1 X Sharp GP2D12 analog distance sensor.
The sensor consists of two eyes.One eye sends the infrared light and the other eye sees the reflection of that infrared light and measures the distance which is then sent to the microcontroller through analog input to perform further operations based on the distance(the operations should be defined in the code).
It is possible that you may not get GP2D12 model.Another option is Sharp GP2Y0A02YK.
The only difference between both the model is the range of distance measurement.
GP2D12 measures distance from 10cm to 80 cm.
GP2Y0A02YK measures distance from 20cm to 150cm.
There is also GP2D120 (i think it is discontinued) which measure distance from 4cm to 30cm
Make sure you get a JST 3-pin connector with these sensors for easy interfacing.
There are three wires coming from the sensor.i.e.Red,Black adn White or it can be Red,Brown and Yellow.
Red is connected to 5V of arduino.
Black or brown to Ground of arduino.
White or yellow to analog input pin of arduino i.e. in this case to analog pin 0.

Batteries
Arduino itself needs 9V(recommended).So it is better to use two different battery packs.
1 x 9V PP3 battery for arduino and 4 AA batteries i.e. 6V for motors and servo.
Make sure you buy good quality batteries like Duracell or so.

1 x 4 AA Battery Holder

2 x Battery connector
One battery connector should have a DC plug at the end so that it can be directly plugged in to the dc jack of arduino.The Dc plug can be bought at any local electronics shop easily.

1 x Acrylic sheet
This acrylic sheet will be used as the robot base.It is not necessary that you have to use a acrylic sheet.You can use plywood,aluminium,etc.But try to use acrylic as it is easy to cut.Use a hack - saw to cut the acrylic plate.
I used a big acrylic sheet for the robot base because of the breadboard i.e for ease of placing parts.

1 x USB cord
The sketch(code) will be downloaded to the arduino with this USB cord from the PC.

1 x double sided tape
We wont be using any screws or nuts for mounting the components.We will just stick the components on the acrylic plate using double sided tape.

Soldering Iron
A standard soldering iron

So got all the materials ? Now lets start with the robot
First solder the wires to the motor leads.In my case,i have cut the male to male jumpers in half and soldered them to the motor leads.It helps in easy connections on the breadboard.

Now mount the wheels to the motor shaft with the help of screw that you got with the wheels.

After that mount the castor wheel on the bottom front and center(roughly) of the robot using double sided tape.The castor wheel usually comes with holes in it for easy mounting using small nuts and bolts but if you dont want to drill holes on the acrylic sheet(robot base) then you can simply stick it with double sided tape as i did.

Now place the two motors on the acrylic sheet with the help of double sided tape as shown in the below image.It would be better if you add some superglue or hotglue as the double sided tape sometimes may not be able to handle the robot weight.Another option would be to use clamps which are available on hobby webshops but again you will have to drill holes for that.I applied hot glue for mounting the motors
The distance between the front castor wheel and the rear wheels should be as less as possible.
Place a servo in the front using double sided tape.The servo should come above(roughly) the castor wheel.
The front of the robot will be where servo is.
Place arduino and breadboard on the base as shown in below image(use double sided tape).
Ok so now place a 9V battery with the connector(with dc plug) and 4 AA batteries with the connector(without the dc plug) on the robot base.
The sensor will be on the servo but dont mount the sensor yet. Because before mounting the sensor on the servo,the servo should be in its center position i.e . 90
Now comes the most important part of the robot building.
Thoroughly view the circuit and make connections accordingly.After making the connections check twice or maybe thrice before powering the robot otherwise you may damage any component if there is some mistake in the connection.
P.S : Use the dc plug to power arduino with 9V battery.The dc plug is not included in the image below because that
software didn't have a dc plug.

High resolution image of the circuit can be found here
The power distribution in this circuit is as follows:
The sensor and both the enable pins of L293D motor driver are powered through 5V regulated supply from the arduino board.The arduino uses 9V power and regulates it to 5V with the help of onboard voltage regulator.
The motors and servo are powered by 6V(4 AA batteries).
From the circuit:
The signal wire(white or yellow) of the sharp sensor is connected to analog pin 0 of the arduino,Vcc(red) to 5V and ground(black or brown) to ground of arduino.Color of the wires may vary.
The signal wire(white or yellow) of servo is connected to digital pin 8 of arduino,Vcc(red) to 6V and ground(black or brown) to ground of arduino.(the ground of the 6V battery pack and arduino should be combined).Color of the wires may vary.
The motors are driven by digital pins 4,5,6 and 7 and are powered by 6V.
Programming
The first thing you need to do is install arduino IDE and setup your arduino.
The arduino IDE can be downloaded from here and the step by step instructions on how to connect your arduino board to the computer can be found here
Ok,so you got your arduino board ready ? Now lets start with the programming.
Remember we didn't mount the sensor on the servo yet.First we will do that.
Make sure you have connected both the power supply as per the circuit diagram.
Open your arduino IDE and copy paste the below code and then click on upload button and then you should get a message "Done uploading".
#include <Servo.h> // includes the servo library
Servo myservo; // creates a servo object
void setup()
{
myservo.attach(8); // attaches the servo to digital pin 8
}
void loop()
{
myservo.write(90); // moves the servo to 90 degree.
delay(700); // wait for the servo to finish its rotation
}
The above code will move the servo to center position i.e. 90.Now mount the sensor facing forward on the servo horn using clamp or double sided tape.
After the sensor is mounted ,you can now play with your servo by moving it left or right by using values between 0 and 180 instead of 90.
Now type in or copy paste this code.
int sensorpin = 0; // analog pin used to connect the sharp sensor int val = 0; // variable to store the values from sensor(initially zero) void setup() { Serial.begin(9600); // starts the serial monitor } void loop() { val = analogRead(sensorpin); // reads the value of the sharp sensor Serial.println(val); // prints the value of the sensor to the serial monitor }
This code is for the sharp analog sensor.After succesfully uploading this code open the serial monitor in arduino IDE and you will see some values changing rapidly.Yes those are the readings(can say distance) coming from the sensor.
Keep your hand or anything infront of the sensor and you see the change in the readings on the serial monitor.The analog values are in the range of 0 to 1023.
IMPORTANT : Lower the analog sensor value greater is the distance and vice versa.
Now making the motors rotate.Copy paste or type the below code in the arduino IDE.
int motor_pin1 = 4; //define the pins to which motor wires are connected int motor_pin2 = 5;
void setup ()
{
pinMode(motor_pin1,OUTPUT); // set the motor pins as output
pinMode(motor_pin2,OUTPUT);
}
void loop()
{
digitalWrite(motor_pin1,HIGH);
digitalWrite(motor_pin2,LOW);
}
This will make one of your motor turn in one direction.The direction can be forward or backward.
If it turns backward then use this code and your motor will turn forward.
int motor_pin1 = 4; //define the pins to which motor wires are connected int motor_pin2 = 5;
void setup ()
{
pinMode(motor_pin1,OUTPUT); // set the motor pins as output
pinMode(motor_pin2,OUTPUT);
}
void loop()
{
digitalWrite(motor_pin1,LOW);
digitalWrite(motor_pin2,HIGH);
}
Note in this code we used LOW for motor_pin1 and HIGH for motor_pin2 i.e opposite to the previous code.
Hence the motor moves in opposite direction.
For the second motor, type or copy paste this code in the arduino IDE
int motor_pin3 = 6; //define the pins to which motor wires are connected
int motor_pin4 = 7;
void setup ()
{
pinMode(motor_pin3,OUTPUT); // set the motor pins as output
pinMode(motor_pin4,OUTPUT);
}
void loop()
{
digitalWrite(motor_pin3,HIGH);
digitalWrite(motor_pin4,LOW);
}
This will turn the second motor in one direction.It can be forward or backward.
If it is moving backwards then use the same logic as for the previous motor to move the second motor forward.
For stopping all the motors make all the motor pins LOW.
Make sure you write down the combinations for forward and backward of each motor somewhere as it will be needed in the final code.
You might have a question in your mind that how will the robot turn left or right.
Heres the answer : We will be using skid steering method for turning the robot.In this method ,the robot skids while turning and hence the name skid steering.
It goes like this
For turning the robot to left:The right motor rotates forward and the left motor rotates backwards.
For turning the robot to right:The left motor rotates forward and the right motor rotates backwards.
For moving forward: Both the motors rotates forward.
For moving backward: Both the motors rotates backward.
Completed robot

And here is a simple code for avoiding obtacles.Just copy and paste this code in the arduino IDE and click on upload and your robot is ALIVE !!
#include <Servo.h> //includes the servo library
int motor_pin1 = 4;
int motor_pin2 = 5;
int motor_pin3 = 6;
int motor_pin4 = 7;
int servopin = 8;
int sensorpin = 0;
int dist = 0;
int leftdist = 0;
int rightdist = 0;
int object = 500; //distance at which the robot should look for another route
Servo myservo;
void setup ()
{
pinMode(motor_pin1,OUTPUT);
pinMode(motor_pin2,OUTPUT);
pinMode(motor_pin3,OUTPUT);
pinMode(motor_pin4,OUTPUT);
myservo.attach(servopin);
myservo.write(90);
delay(700);
}
void loop()
{
dist = analogRead(sensorpin); //reads the sensor
if(dist < object) { //if distance is less than 550
forward(); //then move forward
}
if(dist >= object) { //if distance is greater than or equal to 550
findroute();
}
}
void forward() { // use combination which works for you
digitalWrite(motor_pin1,HIGH);
digitalWrite(motor_pin2,LOW);
digitalWrite(motor_pin3,HIGH);
digitalWrite(motor_pin4,LOW);
return;
}
void findroute() {
halt(); // stop
backward(); //go backwards
lookleft(); //go to subroutine lookleft
lookright(); //go to subroutine lookright
if ( leftdist < rightdist )
{
turnleft();
}
else
{
turnright ();
}
}
void backward() {
digitalWrite(motor_pin1,LOW);
digitalWrite(motor_pin2,HIGH);
digitalWrite(motor_pin3,LOW);
digitalWrite(motor_pin4,HIGH);
delay(500);
halt();
return;
}
void halt () {
digitalWrite(motor_pin1,LOW);
digitalWrite(motor_pin2,LOW);
digitalWrite(motor_pin3,LOW);
digitalWrite(motor_pin4,LOW);
delay(500); //wait after stopping
return;
}
void lookleft() {
myservo.write(150);
delay(700); //wait for the servo to get there
leftdist = analogRead(sensorpin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void lookright () {
myservo.write(30);
delay(700); //wait for the servo to get there
rightdist = analogRead(sensorpin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void turnleft () {
digitalWrite(motor_pin1,HIGH); //use the combination which works for you
digitalWrite(motor_pin2,LOW); //right motor rotates forward and left motor backward
digitalWrite(motor_pin3,LOW);
digitalWrite(motor_pin4,HIGH);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
void turnright () {
digitalWrite(motor_pin1,LOW); //use the combination which works for you
digitalWrite(motor_pin2,HIGH); //left motor rotates forward and right motor backward
digitalWrite(motor_pin3,HIGH);
digitalWrite(motor_pin4,LOW);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
Heres what this code will do.
The robot will move forward if the sensor has reading less than 500.It means that the robot will consider that it has no object infront of it if sensor reading is less than 500.You can set this as per your needs.
If the sensor gets reading greater than or equal to 500 then it will look for another way in the following steps.
First the robot will stop completely,move little backwards,again the robot stops,the servo will move left and sensor will take reading and after that the servo will move right and the sensor will again take reading.The left and right readings are compared.The robot turns towards the direction where the reading is more less(lesser the analog reading greater is the distance) and stops.Now again the same code or cycle repeats till you switch off the power supply.
Congrats you have built your first robot.
Enjoy !!
More pictures of the robot



Important points to remember:
- If you find that the microcontroller is reseting when the motors start rotating,connect a 10uF capacitor across the 6v power supply.The long lead of the capacitor is +ve and the short lead is -ve.
- The delay used in the turnleft,turnright,halt subroutines in the code should be according to the size of your robot.Smaller the size, smaller should be the delay and greater the size ,greater should be the delay.
- In this code, the left of the servo is on the degree greater than 90 and right on the degree less than 90.For you it may be opposite as it depends on how you placed the servo.Adjust the servo degrees accordingly
- Always combine the ground of arduino,servo and external battery.
- The distance between the front castor wheel and the rear wheels should be as less as possible.Reduce the distance if you find that your robot is not turning properly.
- Remember to solder male header pins or gauge wires at the end of the component cables so that they can be easily stacked on the breadboard.
- Please check the connections thoroughly before powering the robot.
- Do not supply more than 6V for the plastic gear motors and servo.
- The sensor should be powered with not more than 5V
if you have any problems or questions regarding this robot then please post it in the forum.
Also if you find any mistakes in this tutorial please let me know.
P.S Out of the two videos one video may not work in your country as it is blocked in some countries due to music.In such case you can view another video.













Save Weight
Dear Kawai, First of all thank you for the comprehensive article "How to make your first robot."
Iam a newbe in this field, making robots and the arduino but got enough experience in the electronic field. I noticed you are using two batteries namely one 9 Volt for the `arduino` and one 6 volt. for the servo motor.
Now in order to save expenses and save weight at the same time.
Why not make use of a 7805 voltage regulator ? The input fed from the 9 Volt battery
and the 5 Volt fed to the Servo motor.And the Motors driver with adequate heatsink it can drain over 1 Amperes. The motors are consuming a total 0.5 Amperes It requires some alterations.... Tapping it from the 9 Volt
I know , but with a bit of imagination it can be done. After all that what tinkering is all about.
This will be a one time purchase of the regulator which can be used in many other appliances.
Thank you for reading this.
regards
Gadget-fan
9V battery
Hi Gadget-Fan,
9V batteries are not capable of providing enough current.It when used with motors and servos drains much faster and the motors and servos barely work.
Using 7805 is a good option but for beginners it adds to the complexity of circuit.
9 Volt battery
Good day Kawai, I do not know what the Ah of the 9 Volt batteries are But I do know what the t the Ah of Li-PO batteries are because that is mentioned on the label. A 7.4 Volt battery for instance has a capacity of 800mAh, ample enough for at least a hour of running the robot. They are very light and can be recharged, another bonus. And because the differential Voltage is only 2.4 Volt, less heat will be generated on the Voltage regulator. The Voltage regulator on the board is not capable of providing the necessary wattage. It is meant for the board and some LEDS ....not for heavy loads. What complexity concerns. I am sure that there is a variety of readers/ builders who will not shy away from making changes in order to make something more effecient. Tthis is not meant as a critque to the contrary The article is good and I like it. But robotics is about experimenting otherwise one will get bored very quickly after the project is working. Keep up the good work Kawai. You'r are helping!
Good day, I appreciate the
Good day,
I appreciate the feedback.
But using only 9v battery for powering the complete robot is something i would not recommend.Instead we can use 4AA(6v) batteries for the complete robot.The motors used in above tutorial need 6v and the servos also work well on 6v.So we can simply use the Vin pin on arduino to power motors and servos if the arduino is powered by 4AA(6v).As for the sensor, 5v regulated voltage from arduino is enough.So in this way we get a good amount of current fro motors,servos without using external voltage regulator.
This robot is more of a experimenting platform for beginners than a robot :)
The version 2 of this tutorial is on its way and this time the robot is going to be compact probably using a single battery pack.
If you have any suggestions then i would be glad to read.
....and my name is Kawal."L" instead of "i"
Have a good day!
Voltage control and Current sensing
Hi,
whilst wandering round the i/net spotted this discussion about current sensing for motors that could stall and burn a standard circuit http://letsmakerobots.com/node/18517 by drawing too much current
HTH
Don
WFO
Don
In the Center of Sunny Trinidad AMEN
How to make the robot move at specific distances
hello. thank you for reading my question. I have done the robot above and did the programming too. However, i want to have modifications in my robot. The situatiuon is i want my robot to travel 5 cm ahead and after reaching that turn to the right and travel another 5 cm and turn left and travel another 20 cm. I have limited knowledge about programming and any hints on how to do this will be greatly appreciated.
There are two ways to achieve
There are two ways to achieve it:
1.Using wheel encoders(recommended)
2.Using arduino delays(works but not reliable)
Pseudo algorithm(using delays):
1.Mark a straight line of 5cm on the floor or some place where your robot can move.
2.Place the robot at the start point of 5cm line.
3.Write a code for moving your robot forward.Just below that code,add a delay with any random value for e.g delay(400).Compile and upload the code.This code will make your robot move forward for 400 milliseconds.Use trial and error method to find out the delay required for your robot to reach at the end point of 5cm line.
4.Just after the delay,write code for making the robot stop i.e. set all motor pins to LOW.
4.After that write code for turning right and find out the delay required for your robot to turn right and then follow the same procedure for further operations of robot.
P.S You need to execute this code only once so do not write this code in loop().Instead write it in void setup() function.
void setup()
and this method is not tested....
hellow kawal
how i can coding the arduino if i use 2 type sensor which is ultrasonic ping sensor and IR sensor for obstacles robot like you??before this, i had build the sketch for my robot and its verify when i compiling..but the problem is when i implement it on hardware its not functioning. i cant detect the matter it is..below is my sketch..
Nazli
Hi,
I am not sure what the problem is but it may be co-ordination of sensor readings with motors.A guy posted a similar forum topic and i suggested the same to him but not sure whether his problem is solved or not.Bump this thread so that he can help you if he ever figured it out.
P.S. As you said the robot does not work only when all the components are connected ,the problem is most probably what i suggested because working of servo and motors is heavily dependent on sensor readings.So if you have not debugged your sensor readings carefully,debug it again and then try again with new values(if any).
Circuit not functioning
Hi Kawal,
I've been constructed the circuit exactly the same as yours. All items are exactly the same. The program downloaded to the board successfully. The servo also functioning well before connected to the circuit and after discarded from the circuit (use testing servo code). Meanwhile, the gear motor also funtion properly when connected straight to battery.Unfortunately, nothing happen to the robot.
Can you give me any alternative that i should try to make the robot run?
Thank you in advance