Introduction: Arduino Servo Motors
Servo motors are great devices that can turn to a specified position.
Usually, they have a servo arm that can turn 180 degrees. Using the Arduino, we can tell a servo to go to a specified position and it will go there. As simple as that!
Servo motors were first used in the Remote Control (RC) world, usually to control the steering of RC cars or the flaps on a RC plane. With time, they found their uses in robotics, automation, and of course, the Arduino world.
Here we will see how to connect a servo motor and then how to turn it to different positions.
The first motor I ever connected to an Arduino, seven years ago, was a Servo motor. Nostalgic moment over, back to work!
We will need the following things:
- An Arduino board connected to a computer via USB
- A servo motor
- Jumper wires
There are few big names in the servo motor world. Hitec and Futaba are the leading RC servo manufacturers. Good places to buy them are Servocity, Sparkfun, and Hobbyking.
This instructable and many more can be found in my Arduino Development Cookbook available here. :D
Step 1: How to Connect Them
A servo motor has everything built in: a motor, a feedback circuit, and most important, a motor driver. It just needs one power line, one ground, and one control pin.
Following are the steps to connect a servo motor to the Arduino:
- The servo motor has a female connector with three pins. The darkest or even black one is usually the ground. Connect this to the Arduino GND.
- Connect the power cable that in all standards should be red to 5V on the Arduino.
- Connect the remaining line on the servo connector to a digital pin on the Arduino.
Check the image for a view of the servo connected to the Arduino.
Step 2: Code
The following code will turn a servo motor to 0 degrees, wait 1 second, then turn it to 90, wait one more second, turn it to 180, and then go back.
// Include the Servo library
#include <Servo.h> // Declare the Servo pin int servoPin = 3; // Create a servo object Servo Servo1; void setup() { // We need to attach the servo to the used pin number Servo1.attach(servoPin); } void loop(){ // Make servo go to 0 degrees Servo1.write(0); delay(1000); // Make servo go to 90 degrees Servo1.write(90); delay(1000); // Make servo go to 180 degrees Servo1.write(180); delay(1000); }
If the servo motor is connected on another digital pin, simply change the value of servoPin to the value of the digital pin that has been used.
Attachments
Step 3: How It Works
Servos are clever devices. Using just one input pin, they receive the position from the Arduino and they go there. Internally, they have a motor driver and a feedback circuit that makes sure that the servo arm reaches the desired position. But what kind of signal do they receive on the input pin?
It is a square wave similar to PWM. Each cycle in the signal lasts for 20 milliseconds and for most of the time, the value is LOW. At the beginning of each cycle, the signal is HIGH for a time between 1 and 2 milliseconds. At 1 millisecond it represents 0 degrees and at 2 milliseconds it represents 180 degrees. In between, it represents the value from 0–180. This is a very good and reliable method. The graphic makes it a little easier to understand.
Remember that using the Servo library automatically disables PWM functionality on PWM pins 9 and 10 on the Arduino UNO and similar boards.
Code breakdown
The code simply declares the servo object and then initializes the servo by using the servo.attach() function. We shouldn't forget to include the servo library. In the loop(), we set the servo to 0 degrees, wait, then set it to 90, and later to 180 degrees.
Step 4: More Things About Servos
Controlling servos is easy, and here are a few more tricks we can use:
Controlling the exact pulse time
Arduino has a built-in function servo.write(degrees) that simplifies the control of servos. However, not all servos respect the same timings for all positions. Usually, 1 millisecond means 0 degrees, 1.5 milliseconds mean 90 degrees, and, of course, 2 milliseconds mean 180 degrees. Some servos have smaller or larger ranges.
For better control, we can use the servo.writeMicroseconds(us) function, which takes the exact number of microseconds as a parameter. Remember, 1 millisecond equals 1,000 microseconds.
More servos
In order to use more than one servo, we need to declare multiple servo objects, attach different pins to each one, and address each servo individually. First, we need to declare the servo objects—as many as we need:
// Create servo objects Servo Servo1, Servo2, Servo3;
Then we need to attach each object to one servo motor. Remember, every servo motor uses an individual pin:
Servo1.attach(servoPin1); Servo2.attach(servoPin2); Servo3.attach(servoPin3);
In the end, we just have to address each servo object individually:
Servo1.write(0); // Set Servo 1 to 0 degrees Servo2.write(90); // Set Servo 2 to 90 degrees
Connection-wise, the grounds from the servos go to GND on the Arduino, the servo power to 5V or VIN (depending on the power input), and in the end, each signal line has to be connected to a different digital pin. Contrary to popular belief, servos don't need to be controlled by PWM pins—any digital pin will work.
Continuous rotation servos
There is a special breed of servos labelled as continuous rotation servos. While a normal servo goes to a specific position depending on the input signal, a continuous rotation servo either rotates clockwise or counter-clockwise at a speed proportional to the signal. For example, the Servo1.write(0) function will make the servomotor spin counter-clockwise at full speed. The Servo1.write(90) function will stop the motor and Servo1.write(180) will turn the motor clockwise at full speed.
There are multiple uses for such servos; however, they are really slow. If you are building a microwave and need a motor to turn the food, this is your choice. But be careful, microwaves are dangerous!
Step 5: Check Out More
More topics regarding motors such as brushless, transistor drivers ormotor speed control can be found in my Arduino Development Cookbook available here. :D
41 Comments
6 years ago
Can I use any of the analog pins (A0-A5) to connect a servo?
Reply 5 months ago
Yes, it will work if you do that, you can try it.
6 years ago
can i control the speed of the servo if yes how ??
Reply 5 months ago
If your servo controller has a speed control feature, you can set the speed at which you want the servo to move, and when you send a new position command, it automatically calculates and sends all the intermediate pulses. I hope you understand now.
Reply 6 years ago
In his library he writes a degree to the servo and it moves there. Set up a loop that increases the degree by say... 1 every time it loops and put a delay in the loop. When you get to the position you want get out of the loop. That will get you to move in degrees per second (you can also use the millis instruction so you can do other things and not actually stop the microcontroller. The millis instruction is more complex but basically you are taking a free running clock time with millis and comparing it to the last time you got the millis instruction. This allows the controller to continue executing main and does not just stop and sit on a delay not operating the rest of the code... are you confused yet?)
Question 1 year ago
I am making a and I have no pin left for gnd pin for servo motor...what to do???
Answer 5 months ago
You can make use of a bread board, you will connect your bread board to your arduino so you can connect any of your pins to your bread broad.
Answer 1 year ago
Just connect all the servos to the same GND pin using any kind of connection. Internally the GND is the same for all components in a DC system, only the VCC is different for each component as it defines the amplitude of the voltage.
Question 8 months ago
I’m having trouble connecting more than 2 servos to one arduino board, it needs more power pins to work….is it even possible to connect more than 2 servos?
Answer 6 months ago
Connect the servos NOT to the ardunio board for the power, but an external power source, say a 5 volt USB or something. the Ardunio board isn't really gonna be powerful enough to supply power to itself and multiple servos.
The positive line can be grouped together to your power source (say 5volt battery pack or whatever), have the ground go to both your battery pack AND an Arduino ground pin.
Question 2 years ago
If we want to attach four servo motors to First 4 analog pins of arduino... Then what is the code?
Answer 1 year ago
Digital pins are declared as 3,4,5...etc, analog pins as A0, A1... etc, so for this application you should write something like:
#define servoPin1 A0
#define servoPin2 A1
#define servoPin3 A3
#define servoPin4 A4
then use the same code and delete the digital pin declaration.
1 year ago
This is exactly what I've been looking for. I couldn't figure out the degree of movement vs time.
Thanks!!
2 years ago on Introduction
Thank you for sharing. I've been working on the same project with my recent bought servo motor. Your article is exactly what I was looking for.
2 years ago
Thank you for this! It was very helpful. I'm working on a project to control a HS-645MG servo motor with an Arduino. I've been getting some wobble and jittering out of the servo when I try and move it. At first I was told that this is likely due to the power source, but with a 4x AA battery pack solely serving the servo, I'm not so sure that that's the culprit. Would the fact that I'm using servo.write and not servo.writeMicroseconds make a difference? If so, is there a good sample code that moves a servo to multiple positions using servo.writeMicroseconds? Thanks again!
3 years ago
good instructable, keep on going. the best instructions what i was able to find
3 years ago on Step 5
Great work. Thanks
3 years ago
i connected sim800l Module with Arduino, Recommended supply 3.3volts are given. But the signal was not connected i tried arduino Power supply with computer and 12 volts Adapter
http://bigbelectronics.in/product.php?product=sim800l-gprs-gsm-module-microsim-card-core-boardquad-band-ttl-serial-port-pcbantenna
Question 3 years ago
Why not above pdf file is downloading.....???Any one can tell me?
3 years ago
Thanks for your ideas it helped me a lot