Controlling Servo Motors with Arduino

Updated 27 April 2022

Servo motors are extremely useful in so many different applications; it’d be good to learn how to control them! Solenoid and DC motor control have been shown already; for the most part, they are both pretty straightforward (only power and ground connections) methods of motor control. We'll be controlling our servo using PWM on an Arduino.

A servo motor is a little different, using 3 connections (Power, Ground, and Signal) to move the motor to a certain rotary position. This position is dictated by what is sent on the signal wire. Once the motor reaches the position specified by the signal, it will hold its position and resist any forces that try to move it from that position. This resistance is known as the Torque Rating of the servo and will be found on the datasheet.

There are a couple of types of servo you can get:

  1. Fixed Rotation Servos – These servos have fixed limitations in their rotation, so you can’t turn it past its limitations. Great for robotic arms, pulling/pushing levers etc.
  2. Continuous Rotation Servos- These servos allow you to continually turn either clockwise or counter-clockwise indefinitely.
  3. Linear Servo – Allows you to have linear motion with servo motor control.

Pulse Width Modulation timing and servo rotation angles diagramPulse Width Modulation

Servo motors tend to work from the 0-6V range, especially the hobby servos we are used to seeing in the Makersphere. In order to control the rotation, we send a pulse of a variable width on the signal wire (the power and ground wires used as references for the signal). If you aren't familiar with it, a pulse is simply a square waveform that goes from 0V to another voltage for a set period of time, before returning to 0V. The servo will turn left or right according to the width of this pulse.

The idea of using pulses to control a servo’s position can seem a little backward at first, take a look at this diagram to get a more intuitive understanding of how it all works. Essentially there is a neutral position that the servo motor’s circuitry will hold at, this will be defined by a pulse width (let’s call it 1ms for this example).

Hypothetically, imagine the 1ms pulse that holds our servo at neutral will hold that position indefinitely. If we send a pulse that is shorter than the 1ms neutral pulse ( <1ms ) the motor will fractionally turn toward zero degrees. The opposite happens on the other side ( > 1ms ) and the motor will turn fractionally toward maximum rotation (180 degrees). The circuitry that calculates this fractional amount of rotation either side of neutral is a lot more complicated than this simplification. That should give you a good idea of the way it works, though.

This theory behind the servo isn’t too relevant when we are programming with the Arduino either. There is a library that we import that takes care of most of this PWM talk behind the scenes. The library is known as the Servo library and you can see Arduino’s reference page for all the different functions of that library.

The basics of using the servo library includes the following steps:

  1. Name our servo motor as a servo, this is done using the Servo [servo name]; function.
  2. Attach our servo to a pin, this pin will be the signal pin for our servo and uses the [servo name].attach([PinNumber])
  3. Create a variable for the position of the servo (or just use an integer value)
  4. We can now write a position to the servo using [servo name].write([position integer]).

We are going to use an Arduino Uno and a small hobby servo in this tutorial. Generally, servos draw more current than what the Arduino pins can supply (20mA per pin), always check the power specs of a device before connecting it to your Arduino. As we know this hobby servo is within the specs, it will be fine to use directly connected to the Arduino for this tutorial. If your servo motor is larger, look into connecting via a servo shield or similar external supply device.

Your setup should look like this, note we are using the Pin 10 with PWM but any of the PWM pins will do!

Arduino Uno connected to Servo circuit diagram

Writing the code is as simple as following the process above for using the Servo library. We will write a sketch that can move our servo backward and/or forward. You can see our code below but feel free to write your own and use ours as a checklist of sorts.

#include <Servo.h>//import the servo library

Servo coreservo; //Name the Servo

void setup() {
  coreservo.attach(10); 
  /*Attach the named servo object to Digital IO 13, use following syntax: 
  servoname.attach(Pin#, minimum Pulse width (ms), maximum pulse width (ms)); 
  if you want to define the pulse widths for your motor*/

}

void loop() {
  coreservo.write(0);
  delay(200);
  coreservo.write(90);
  delay(200);
  coreservo.write(180);
  delay(200);
  /* If you wanted to read the angle of your servo at any given time, use servoname.read();
   * If you wanted to write a pulse of a certain width use servoname.writemicroseconds(value in microseconds);
   */
}

If you are wanting to build a robot or something that uses a whole bunch of servos take a look at this great servo shield from Adafruit, It allows you to have 16 Servos connected up at any given time!

Thanks for taking the time to look over this tutorial, if you were left wondering about anything to do with servo control with an Arduino or you think we could've been a little more in-depth about any of the concepts in this tutorial let us know in the comments below!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.