How to Make a Servo Move Then Hold Position and Then Move Again in Arduino
In this tutorial, you will learn how servo motors work and how to control them with Arduino. I take included wiring diagrams and several example codes! Servo motors are often used in robotics projects merely you can also find them in RC cars, planes, etc. They are very useful when you lot need precise position control and/or high torque.
In the first part of this article, we will expect at the inner workings of a servo and what type of command signal it uses. I also explain what the differences betwixt a standard and a continuous servo are. Next, I will testify you lot how to connect a servo motor to the Arduino.
With the first code example, yous tin command both the position equally well as the speed of the servo motor. Afterwards that, nosotros volition look into decision-making a servo with a potentiometer and how you lot can modify the lawmaking to control multiple servo motors at the aforementioned fourth dimension. Lastly, at the terminate of this article, y'all can observe the specifications and dimensions of some of the near popular servo motors on the market.
If yous would like to larn more about other types of motors, check out the manufactures below:
Recommended articles
- 28BYJ-48 Stepper Motor with ULN2003 Driver and Arduino Tutorial
- How to command a Stepper Motor with Arduino Motor Shield Rev3
- How to control a stepper motor with A4988 commuter and Arduino
- How to command a character I2C LCD with Arduino
If yous accept any questions, please leave a comment below.
Supplies
Hardware components
SG90 micro servo | × 1 | Amazon |
MG996R loftier-torque servo | × 1 | Amazon |
Arduino Uno Rev3 | × 1 | Amazon |
Jumper wires | × xv | Amazon |
Breadboard | × 1 | Amazon |
10 kΩ potentiometer (breadboard type) | × 1 | Amazon |
USB cable type A/B | × i | Amazon |
5V power supply (optional) | × 1 | Amazon |
Software
Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advert and linking to products on Amazon.com.
How does a servo motor piece of work?
A standard hobby servo typically consists of a modest electric motor, a potentiometer, control electronics, and a gearbox. The position of the output shaft is constantly measured by the internal potentiometer and compared with the target position set by the controller (due east.one thousand. the Arduino).
According to the error, the control electronics adjust the actual position of the output shaft and then that it matches the target position. This is known as a closed-loop control system.
![](https://www.makerguides.com/wp-content/uploads/2020/08/Schematic-of-an-RC-servo-motor.jpg)
The gearbox decreases the speed of the motor, which increases the torque at the output shaft. The maximum speed of the output shaft is usually around 60 RPM.
Servo control
Servo motors are controlled by sending a PWM (pulse-width modulation) betoken to the signal line of the servo. The width of the pulses determines the position of the output shaft. When yous send the servo a bespeak with a pulse width of one.5 milliseconds (ms), the servo will motion to the neutral position (90 degrees). The min (0 degrees) and max (180 degrees) position typically stand for to a pulse width of ane ms and 2 ms respectively. Note this tin vary slightly betwixt different types and brands of servo motors (e.g. 0.5 and 2.five ms). Many servos only rotate through about 170 degrees (or fifty-fifty only 90) simply the centre position is nigh ever at ane.5 ms.
For adjusting the min and max position in the lawmaking, see the section below.
![](https://www.makerguides.com/wp-content/uploads/2020/08/servo-control-2.png)
Servo motors generally expect a pulse every 20 milliseconds or 50 Hz just many RC servos work fine in a range of 40 to 200 Hz.
360-degree (continuous) vs 180-degree (standard) servo
About RC servos are from the 180-degree variety, which means that they can merely rotate in a range of 0 to 180 degrees. However, continuous rotation, also known equally 360-caste servo motors, are also available.
Continuous rotation servos react differently to the command bespeak than standard 180-degree servos. With a continuous rotation servo, y'all can non command the exact position of the output shaft, merely the speed and the direction. A 1 ms pulse will set the speed of the servo motor to full speed in one direction and a two ms pulse to full speed in the other. A value almost 1.five ms lets the motor cease.
If your servo behaves in an unexpected way, yous might be using a continuous servo instead of a standard 1.
How to connect a servo motor to the Arduino?
Wiring a servo motor is very easy considering you simply demand to connect iii wires: power, ground, and betoken. The power wire is typically red and needs to be connected to v V.
A micro servo similar the SG90 consumes effectually ten mA when it's idle and 100 – 250 mA when rotating, so y'all can ability it directly with the v V output of the Arduino. However, you need to exist conscientious when using multiple or larger servo motors. If your motor(s) consume more than 300 mA you should use an external power supply to avoid damaging the Arduino! See the schematic below for using external power supplies.
The ground wire is typically black or brown and should exist connected to the basis pivot of the Arduino. When using a separate power supply, connect the basis wire to both the Arduino and the power supply ground.
The indicate wire is typically yellow, orange, or white can be connected to whatsoever of the digital pins of the Arduino. In this case, I connected it to digital pin 9.
![](https://www.makerguides.com/wp-content/uploads/2020/08/servo-motor-with-arduino-uno-wiring-diagram-schematic-circuit-tutorial.png)
The connections are as well given in the table beneath.
Servo motor connections
Servo motor | Arduino |
---|---|
Ability (scarlet) | 5 V |
Ground (black or chocolate-brown) | GND |
Signal (xanthous, orange or white) | Pin 9 |
As I mentioned earlier, if you are using big or multiple servo motors you should use an external power supply. Simply connect the power supply as shown in the wiring diagram beneath. Brand sure to connect the GND pivot of the Arduino and the power supply together.
You lot tin can also use this setup if your servo motor requires a dissimilar voltage than the Arduino can provide east.g. 6 V or higher.
![servo-motor-with-arduino-uno-and-external-power-supply-wiring-diagram-schematic-circuit-tutorial](https://www.makerguides.com/wp-content/uploads/2020/08/servo-motor-with-arduino-uno-and-external-power-supply-wiring-diagram-schematic-circuit-tutorial.png)
Servo motor with external power supply connections
Servo motor | Connection |
---|---|
Power (reddish) | 5 5 power supply |
Basis (blackness or brown) | Power supply ground and Arduino GND |
Point (yellow, orange or white) | Pin ix Arduino |
Servo motor with Arduino example lawmaking
To command the servo motor nosotros will exist using the Servo.h library which comes pre-installed with the Arduino IDE. With the example code below, yous can control the exact position of the servo motor and it also includes lawmaking to sweep the servo arm back and forth automatically.
Yous can upload the instance lawmaking to your Arduino via the Arduino IDE. Side by side, I volition explicate how the code works.
You tin can copy the code by clicking on the push button in the top right corner of the code field.
/* Servo motor with Arduino case code. Position and sweep. More info: https://www.makerguides.com/ */ // Include the servo library: #include <Servo.h> // Create a new servo object: Servo myservo; // Ascertain the servo pivot: #define servoPin 9 // Create a variable to store the servo position: int angle = 0; void setup() { // Attach the Servo variable to a pin: myservo.attach(servoPin); } void loop() { // Tell the servo to get to a particular angle: myservo.write(90); delay(grand); myservo.write(180); filibuster(chiliad); myservo.write(0); filibuster(1000); // Sweep from 0 to 180 degrees: for (angle = 0; angle <= 180; bending += 1) { myservo.write(bending); delay(fifteen); } // And back from 180 to 0 degrees: for (angle = 180; angle >= 0; bending -= one) { myservo.write(bending); delay(xxx); } delay(1000); }
How the code works
The first step is to include the required Arduino library. You tin can also find this library nether Sketch > Include Library > Servo.
// Include the servo library: #include <Servo.h>
Next, you lot need to create a new object of the Servo class. In this case, I called the servo 'myservo' but y'all can use other names too. Note that yous will also have to modify the name of the servo in the rest of the code.
// Create a new servo object: Servo myservo;
After that, I divers to which Arduino pin the servo motor is connected.
// Define the servo pin: #define servoPin 9
The statement#ascertain
is used to requite a name to a constant value. The compiler will replace any references to this abiding with the defined value when the program is compiled. So everywhere you mentionservoPin
, the compiler will supersede it with the value ix when the program is compiled.
The variable bending
is used to store the current position of the servo in degrees.
// Create a variable to store the servo position: int angle = 0;
In the setup section of the code, nosotros link the servo object that we created to the pivot that volition control the servo. The attach()
function also has 2 optional parameters, which I discuss in the section beneath.
void setup() { // Adhere the Servo variable to a pin: myservo.attach(servoPin); }
Control bending/position:
In the first part of the loop, we simply tell the servo motor to movement to a particular angle with the role write()
. Note that yous need a delay between the commands to give the servo motor some time to movement to the gear up position.
// Tell the servo to become to a particular angle: myservo.write(90); delay(one thousand); myservo.write(180); filibuster(one thousand); myservo.write(0); delay(1000);
Control speed:
In the terminal part of the code, I used two for loops to sweep the servo motor back and along. This piece of code can also be useful if you want to control the speed of the servo motor. By changing the delay value at the end of the for loop, you lot tin can adjust the speed of the servo arm.
// Sweep from 0 to 180 degrees: for (bending = 0; bending <= 180; angle += ane) { myservo.write(angle); delay(15); } // And back from 180 to 0 degrees: for (angle = 180; angle >= 0; angle -= 1) { myservo.write(bending); delay(30); }
Why doesn't my servo plow a full 0 – 180 degrees?
As I discussed in the introduction, the bending of the output shaft of the servo motor is determined by the width of the electrical pulse that is applied to the control wire. Generally, a pulse width of about i ms (millisecond) corresponds to the minimum position, 2 ms to the maximum position, and 1.5 ms to 90° (neutral position). Withal, this can vary slightly between brands and even different servos of the same brand. This means that you lot volition have to conform the minimum and maximum values in the code to friction match the servo that yous are using.
The Arduino Servo library makes it very easy to tune the min and max angle of the servo motor by specifying 2 optional parameters in the attach()
office. In this function, the commencement parameter is the number of the pin that the servo is fastened to. The second parameter is the pulse width, in microseconds (μs), corresponding to the minimum (0-degree) angle of the servo motor. The tertiary parameter is the pulse width, in microseconds, corresponding to the maximum (180-degree) angle of the servo motor.
By default, the min and max pulse width is set to 544 and 2400 microseconds. These values work for well-nigh mutual servos, only sometimes y'all take to adjust the values slightly.
I recommend adjusting the min and max values in minor increments (10-20 microseconds) to avoid dissentious the servo. If the servo arm is hitting the concrete limits of the motor, increment the min value, and decrease the max value.
#define servoPin nine int min = 480; int max = 2500; Servo myservo; void setup() { myservo.adhere(servoPin, min, max); }
Command a servo motor with a potentiometer and Arduino
![servo-motor-with-arduino-uno-and-potentiometer-wiring-diagram-schematic-circuit](https://www.makerguides.com/wp-content/uploads/2020/08/servo-motor-with-arduino-uno-and-potentiometer-wiring-diagram-schematic-circuit-1024x427.png)
Controlling the position of a servo motor with a potentiometer is very easy and can be very useful if you want to adjust the motor position by mitt. As you tin can see in the wiring diagram in a higher place, the servo motor is wired in the same way as before. The only difference is that I used a breadboard to distribute the ability from the Arduino.
The potentiometer has 3 pins, connect the outside pins to 5 Five and GND. The center pin of the potentiometer is connected to the analog pin A0 of the Arduino.
Servo motor with potentiometer Arduino example lawmaking
The example code below lets you control a servo motor with a potentiometer.
You can re-create the lawmaking by clicking on the push in the top right corner of the code field.
/* Servo motor with potentiometer and Arduino case lawmaking. More info: https://www.makerguides.com/ */ #include <Servo.h> // include the required Arduino library #define servoPin 9 // Arduino pin for the servo #define potPin A0 // Arduino pin for the potentiometer int bending = 0; // variable to store the servo position in degrees int reading = 0; // variable to store the reading from the analog input Servo myservo; // create a new object of the servo class void setup() { myservo.attach(servoPin); } void loop() { reading = analogRead(potPin); // read the analog input angle = map(reading, 0, 1023, 0, 180); // map the input to a value between 0 and 180 degrees myservo.write(bending); // tell the servo to become to the set position delay(15); // await fifteen ms for the servo to reach the position }
Discover that earlier the setup and loop section of the code a new variable reading
is added and the potentiometer input pin is defined.
In the loop department of the lawmaking, we read the value from the analog pin A0 with the part analogRead()
.
reading = analogRead(potPin); // read the analog input
Arduino boards incorporate a 10-bit analog to digital converter (ADC), then this gives the states a value betwixt 0 and 1023 depending on the position of the potentiometer.
Considering the servo motor tin simply rotate betwixt 0 and 180 degrees, we need to scale the values downwards with the map() role. This part re-maps a number from one range to another.
angle = map(reading, 0, 1023, 0, 180); // map the input to a value between 0 and 180 degrees
Lastly, we write the angle to the servo motor:
myservo.write(angle); // tell the servo to go to the set position delay(15); // expect 15 ms for the servo to reach the position
Decision-making multiple servo motors
Controlling multiple servos is but every bit piece of cake as decision-making just one simply I ofttimes become questions virtually how to alter the code. Therefore, I accept added a simple case below.
![how-to-control-multiple-servo-motors-with-arduino-wiring-diagram-schematic-circuit-tutorial-1](https://www.makerguides.com/wp-content/uploads/2020/08/how-to-control-multiple-servo-motors-with-arduino-wiring-diagram-schematic-circuit-tutorial-1-1024x768.png)
Notation that y'all will have to use an external power supply to power the servos considering the Arduino can not provide plenty current to power all of the motors.
For this example, nosotros just use more Arduino pins for the additional servos. However, this means that you are limited to 12 servos when using an Arduino Uno, and you might not take enough pins left over for other components.
Another option is to apply 1 or multiple PCA9685 PWM/servo drivers. This driver allows you to command 16 servos with just 2 pins from the Arduino by using I2C. Adafruit also sells these in the form of an Arduino shield.
Because the setup of these servo drivers is a scrap more than difficult, I will encompass this in a separate tutorial.
Arduino with multiple servos example lawmaking
As y'all tin see in the instance below, yous only have to create more objects of the Servo grade with different names. Y'all can address each servo by using the right proper name in the setup and loop section of the code.
/* Arduino with multiple servos example code. More info: https://www.makerguides.com/ */ #include <Servo.h> Servo servo1; Servo servo2; Servo servo3; Servo servo4; Servo servo5; void setup() { servo1.adhere(ix); servo2.adhere(x); servo3.attach(11); servo4.attach(12); servo5.adhere(xiii); } void loop() { servo1.write(0); servo2.write(0); servo3.write(0); servo4.write(0); servo5.write(0); delay(2000); servo1.write(90); servo2.write(90); servo3.write(90); servo4.write(90); servo5.write(90); delay(1000); servo1.write(180); servo2.write(180); servo3.write(180); servo4.write(180); servo5.write(180); filibuster(1000); }
Servo motor specifications
Below you can find the specifications of some of the most popular servo motors on the market place. The original manufacturer of these servo motors is Tower Pro Pte Ltd. but similar models tin can exist purchased from many other suppliers likewise.
SG90 analog micro servo
Pinout
Dark-brown | GND |
Ruddy | VCC |
Yellow | Signal (PWM) |
Specifications
Operating voltage | 4.8 V |
Weight | ix g |
Stall torque | 1.8 kg/cm (four.8 V) |
Gear type | POM gear set up |
Operating speed | 0.12 sec/60° (4.8 V) |
Operating temperature | 0 – 55 °C |
Cost | Check price |
Dimensions
A | 34.5 mm |
B | 22.8 mm |
C | 26.7 mm |
D | 12.6 mm |
Due east | 32.five mm |
F | 16 mm |
MG90S digital micro servo
Pinout
Brown | GND |
Red | VCC |
Yellow | Signal (PWM) |
Specifications
Operating voltage | four.eight V |
Weight | 13.4 chiliad |
Stall torque | i.viii kg/cm (4.eight V), two.2 kg/cm (6.6 V) |
Gear type | 6061-T6 aluminum |
Operating speed | 0.ten sec/60° (4.viii V), 0.08 sec/60° (half dozen.0 5) |
Operating temperature | 0 – 55 °C |
Cost | Check price |
Dimensions
A | 32.5 mm |
B | 22.eight mm |
C | 28.4 mm |
D | 12.four mm |
Eastward | 32.1 mm |
F | 18.5 mm |
MG996R high torque digital servo
Pinout
Brown | GND |
Red | VCC |
Yellow | Signal (PWM) |
Specifications
Operating voltage | iv.8 – 6.half-dozen V |
Current draw at idle | 10 mA |
No-load operating current draw | 170 mA |
Stall current draw | 1400 mA |
Weight | 55 g |
Stall torque | nine.4 kg/cm (iv.8 5), 11 kg/cm (6.0 Five) |
Gear blazon | Metallic gear |
Operating speed | 0.19 sec/threescore° (4.viii 5), 0.xv sec/threescore° (6.0 V) |
Operating temperature | 0 – 55 °C |
Toll | Check price |
Dimensions
A | 42.7 mm |
B | 40.9 mm |
C | 37 mm |
D | xx mm |
Eastward | 54 mm |
F | 26.eight mm |
Conclusion
In this tutorial, I have shown you lot how to use servo motors with Arduino. We looked at the basics of controlling the position and speed of servo motors, how to control a servo motor with a potentiometer, and how to control multiple servo motors at the same time.
I hope you found this article useful and informative. If you did, please share it with a friend who too likes electronics and making things!
I would love to know what project you program on building or have already built with servo motors and the Arduino. If you have whatever questions, suggestions, or if you think that things are missing in this tutorial, please leave a annotate below.
Note that comments are held for moderation to foreclose spam.
![Creative Commons License](https://www.makerguides.com/wp-content/uploads/2019/01/Creative-Commons-License.png)
Source: https://www.makerguides.com/servo-arduino-tutorial/
Post a Comment for "How to Make a Servo Move Then Hold Position and Then Move Again in Arduino"