How to connect servo to arduino

25,744 Views
How to connect servo to arduino

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

Servo wiring:

Black or brown goes to Ground

Red or orange goes to Vcc(4.8V ot 6V)

White or Yellow goes to digital output and is called signal wire.

 

How much voltage to use ?

Well most of the servos run happily between 4.8V to 6V but it can be different(rarely happens).However you'll get to know that in the servo specs.

 

Connecting two servos to arduino

In this method we will use two different batteries i.e 9V(for arduino) and 6V(for servos)

Refer the figure below for connections

Note: it is very important to combine the ground of servo and arduino.

 

Done with the connections ? Now upload the code

Code


// Community of Robots//
// Driving two servos simultaneosly with arduino//

#include <Servo.h>
Servo first; // create servo object
Servo second; // create servo object

int firstservopin = 8;                               // digital output to which signal wire(yellow) of first servo is connected
int secondservopin= 7;                      //  digital output to which signal wire(yellow) of second servo is connected

void setup() {
  first.attach(firstservopin); // attach the servo to digital output 8
  second.attach(secondservopin); // attach the servo to digital output 7
  first.write(90); // center the servo
   second.write(90); // center the servo
   delay(1000);
}

void loop() {
 
  first.write(0);                        // move first servo to pos 0
  second.write(0);                 // move second servo to pos 0
  delay(1000);
  first.write(180);               // move first servo to pos 180
  second.write(180);         // move second servo to pos 180
  delay(1000);
}


 

The above code will turn both the servos simultaneously

 

You can also use the below code.This code will the make the servos sweep simultanesoulsy


// Community of Robots//
// Sweep example code for two servos//

#include <Servo.h>
Servo first;                                              // create servo object
Servo second;                                         // create servo object

int firstservopin = 8;                               //digital output to which signal wire(yellow) of first servo is connected
int secondservopin= 7;                          //  digital output to which signal wire(yellow) of second servo is connected
int pos = 0;

void setup() {
  first.attach(firstservopin);                           //attach the servo to digital output 8
  second.attach(secondservopin);                 // attach the servo to digital output 7
  first.write(90);                                          // center the servo
  delay(1000);                                            // wait for the servo to finish its spin
  second.write(90);                                   // center the servo
  delay(1000);
}

void loop()
{
  for(pos = 0; pos < 180; pos += 1)                  // goes from 0 degrees to 180 degrees
  {                                                            // in steps of 1 degree
    first.write(pos);                                      // tell servo to go to position in variable 'pos'
    second.write(pos);
    delay(15);                                           // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos >=1; pos-=1)                 // goes from 180 degrees to 0 degrees
  {                                
    first.write(pos);                                  // tell servo to go to position in variable 'pos'
    second.write(pos);
    delay(15);
}
}
 


NOTE:Battery should always be connected after all the connections have been made and checked.

Embedded video:
See video

Comments

hbajwa9's picture

h-bridge

hi,

 

First off all thanks for refering me to this page. I was wondering how would I go about connecting a h-bridge to this circuit as i would like my motors to go forwards and to be able to reverse aswell, also how would I implent my code into the arduino? 

 

Thanks!

 

Haadi

kawal's picture

I am not sure what you want

I am not sure what you want but if you are using servo motors for driving your robot then you do not need Hbridge.

targetdg15's picture

thanks!

I just got started in the hobby and this was a super helpful tutorial! I'll be sure to pass it along!

Dortha's picture

Thanky Thanky for all this

Thanky Thanky for all this good inofrmtaion!

Add a new comment

Login or register to post comments

New to community of robots ?

Ratings

0

Advertisement