How to make your first robot using arduino

227,527 Views
How to make your first robot using arduino

style="display:inline-block;width:468px;height:15px"
data-ad-client="ca-pub-3507530299542268"
data-ad-slot="6937426494">

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:

  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

Comments

Goofballcent's picture

Survo interference

So I have my robot fully Assembled and I did the first few codes. It works until I get to the part about testing the motors. When ever the motors spin, the survo goes nuts and then the arduino will start to reset it self. I tried to run the last bit of code labeled, obistical avoidance but when I turn the robot on, I get no response for the robot what so ever. The LEDs on the arduino are on, but the motors, the survo, nothing does anything, I just don't get it...

kawal's picture

This should solve your

This should solve your problem.

 

 

robotist's picture

arduino

can i use arduino uno r3

robotnerd's picture

Thank You!

Hey Kawal,

Thank You so much for this turoial, its been like the simplest tutorial, to top it up u included the images too which most tutorials DONT. Thank You

Goofballcent's picture

Motors

On the motors in schematic, which color is positive and negative?

kawal's picture

It doesn't really matters.

It doesn't really matters.

ehms_21's picture

how can i use the ultrasonic

how can i use the ultrasonic sensor on this robot? 

please help me.

kolnari's picture

Thank you!

Your article and code is just the push in the right direction I needed. Wanted to let you know that I found your info and ocde easy to follow.

Kolnari

gurprith's picture

help

hi,

i was just wondering what part of the code will need to be changed if i was to swap the sensor to a ultrasonic ping sensors, so just been trying to figure it out but keep getting error messages.

if you can help with it, ill appreciate it alot

heres what i have

 

#include <Servo.h>                                  //includes the servo library

int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int servoPin = 9;

const int pingPin = 8;
const int dist = 0;

int leftdist = 0;
int rightdist = 0;
int object = 10;             //distance at which the robot should look for another route                           

Servo myservo;

void setup ()
{
  Serial.begin(9600);   // initialize the data rate in bits per second
  myservo.attach(9);
  pinMode(motorPin1,OUTPUT);
  pinMode(motorPin2,OUTPUT);
  pinMode(motorPin3,OUTPUT);
  pinMode(motorPin4,OUTPUT);
  myservo.attach(servoPin);
  myservo.write(90);
  delay(700);
}
void loop()
{
   

 
   
  if(dist < object) {                         //if distance is less than 5
   forward();                                  //then move forward
  }
  if(dist >= object) {               //if distance is greater than or equal to 5
    findroute();
  }
}
 
void forward() {                         
   digitalWrite(motorPin1,HIGH );
   digitalWrite(motorPin2,LOW);
   digitalWrite(motorPin3,HIGH);
   digitalWrite(motorPin4,LOW);
   return;
 }
 
void findroute() {
  halt();                                             // stop
  reverse();                                       //go backwards
  lookleft();                                      //go to subroutine lookleft
  lookright();                                   //go to subroutine lookright
                                      
  if ( leftdist < rightdist )
  {
    turnleft();
  }
 else
 {
   turnright ();
 }
}

void backward() {
  digitalWrite(motorPin1,LOW);
  digitalWrite(motorPin2,HIGH);
  digitalWrite(motorPin3,LOW);
  digitalWrite(motorPin4,HIGH);
  delay(500);
  halt();
  return;
}

void halt () {
  digitalWrite(motorPin1,LOW);
  digitalWrite(motorPin2,LOW);
  digitalWrite(motorPin3,LOW);
  digitalWrite(motorPin4,LOW);
  delay(500);                          //wait after stopping
  return;
}
 
void lookleft() {
  myservo.write(150);
  delay(700);                                //wait for the servo to get there
  leftdist = digitalRead(pingPin);
  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 = digitalRead(pingPin);
  myservo.write(90);                                  
  delay(700);                        //wait for the servo to get there
  return;
}

void turnleft () {
  digitalWrite(motorPin1,HIGH);       //use the combination which works for you
  digitalWrite(motorPin2,LOW);      //right motor rotates forward and left motor backward
  digitalWrite(motorPin3,LOW);
  digitalWrite(motorPin4,HIGH);
  delay(1000);                     // wait for the robot to make the turn
  halt();
  return;
}

void turnright () {
  digitalWrite(motorPin1,LOW);       //use the combination which works for you
  digitalWrite(motorPin2,HIGH);    //left motor rotates forward and right motor backward
  digitalWrite(motorPin3,HIGH);
  digitalWrite(motorPin4,LOW);
  delay(1000);                              // wait for the robot to make the turn
  halt();
  return;
}
long duration, inches, cm;  //  function from datasheet of ping sensor
  pinMode(pingPin,OUTPUT);    //  
  digitalWrite(pingPin,LOW);
  delayMicroseconds(2);       // delay recomended from datasheet
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(5);       // delay recomended from datasheet
  digitalWrite(pingPin,LOW);
  pinMode(pingPin,INPUT);
  duration =pulseIn(pingPin,HIGH);
 
  inches = microsecondsToInches(duration); // converting time into distance
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);

long microsecondsToInches(long microseconds)

{
  return microseconds / 74 / 2;  

}

long microsecondsToCentimeters(long microseconds)

{
 
  return microseconds / 29 / 2;

air's picture

proj

for our final project we decided to make this .. and this is it but we cant make this robot in a givin day !!

 

can you send me the code which is a combination of all codes .. complete code is provided but we need to pass this before the deadline..

if you can send it to me then very big help to us .. shekinah_niece@ymail.com

im hoping !! godbless

thank you..

Add a new comment

Login or register to post comments

New to community of robots ?

Ratings

5

Advertisement