Arduino projects

servo drives in Arduino projects

Arduino SG90, MG995, MG996 servos: wiring diagram and control

Arduino SG90, MG995, MG996 servos: wiring diagram and control

In this article, we will talk about servo drives in Arduino projects. It is thanks to servo motors that ordinary electronic projects become robotic. Connecting a servo to an Arduino project allows you to respond to sensor signals with some precise movement, for example, open a door or turn sensors in the right direction. The article discusses issues of servo control, possible servo connection schemes to Arduino, as well as sketch examples.

Servo for Arduino

A servo motor is a type of drive that can precisely control the parameters of movement. In other words, it is a motor that can rotate its shaft at a certain angle or maintain continuous rotation with a precise period.

MG995 ServoThe servo drive operation scheme is based on the use of feedback (a closed-loop circuit in which the input and output signals are not matched). Any type of mechanical drive that includes a sensor and a control unit that automatically maintains all the parameters set on the sensor can act as a servo drive. The servo drive design consists of a motor, a positioning sensor, and a control system. The main task of such devices is implementation in the field of servo mechanisms. Servo drives are also often used in such areas as material processing, production of transport equipment, wood processing, production of metal sheets, production of building materials, and others.

In Arduino robotics projects, servos are often used for simple mechanical actions:

  • Rotate the rangefinder or other sensors to a certain angle to measure the distance in a narrow field of view of the robot.
  • Take a small step with your foot, move your limb or head.
  • To create robotic manipulators.
  • To implement the steering mechanism.
  • Open or close a door, damper or other object.

Of course, the scope of application of servos in real projects is much wider, but the examples given are the most popular schemes.

Servo drive diagram and types

The principle of operation of a servo drive is based on feedback from one or more system signals. The output value is fed to the input, where its value is compared with the setting action and the necessary actions are performed – for example, the engine is turned off. The simplest implementation option is a variable resistor, which is controlled by the shaft – when the resistor parameters change, the parameters of the current supplying the engine change.

In real servo drives, the control mechanism is much more complex and uses built-in microcircuit controllers. Depending on the type of feedback mechanism used, there are analog and digital servo drives. The former use something similar to a potentiometer, the latter – controllers.

Servo drive deviceThe entire servo control circuit is located inside the housing, control signals and power are supplied, as a rule, via three wires: ground, supply voltage and control signal.

Continuous rotation servo 360, 180 and 270 degrees

There are two main types of servo motors – with continuous rotation and with a fixed angle (usually 180 or 270 degrees). The difference between a limited rotation servo is in the mechanical elements of the design, which can block the movement of the shaft outside the angles specified by the parameters. Having reached an angle of 180, the shaft will affect the limiter, and the limiter will give a command to turn off the motor. Continuous rotation servo motors do not have such limiters.

Servo Gear Materials

Most servos use a gear as the link between the shaft and the external elements, so the material it is made of is very important. The most affordable options are metal or plastic gears. More expensive models may feature carbon or even titanium elements.

Arduino SG90, MG995, MG996 servos: wiring diagram and control Arduino SG90, MG995, MG996 servos: wiring diagram and control

Plastic options are naturally cheaper, easier to manufacture and are often used in inexpensive servo models. For educational projects, when the servo makes several movements, this is not a problem. But in serious projects, the use of plastic is impossible, due to the very rapid wear of such gears under load.

MG995 ServoMetal gears are more reliable, but this certainly affects both the price and the weight of the model. Thrifty manufacturers can make some parts plastic and some metal, this should also be kept in mind. And, naturally, in the cheapest models, even the presence of a metal gear is not a guarantee of quality.

Titanium or carbon gears are the most preferable option if you are not limited by budget. Lightweight and reliable, such servos are actively used to create model cars, drones and airplanes.

Advantages of servo motors

The wide use of servo drives is due to the fact that they have stable operation, high resistance to interference, small dimensions and a wide range of speed control. Important features of servo drives are the ability to increase power and provide feedback information. And it follows that in the forward direction, the circuit is a transmitter of energy, and in the reverse direction – a transmitter of information, which is used to improve control accuracy.

Differences between servo and regular motor

By turning on or off a regular electric motor, we can generate a rotational motion and make wheels or other objects attached to the shaft move. This motion will be continuous, but in order to understand at what angle the shaft has turned or how many revolutions it has made, it will be necessary to install additional external elements: encoders. The servo drive already contains everything necessary to obtain information about the current rotation parameters and can turn off automatically when the shaft has turned at the required angle.

Differences between servo and stepper motor

stepper motorAn important difference between a servo motor and a stepper motor is the ability to work with high acceleration and variable load. Also, servo motors have higher power. Stepper motors do not have feedback, so the effect of step loss can be observed, in servo motors step loss is excluded – all violations will be recorded and corrected. With all these obvious advantages, servo motors are more expensive devices than stepper motors, have a more complex connection and control system and require more qualified maintenance. It is important to note that stepper motors and servo drives are not direct competitors – each of these devices occupies its own specific area of ​​​​application.

Where to buy popular servos SG90, MG995, MG996

The most affordable version of the SG90 1.6KG servo SG90 and MG90S servos for Arduino for less than 70 rubles
Another version of SG90 Pro 9g servo from a trusted supplier on Ali Servo SG90 from reliable supplier RobotDyn
Servo tester Several options for servo testers
Protected servo drive with 15 kg torque Servo JX DC5821LV 21KG Full waterproof Core mental gear 1/8 1/10 RC car Scaler Buggy Crawler TRAXXAS RC4WD TRX-4 SCX10 D90
Servo MG996R MG996 Servo Metal Gear for Futaba JR Servo 13KG 15KG Servos Digital MG995 MG996 MG996R Servo Metal Gear

Servo control

Servo controlThe decisive role in the control of servo drives is played by the control signal, which is a pulse of constant frequency and variable width. The pulse length is one of the most important parameters that determines the position of the servo drive. This length can be set in the program manually by the selection method through the angle or using library commands. For each brand of device, the length may be different.

When the signal gets to the control circuit, the generator sends its pulse, the duration of which is determined by the potentiometer. In another part of the circuit, the duration of the sent signal and the signal from the generator are compared. If these signals are different in duration, the electric motor is switched on, the direction of rotation of which is determined by which of the pulses is shorter. If the pulses are equal in length, the motor stops.

The standard frequency at which pulses are given is 50 Hz, i.e. 1 pulse per 20 milliseconds. With these values, the duration is 1520 microseconds, and the servo takes the middle position. Changing the pulse length causes the servo to rotate – when the duration increases, the rotation is clockwise, when it decreases, it is counterclockwise. There are duration limits – in the Arduino Servo library, the pulse value for 0° is set to 544 μs (lower limit), for 180° – 2400 μs (upper limit).

It is important to consider that the settings on a specific device may differ slightly from the generally accepted values. For some devices, the average position and pulse width may be 760 µs. All accepted values ​​may also differ slightly due to the error that may be allowed during device production.

The drive control method is often mistakenly called PWM/PWM, but this is not entirely correct. Control directly depends on the pulse length, the frequency of their occurrence is not so important. Correct operation will be ensured both at 40 Hz and at 60 Hz, only a strong decrease or increase in frequency will contribute. With a sharp drop, the servo drive will start to work jerkily, if the frequency is increased above 100 Hz, the device may overheat. Therefore, it is more correct to call it PDM.

By the internal interface, we can distinguish analog and digital servo drives. There are no external differences – all the differences are only in the internal electronics. The analog servo drive contains a special microcircuit inside, the digital one – a microprocessor that receives and analyzes pulses.

Arduino SG90, MG995, MG996 servos: wiring diagram and control

When receiving a signal, the analog servo drive decides whether to change the position or not and, if necessary, sends a signal to the motor at a frequency of 50 Hz. During the reaction time (20 ms), external influences may occur that will change the position of the servo drive, and the device will not have time to react. The digital servo drive uses a processor that sends and processes signals at a higher frequency – from 200 Hz, so it can respond to external influences faster, develop the required speed and torque faster. Consequently, the digital servo drive will better maintain the set position. At the same time, more electricity is required to operate a digital servo drive, which increases their cost. The complexity of their production also makes a large contribution to the price. The high cost is the only drawback of digital servo drives; in technical terms, they are much better than analog devices.

Connecting a servo motor to an arduino

The servo has three contacts, which are painted in different colors. The brown wire leads to the ground, the red one to the +5V power supply, the orange or yellow wire is the signal wire. The device is connected to the Arduino via a breadboard as shown in the figure. The orange wire (signal) is connected to the digital pin, the black and red ones to the ground and power supply, respectively. To control the servo motor, you do not need to connect to the shim pins – we have already described the principle of servo control earlier.

It is not recommended to connect powerful servos directly to the board, because they create a current for the Arduino power supply that is incompatible with life – you’ll be lucky if the protection works. Most often, the symptoms of overload and improper power supply of the servo are “twitching” of the servo, an unpleasant sound and rebooting of the board. For power supply, it is better to use external sources, be sure to connect the grounds of the two circuits.

Sketch for controlling a servo in Arduino

Controlling a servo directly by changing the pulse duration in a sketch is a rather non-trivial task, but fortunately we have an excellent Servo library built into the Arduino development environment. We will consider all the nuances of programming and working with servos in a separate article. Here we will give the simplest example of using Servo.

The algorithm of operation is simple:

  • First we include Servo.h
  • Create an object of the Servo class
  • In the setup block we indicate to which pin the servo is connected
  • We use the methods of the object in the usual way for C++. The most popular is the write method, to which we give an integer value in degrees (for a 360 servo, these values ​​will be interpreted differently).

Example of a simple sketch for working with a servo drive

An example project where we first set the servo motor to zero angle and then rotate it 90 degrees.

#include <Servo.h>

Servo servo; // Create an object
void setup() {
servo.attach(9); // Tell the Servo class object that the servo is attached to pin 9
servo1.write(0); // Set the initial position
}

void loop() {
servo.write(90); // Rotate the servo 90 degrees
delay(1000);
servo.write(1800);
delay(100);
servo.write(90);
delay(1000);
servo.write(0);
delay(1000);
}

Sketch for two servos

And in this example we work with two servos at once:

#include <Servo.h>

Servo servo1; // First servo
Servo servo2; // Second servo

void setup() {
servo1.attach(9); // Tell the Servo class object that the servo is attached to pin 9
servo2.attach(10); // And this servo is attached to pin 10

}

void loop() {
// We set the positions
servo1.write(0);
servo2.write(180);
delay(20);
// Change positions
servo2.write(0);
servo1.write(180);
}

Controlling a servo with a potentiometer

In this example, we rotate the servo based on the value received from the potentiometer. We read the value and convert it to an angle using the map function:

//Fragment of a standard example of using the Servo library
void loop() {
val = analogRead(A0); // Read the value from the pin to which the potentiometer is connected
val = map(val, 0, 1023, 0, 180); // Convert a number in the range from 0 to 1023 to a new range – from 0 to 180.
servo.write(val);
delay(15);
}
Servo drive SG90

 

SG-90 Specifications and Connection

If you are going to buy the cheapest and simplest servo drive, then the SG 90 will be the best option. This servo is most often used to control small light mechanisms with a rotation angle from 0° to 180°.

Technical characteristics of SG90:

  • Command processing speed 0.12 sec/60 degrees;
  • Power supply 4.8V;
  • Operating temperatures from -30C to 60C;
  • Dimensions 3.2 x 1.2 x 3 cm;
  • Weight 9 g.

Description SG90

The colors of the wires are standard. The servo drive is inexpensive, it does not provide precise settings for the initial and final positions. In order to avoid unnecessary overloads and characteristic crackling in the 0 and 180 degree positions, it is better to set the extreme points at 10° and 170°. When operating the device, it is important to monitor the supply voltage. If this indicator is greatly overestimated, the mechanical elements of the gear mechanisms may be damaged.

MG995 and MG996 tower pro servos

The MG995 servo is the second most popular servo model, most often connected to Arduino projects. These are relatively inexpensive servos, with much better performance than the SG90.

MG995 Specifications

The output shaft of the MG995 rotates 120 degrees (60 in each direction), although many sellers indicate 180 degrees. The device is made in a plastic case.

  • Weight 55 g;
  • Torque 8.5 kg x cm;
  • Speed ​​0.2s/60 degrees (at 4.8V);
  • Operating power supply 4.8 – 7.2V;
  • Operating temperatures – from 0C to -55C.

Description MG995

Connection to the Arduino also occurs via three wires. In principle, for amateur projects it is allowed to connect the MG995 directly to the Arduino, but the motor current will always create a dangerous load for the board inputs, so it is still recommended to power the servo separately, not forgetting to connect the ground of both power circuits. Another option that simplifies life is to use ready-made servo controllers and shields, a review of which we will prepare in a separate article.

MG996R is similar to MG995 in its characteristics, only it is made in a metal case.

Converting a servo drive into a continuous rotation servo

As described above, the servo is controlled by variable-width pulses that set the rotation angle. The current position is read from the potentiometer. If the shaft and the potentiometer are disconnected, the servo motor will take the position of the potentiometer slider as the midpoint. All these actions will result in the feedback being removed. This allows you to control the speed and direction of rotation via the signal wire, and create a continuous rotation servo. It is important to note that a continuous rotation servo cannot rotate at a certain angle and make a strictly specified number of revolutions.

To perform the above actions, you will have to disassemble the device and make changes to the design.

In the Arduino IDE, you need to create a small sketch that will put the rocker in the middle position.

#include <Servo.h>

Servo myservo;

void setup(){
myservo.attach(9);
myservo.write(90);
}

void loop(){
}
After this, the device needs to be connected to Arduino. When connected, the servo will start rotating. It is necessary to achieve its complete stop by adjusting the resistor. After the rotation stops, you need to find the shaft, pull out the flexible element from it and install it back.

This method has several disadvantages – the resistor setting to a complete stop is unstable, with the slightest impact/heating/cooling the adjusted zero point can be lost. Therefore, it is better to use the method of replacing the potentiometer with a trimmer. To do this, you need to pull out the potentiometer and replace it with a trimmer with the same resistance. The zero point must be adjusted with a calibration sketch.

Any of the methods for converting a servo drive into a continuous rotation servo has its drawbacks. First, it is difficult to adjust the zero point, any movement can knock it down. Second, the control range is small – with a small change in pulse width, the speed can change significantly. The range can be expanded programmatically in Arduino.

Conclusion

Servos play a very important role in many Arduino projects, from robotics to smart home systems. Anything related to movement traditionally requires special knowledge, and creating a fully functional actuator is not an easy task. But with the help of servo motors, you can simplify the task in many cases, which is why servos are constantly used even in entry-level projects.

In this article, we tried to cover various aspects of using servos in Arduino projects: from connecting to writing sketches. By choosing the simplest servo model (for example, sg 90), you can easily repeat the examples given and create your first projects in which something moves and changes. We hope this article will help you with this.

Back to top button