How to make your first robot using arduino

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.



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.



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

void loop()

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

void loop()

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

void loop()

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 ()
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
void forward() {                            // use combination which works for you
void findroute() {
  halt();                                             // stop
  backward();                                       //go backwards
  lookleft();                                      //go to subroutine lookleft
  lookright();                                   //go to subroutine lookright
  if ( leftdist < rightdist )
   turnright ();

void backward() {

void halt () {
  delay(500);                          //wait after stopping
void lookleft() {
  delay(700);                                //wait for the servo to get there
  leftdist = analogRead(sensorpin);
  delay(700);                                 //wait for the servo to get there

void lookright () {
  delay(700);                           //wait for the servo to get there
  rightdist = analogRead(sensorpin);
  delay(700);                        //wait for the servo to get there

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
  delay(1000);                     // wait for the robot to make the turn

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
  delay(1000);                              // wait for the robot to make the turn

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:

  1. 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.
  2. 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.
  3. 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
  4. Always combine the ground of arduino,servo and external battery.
  5. 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.
  6. 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.
  7. Please check the connections thoroughly before powering the robot.
  8. Do not supply more than 6V for the plastic gear motors and servo.
  9. 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.


Embedded video:
See video
See video


kawal's picture


Seems to be a good alternative.I hope prestik is more strong as compared to double sided tape.

I now use hot glue.Hot glue is very much reliable for heavy components.

Awesome man's picture

Arduino board.

When we put the example of code into the arduino, It was not working.


Also when we put the wire into 5V, the LED light was not woring,


I don't know why the light is gone.


Can you tell me why it is not working ?


Gadget-Fan's picture

Arduino board



Awesome man, your question is so vaque. Nobody will understand what you are talking about.

You have to be more specifict. Wich project are you working on ?

And what part of the code is not working (which phase)?

Which LED is not working, They do have a text printed next to them on the board.  

For example: if the ON light is not lit. The reason can be a bad connection between power source and platform or you do a have a short in one of you circuits. The fuse will open up untiil fault  is rectified. Before you do power up your system, you should check it out wich a multimeter. that is good practice. Hope this helps you.  

Gadget-Fan's picture

Line follower

Hi Kawal, This time I got your name spelled right. My apologies for spelling it wrong on the other threads.

Have you  publised in the past an article on line followers? If not are you planning to do one?

I want to build one after I have finished the robot for beginners project. But as I mentioned i am new at this game and need some guidance.

Could not get hold of  theGP2D12 so had to buy the more expensive one GP2Y0A02YK measures distance from 20cm to 150cm will it make a difference in behaviour of the bot or does it make no difference?


Cheers and beers


kawal's picture

I have not published any line

I have not published any line follower tutorial yet but will be publishing soon.


No,the sensor wont make much difference except that your robot will detect obstacles at more distance.


Gadget-Fan's picture

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.




kawal's picture

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.

Gadget-Fan's picture

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!


kawal's picture

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!

Don's picture

Voltage control and Current sensing


 whilst wandering round the i/net spotted this discussion about current sensing for motors that could stall and burn a standard circuit by drawing too much current





            In the Center of Sunny Trinidad AMEN

Add a new comment

Login or register to post comments

New to community of robots ?