• EUR€
  • £GBP
  • $USD

Arduino Project 10: How to Drive A Servo

DFRobot May 09 2017 2473

Related Product: Beginner Kit for Arduino

Servos are ideal for embedded electronics applications because they can move to a specific position accurately. Most servos can turn 180 degrees at maximum. Some even larger ones can turn to 360 degrees. They can be used in mobile platforms for detection devices such as cameras and detectors of smart vehicles, or in robotic joints.

This arduino starter kit will teach you How to Drive A Servo


DFRduino UNO R3 (similar as Arduino Uno R3)*1

Servo  *1

Jumper Cables M/M *3


The servo has three leads. The color of the leads varies between servos but the red lead is always 5V and GND will either be black or brown. The other lead is the signal lead and this is usually orange or yellow. This signal lead is connected to digital pin 9.

Circuit diagram of a servo connected to an Arduino


Sample Code 10-1

//Project 10 Servo
#include <Servo.h>                          //declare to insert Servo.h library
Servo myservo;                                // create servo object to control a servo
int pos = 0;                                       //variable pos to store position of servo
void setup() {
myservo.attach(9);                         //attach the servo to digital pin 9.}

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

After uploading the sketch, you will see servo sweeping back and forth from 0 to 180 degrees.


The sketch starts from inserting <Servo.h > library.
#include <Servo.h>
This library is already in Arduino IDE. Identify it by opening Arduino-1.0.5/ libraries/ Servo/ Servo.h.

Libraries are collections of new commands that have been packaged together to make it easy to include them in your sketches. Arduino comes with a handful of of useful libraries, such as the servo library used in this example that can be used to interface to more advanced devices.

We need to create a name in the code for the servo:

Servo myservo; // create servo object to control a servo

There is another command in the setup() function.
Declaring functions in the servo library is a bit different from declaring other functions. We need to declare various functions in the library including declaring the servo object and defining the function. Just like in the library, you need to point out the object so that the program can identify it. The format of library function is as below.

Don't miss the dot sign(".") in between the word "my servo" and "attach". myservo is the servo object we named before. So the function we invoke is:

attach(pin) assigns the pin. We  can use any digital pin, except 0 and 1. In this project, we have chosen digital pin 9.

In the main program, there are 2 for statements. The first one starts from 0 then spins to 180 degrees in 1 degree increments. The second one starts from 180 degrees and goes back to 0 in 1 degree increments.
Just like the previous command, you have to declare a name for this command. The parameters of this function is an angle. The unit is degrees.
If you want to know more about the functions in the servo library, visit the arduino website: http://ardui-no.cc/en/reference/servo

Related category: arduino kits > education kits

Last Arduino Tutorial 9: Light Sensitive LED
Next Arduino Project 11: Controllable Servo