Tutorial – Servo

Tutorial – Servo

What is a servomotor?

A servomotor is a motor that allows a precise control in terms of angular position, more than other kinds of motors.

Infact they are used in applications in which you need an high grade of angular precision. But you’ll use them not only for this reason. Infact servos are very easy to be controlled by an Arduino.

If you want to learn how to controll a Servo with an Arduino read our guide. You will learn how to use Servos in your own projects!!

If you want to know more about Servo read untill the end this article! We will explain what is inside a Servo and how they are controlled.

How is made inside?

Inside of it there are: a small DC-motor, a potenziometer, some gears and a control circuit.

  • A DC-motor is used inside a servo, but it spins too fast. So there are some gears to reduce angular speed, and get also more power from the servo.
  • Gears can be made of plastic, nylon or metal. Gears also prevent from continuous rotation. Usually Servos have a rotation angle of 180°.
  • The Potentiometer is used as an encoder to know the angular position.
  • The integrated circuit is a control circuit that controlls the DC-motor.

How are they controlled?

They are controlled by electrical pulse (PWM: pulse-width modulation). So the servo motor will change its position as a function of the PWM.

A servo has 3 pins:

  • Black for GND.
  • Red for VCC.
  • Third (usually orange or yellow) for PWM.

You need to use PWM pins of Arduino to controll a servo. Of course it is really easy for us thanks to Arduino’s libraries, infact we only need to write the desired angle in the function of the library.

How to control a Servo with Arduino

Servos are really usefull for a lot of types of applications. We will use them a lot in our tutorials, so be sure to know well how to use them!

If this is your first time using a servo or if you just don’t remember well how to use them, you’are in right place! Just read this tutorial untill the end and you will know a lot more about this awesome component.

First of all Servos are controlled by PWM (we explained it in a MechLearn tutorial. Read Here). So they change the angle as a function of the particular Duty Cycle. Of course we do not have to know the corresponding Duty Cycle to have the angle we want. We will use the library Servo.h that will help us to make it quick to create our sketches with servos.

Schematic

The schematic is really easy. Connect the red wire to 5V, the black (sometimes could also be brown) to GND and the third wire to the PWM pin that you have choosen(in our tutorial it will be pin 9).

Now we have to write our code. Here there are two simple sketches that you can do to learn the basics of this component.

We suggest you to try both the sketches and then to change some parameters as you want. You will really learn how to use a servo in your own projects!

Before going to the sketches, have a look at the methods of the servo class (note that myservo is an object of the servo class):

  1. attach() – Attach the Servo variable to a pin. – Syntax: myservo.attach(pin)
  2. attached() – Check whether the Servo variable is attached to a pin. – Syntax: myservo.attached() – it returns true if the servo is attached to a pin, false otherwise.
  3. detach() – Detach the Servo variable from its pin.  – Syntax: myservo.detach()
  4. read() – Read the current angle of the servo. – Syntax: myservo.read()
  5. write() – Writes a value to the servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. – Syntax: myservo.write(angle)

First Example Code

The first tutorial will teach you how to create an object of the Servo class and how to get a simple movement.

#include <Servo.h>   //we are including the library
Servo myservo;       //we are creating an object for the class Servo
int angle = 0;       //that's the variable that we will use to change the angle 

void setup()
{
  myservo.attach(9); // we are using a Servo's class method. With it we are stating that the pin to which is attached the servo is the pin 9
  myservo.write(0); //we initialize the servo's angle to 0
}
void loop()
{
  myservo.write(90); //the servo will change the angle and won't do any other thing
}

Second Example Code

The second tutorial will teach you how to get a continue movement.

#include <Servo.h>    //we are including the library
Servo myservo;       //we are creating an object for the class Servo
int angle = 0;       //that's the variable that we will use to change the angle 

void setup()
{
  myservo.attach(9); // we are using a Servo's class method. With it we are stating that the pin to which is attached the servo is the pin 9
  myservo.write(0);  //we initialize the servo's angle to 0
}
void loop()
{
  for(angle = 0; angle < 90; angle++)    //in that example the servo won't stop
  {                                      //it will always do something thanks to the for cycle
    myservo.write(angle);                //it will increase it's angle untill it is 90°
    delay(20);
  }
  for(angle = 90; angle>=1; angle--)     //then it decrease untill it is 0
  {
    myservo.write(angle);
    delay(20);
  }
}

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: