Home » Electronica » Arduino » Servo

Servo

Learn how to use a Servo. DYOR robots use SG90 or TS90M servos to control movement of their grips or legs. Here we explain the working principle, connection diagrams and programming examples with Facilino and Arduino and ESP processors.

How does a Servo work?

A servo is a direct continuous (DC) motor with an electronic board that allows you to control the position of the rotating axis with a pulse-position modulated signal (PPM). Most of the servos use a standard signal of 20ms period, whose minimum position can be set with a pulse width of 0.5ms, its maximum position with a pulse width of 2.5ms and its intermediate position with a pulse width of 1.5ms.

PPM specifications come from radio control (RC) systems, for this reason, electronic speed control (ESC) board used for controlling the speed of brushless motors (typically used in drones) are also controlled with the same signal (with some additional protocols). Actually, we can see PPM as a particular case of pulse width modulation (PWM) with a frequency of 50Hz to control the position. The position of the servo motor is done in closed-loop, meaning that it measures the actual angle of the shaft with a rotational potentiometer and depending on the difference (error) of this angle and the actual reference, a current is injected onto the motor (to one direction or the opposite). There’s a mechanical limitation which prevents the motor to rotate further, typically the maximum angle range is 180º. There exists models with different ranges, so it is convenient to google for specification on each case.

Servo connection

Servos use a 3 pin connector. The cable colours and pinout depends on the manufacturer, although almost all are based on Hitachi connector and therefore they are compatible. This is the kind of connectors used also in many expansion boards, and particularly, Arduino Nano expansion board in DYOR robot. Fortunately, if we reverse the polarity of the connector, neither the Servo or Arduino will be damaged.

Servo control

In order to connect a Servo to Arduino expansion board, we can connected to any pin, while if we use ESP board it is preferable that they are connected to PWM pins.

Here you can download Fritzing diagram:

A servo can be controlled by indicating its position in degrees, a value between 0º and 180º. The position of 90º is the intermediate position. In this example, we are generating and positioning the servo, connected to pin 3, on alternate positions (45º and 135º) every 2 sec.

It is important to remark that, since the signal period of a servo is 20ms, the minimum time between two consecutive Servo instructions should be greater than 20ms. Therefore, we should always guarantee that this time has elapsed by introducing delays or controlling the periodicity of the code, otherwise it won’t work. From a mechanical point of view, we should also consider that servos have some “slow” dynamics, which means that we should not require to apply commands which require rotational speed over their specifications (i.e.: between 500º/sec and 600º/sec).

Here you can download Facilino code for the previous example: