How to connect a servo to Arduino

Servo drives are the basis for ham enthusiasts who work with Arduino. They are used everywhere: automatic door opening, robot movement, elevator crane, and much more. This article describes which servo drive for Arduino to choose, how to connect it and how to control it.

About servos

Types of Servo Drives




This is a kind of motor, but it differs in that it can be set to a certain angle of rotation of the shaft using program code. They are very different: cheap and weak, expensive and accurate. Do not chase the most expensive and best servo, as it is likely that your project is suitable and cheap. On the market you can find a fairly large selection.

Servo SG92R

Microserver SG92R Connection




The most famous cheap servo is the Tower Pro SG92R. This servo drive for the Arduino can be found in almost all of the initial amateur radio sets. This is due to the fact that its price is only some one hundred rubles, and for bulk purchases (for example, from ten pieces), it even comes out cheaper. SG92R weighs only 9 grams, can take on a shaft of 1.6 kg. This is written only in the specifications in online stores.





Microservice SG92R




In fact, these servos are weak and it is impractical to use them, for example, to open a large door, since they are completely plastic and often break. It is better to spend more money, buy a more powerful one, for example, MG995. But for the study of small projects, the SG92R servo fits perfectly.

Connection

Servo connection to Arduino




Connecting a servo to the Arduino does not take much time. For this, only three contacts are used: earth, power, logic. Servo works with "Arduino" using digital contacts. This is the Digital strip on the board. Unfortunately, the servo itself does not indicate which wires are responsible for what.

  1. Red is responsible for nutrition. By the way, the servo can work from 5 and 6V. Of course, from 6V it works faster.
  2. Black (sometimes even brown) wire is earth. Connect it to GND on the Arduino board.
  3. Yellow (in some cases it is white) is responsible for the logic. Connect it to any PWM (pulse width modulation) pin. For example, the ninth pin on the Arduino board.

That's all. In such a simple way, you can connect the servo to the Arduino. By the way, they all connect and work with Arduino the same way. You just need to figure out how to manage them.





Control

To control the servo with Arduino, the Servo library is used. It already comes with the Arduino IDE and does not need to be downloaded additionally. Working with the library is easy enough. The following is an example code for rotating a servo shaft.

#include <Servo.h> Servo myservo; void setup() { //       myservo.attach(9); } void loop() { //   0° myservo.write(0); delay(2000); //   90° myservo.write(90); delay(2000); //   180° myservo.write(180); delay(2000); }
      
      



The code explanation is as follows. The first step is to connect the library to work with the servo. After that, a variable of type Servo is created. Names can be given completely different - everything is limited only by your imagination. Then you should indicate which contact your servo drive in Arduino is connected to. Then you must set the rotation angle to zero degrees, and then set the wait for two seconds. This is necessary in order for the servo drive to rotate the shaft and work more stable.

After that, you should put the angle at ninety and one hundred eighty degrees. Since the Loop function is a loop, after compilation you will get that your servo will constantly rotate its shaft. This is all when connecting a single servo to the Arduino. The fact is that although it is small, it takes 0.4 mA when activating the rotation of the shaft, which is quite a lot. And when connecting several servos, a certain surge in electricity will occur, which can lead to unstable operation of sensors that are connected to the Arduino.

This problem is solved as follows: you need to take some other 5V power supply - the converted phone charger is perfect. It turns out that the servos are powered by a separate power source, and they only turn to Arduino for signals. Some people think that even one servo drive should not be connected directly to the Arduino.

Servo control with Arduino




Project example

Below is a project with a servo on "Arduino". Imagine a situation when you want to create automatic opening of window leaves in a greenhouse when a certain temperature is reached. For this, a DHT-11 sensor and an SG92R servo drive are used. The program code is as follows:

 #include "DHT.h" #include <Servo.h> #define DHTPIN 2 //  ,     Servo myservo; //  Servo //   DHT dht(DHTPIN, DHT11); void setup() { Serial.begin(9600); myservo.attach(9); dht.begin(); } void loop() { //  2    delay(2000); //   float t = dht.readTemperature(); if (t>30) { myservo.write(90); } }
      
      



For starters, libraries for working with the DHT-11 sensor and a servo drive are connected. Following are the variables and which contacts they are connected to. After that, every two seconds, the sensor detects the temperature, and if it is above thirty degrees, the servo is triggered, thereby opening the window. This is an example of his work with the Arduino and the DHT-11 sensor.

Pros and Cons of SG92R

The SG92R is a pretty good servo, and it does its job perfectly. What are the advantages and disadvantages of it?

Among the advantages are the following:

  1. Availability. This servo is sold at a very low price, which allows absolutely everyone to purchase it and use it for their projects.
  2. Sizes. Due to its small size, it can be used in the construction of some small projects.

Among the shortcomings, the most significant are the following:

  1. Weak. Even small loads are enough, and it will break. Of course, this is due to its low price, but still it’s worth overpaying only twice as much and you can already buy a more powerful servo drive, which is made of metal and is able to work with heavy loads.
  2. Noisy. You just need to turn it on, it starts to make sounds. It’s not that they are strong, but the eternal “buzzing” is a hindrance.

The number of pluses and minuses is the same. Be that as it may, the SG92R is an excellent servo, at least for the study and concept of their operation.




All Articles