Servo: how to use the servo motor with Arduino

servo, servo motor

If you want to use a servo motor, or servo , with Arduino , this article will teach you everything you need to know to get started. We've already covered the basics of using electric motors , stepper motors , and other concepts necessary to understand how these devices work, such as the article on PWM , in other articles.

Now, you can add another new electronic component to the list of analyzed devices that you can integrate into your DIY projects to add new functionalities.

What is a servo?

servo

A servomotor , or simply servo, is an electronic motor similar to conventional DC motors, but with some unique features. In this case, it has the ability to maintain a specified position, something that electric motors cannot do.

On the other hand, the servo can also precisely control the rotation speed, thanks to a series of internal gears and a system that allows for much better control than could be done in other types of motors.

These features make it especially interesting for robotics applications , or for other devices where movement and position control is needed, such as a printer or a remote-controlled car. In this type of radio-controlled car, there is a conventional motor to propel the car and a servo for steering, allowing for precise control of the turning.

Difference between stepper motor and servo motor

Nema 17

If you're wondering about the difference between a servo motor and a stepper motor , the truth is they can be confused, since stepper motors can also control rotation quite precisely, and their applications are very similar to servo motors. However, there are some differences.

Servomotors typically use rare-earth magnets , while stepper motors use cheaper, conventional magnets. Therefore, a servo can achieve higher torque output while remaining compact. Consequently, the turning force will be very high.

Technical characteristics

Whenever you buy a servo, you should consult its technical specifications or datasheet. This way, you'll be sure of its technical characteristics , but also of the limits you can subject it to, such as voltage, current, maximum load, torque, etc. Remember that each model can be quite different.

For example, if you analyze one of the most popular ones, the Micro Servo 9G SG90 from the well-known firm Tower Pro , then you will find some very peculiar characteristics, although the programming and connection of the models are more or less the same and everything said here applies to any of them.

In the case of this model, it is a high quality motor, with a turning angle that allows a sweep between -90 and 90º, that is to say, a total turn of 180º. The resolution that you can achieve is very high, so you will be able to advance very little by little. For example, with the PWM signal limitations of Arduino UNO, you could even get an advance from grade to grade.

Similarly, the PWM signal will also impose another limit: the number of times the servo can change position per unit of time. For example, since the pulses are between 1 and 2 ms and have periods of 20 ms (50 Hz), the servo will only be able to move once every 20 ms.

Furthermore, it will weigh just 9 grams and, despite that weight and compact size, it can develop a torque of 1.8 kg/cm² at 4.8v. This is thanks to its POM gear set.

Finally, you already know that, depending on what you want to achieve, you'll need to choose one model or another, so that it has the necessary characteristics for your project . In other words, it's not the same to want a motor to move load X as it is to want one for XX…

Where to buy a servo

servo-motor

If you want to start using this type of servo motor, you can find it inexpensively in many specialized stores, and you can also get it online at Amazon. For example, here are some recommended products that might interest you:

They all have a pretty good turning radius, but they differ mainly in the torque each one can handle. I've included three different models . The first, and cheapest, may be sufficient for most applications. But if you need something with more power for other uses, there are the 25 and 35 models, which are quite remarkable…

Integration with Arduino

arduino servo

As you can see in the image above, the servo connects to the Arduino very easily. It only has three wires, which you can connect as follows:

  • Red with 5V
  • Black with GND
  • Yellow with an Arduino PWM pin, in this case with -9.

To program a sketch to start using these types of motors, you have several options. But first, you need to add the Arduino IDE library for handling these servo motors:

  1. Open Arduino IDE.
  2. Go to Program.
  3. Then Include Library.
  4. Servo

As for the sketch code , it could be as simple as this: the servo will go through its positions, stopping at 0º, 90º, and 180º:

//Incluir la biblioteca del servo
#include <Servo.h>
 
//Declarar la variable para el servo
Servo servoMotor;
 
void setup() {
  // Iniciar el monitor serie
  Serial.begin(9600);
 
  // Iniciar el servo para que use el pin 9 al que conectamos
  servoMotor.attach(9);
}
 
void loop() {
  
  // Desplazar a la posición 0º
  servoMotor.write(0);
  // Esperar 1 segundo
  delay(1000);
  
  // Desplazar a la posición 90º
  servoMotor.write(90);
  // Esperar 1 segundo
  delay(1000);
  
  // Desplazamos a la posición 180º
  servoMotor.write(180);
  // Esperar 1 segundo
  delay(1000);
}

Now, if you want to move it degree by degree , then it would be like this:

// Incluir la biblioteca servo
#include <Servo.h>
 
// Declarar la variable para el servo
Servo servoMotor;
 
void setup() {
  // Iniciar la velocidad de serie
  Serial.begin(9600);
 
  // Poner el servo en el pin 9
  servoMotor.attach(9);
 
  // Iniciar el servo en 0º
  servoMotor.write(0);
}
 
void loop() {
 
  // Los bucles serán positivos o negativos, en función el sentido del giro
  // Positivo
  for (int i = 0; i <= 180; i++)
  {
    // Desplazar ángulo correspondiente
    servoMotor.write(i);
    // Pausa de 25 ms
    delay(25);
  }
 
  // Negativo
  for (int i = 179; i > 0; i--)
  {
    // Desplazar el ángulo correspondiente
    servoMotor.write(i);
    // Pausa e 25 ms
    delay(25);
  }
}


Add as preferred source in Google