Home / Tutorials / Arduino Tutorial / Connecting a Servo Motor to An Arduino
Arduino Servo MG996R

Connecting a Servo Motor to An Arduino

Building robots with Arduino starts with knowing how to move objects through motors. There are a lot of type of motors used in microcontroller-based systems: dc motors, stepper motors and servo motors.  In this Arduino servo motor article, I will discuss what is a servo motor and how to use it with an Arduino.

Introduction to Servo Motors

A servo motor is a special device containing a dc motor, feedback electronics and a set of gears. It follows the concept of servomechanism.

The operation of the servo motor can be described by the diagram below:

Arduino Servo Diagram

  • A servo motor accepts pulses and outputs rotation.
  • When the DC motor is at stop, it means the motor shaft and potentiometer knob are in its equilibrium position. The inputs to the error amplifier are equal and the motor is undriven.
  • When a pulse is applied, the two inputs on the error amplifier is no longer equal and thus the amplifier drives the motor. As the motor turns, the gear assembly turns and thus also turns the potentiometer.
  • The potentiometer (via voltage divider) generates a voltage equivalent to that of the input pulse that stops the DC motor, indicating equilibrium.

The gear assembly slows down the rotation of the motor to a speed that the potentiometer can catch up. Also, the gears boost the torque output of the servo motor.

Pulse Width and Position

The position of the servo motor arm corresponds with the width of the pulse:

Servo Motor PWM position control
From SM2608 Hardware Hacking Workshop

The exact pulse width could vary per servo motor. If you want to get the exact pulse width for a specific angle, you must test your servo motor.

There is mechanical stop at the gear assembly to limit the rotation of servo motors to 180 degrees. There are two reasons why servo motors don't rotate for a full circle:

  1. Originally designed for converting rotational motion to linear motion, servo motors do not need full 360 degree rotation (see crank mechanism).
  2. The limited possible values for PWM signals needs to be mapped to a smaller range (of angles).

There is a way to make a continuous rotation servo and is shown in this tutorial. A continuous rotation servo is stronger than a an ordinary dc motor.

Servo Motor Internals

Here is my MG996R servo motor:
Arduino Servo MG996R

As you can see, there are three wires: red for +5V, black for ground and yellow for pulse. Other servo motor brands have different colors for their wires but almost all uses red for +5V and black for ground.

This servo motor is capable of producing high torque (rotational force), and thus has metal gears inside:

Arduino Servo MF996R Disassembled

Servo motors that don't produce high torque typically contain plastic gears.

A better look at the circuit board, dc motor and potentiometer:

Arduino Servo MG996R internals

Arduino to Servo Motor Wiring Diagram

To begin, wire this circuit:

arduino servo circuit

Most servo motors run on 5V so you can attach the red lead to the Arduino’s +5V pin. However, larger servos might draw more current which can reset the Arduino.

My MG996R draws 10 mA at idle, 170 mA when operating but without any load connected and stalls at 1400 mA (!).

Remember that the Arduino +5v pin is limited to 500 mA! Thus,  the number (and sometimes, type) of servos you can use is limited.

The yellow pin is where you would send PWM signals to the Arduino. This means this lead must be connected to any of the pins with the ~ in them.

Side note: But it doesn’t mean that the control pin is limited to only a PWM pin. By the way, there are other ways to generate PWM besides the hardware PWM on the Arduino.

Programming an Arduino for Servomotors

The Arduino Servo library has two easy-to-use examples. This library comes with the IDE so no additional downloads necessary. We’ll first be using the Sweep example. On the Arduino IDE, go to File > Examples > Servo > Sweep. This sketch will open up:

Dissecting the Sketch

Of course, the code starts with

Which simply tells the compiler to include all the Arduino Servo library functions.

Next, you must create a Servo object like this:

According to the creators of the Servo library, you can create up to 12 servo objects for the UNO and most other boards and up to 48 for the Arduino Mega.

You then need to attach the Servo object to a pin:

To move the servo arm, you use the servo.write() function like so:

The servo.write() function receives the angle in degrees as the argument. In this code snippet, pos is incremented from 0 to 180 inside a for loop and then decremented from 180 to 0 in another for loop.

Using Microseconds instead of Angles

If you want to write microseconds for controlling instead of degrees, you can use the writeMicroseconds() function. This is a common example that positions the servo motor arm at center:

Here, a 1500 microsecond pulse is generated by the Arduino which is equivalent to 1.5 millisecond, the pulse width for centering the arm of the servo motor.

Use Potentiometer to Control a Servo Motor

The other example, the Knob sketch, shows how to control a servo motor with a potentiometer. The voltage from the potentiometer circuit is read through analogWrite and converts it to rotation. I used this wiring diagram for this next example:

Arduino diagram for Knob sketch

Knob Sketch:

Similar to the Sweep sketch, the servo motor signal pin is attached to pin 9 of the Arduino UNO. The wiper (middle) arm of the potentiometer is connected to analog pin 0 while the other pins are connected to +5 and GND. With this connection, the potentiometer acts as a voltage divider circuit.

When the wiper arm of the potentiometer is turned, the voltage on the A0 pin changes. Moreover, the minimum value is zero while the maximum value read by the analogRead() function is 1023 according to ADC theory. This range of values is converted to degrees of rotation using the map() function.

Multiple Servo Connections

It's more fun to use more servo motors! However, there are challenges. You cannot use the Arduino's +5V pin to power three or more servo motors because of the current limits of the board. The solution is to have a separate power source for the servo motors. Here is an example diagram:

arduino servo multiple


The six servo motors are powered by a 4.8 V battery (rechargeable) pack.

Sketch for Multiple Servo Motors

An example code for testing out multiple servos:

Upload this sketch above to your UNO board and then open serial monitor. You can control which servo rotates by typing the servo number on the monitor.

Now that you know the basics of servo motors, try recreating my hand-gesture controlled robotic arm! Here's that project in action:

I hope you found this tutorial useful. Have fun creating with arduino and servo motors!

Check Also

Arduino AD9833 signal generator

Using AD9833 Signal Generator with Arduino

Ever wondered how you can create sine, square or triangular waves with Arduino? Apparently, there’s …