Serve by Rotation
Or sweep servo back and forth
This note illustrates the introductory use of a servo motor directly from an Arduino single board microcomputer. It is the second note in a planned series of articles on using motors with micro-computers for electronic projects. The previous note illustrated the use of hobby DC motors.
There are three basic types of motors used with single board computers:
- Direct Current (DC)
This note is limited to a simple (and single) servo motor. The use of a stepper motor will be illustrated in a future article.
Servo is a truncation of the term, servomechanism, that describes the operation of the integrated solution to move a shaft to a desired position. The movement can be rotational for an angular position (as illustrated in this note) or can be linear too. The servo motor permits a degree of control over the angle of its shaft using the following functions:
- Electronic controller with integrated potentiometer
- Gear train
- Drive shaft
The rotation angle can be converted to a linear position with additional mechanical gear components. The angle of the shaft (from a reference position) is fed back to a closed-loop servomechanism (i.e. electronic controller) for comparison with the desired position that is sent as a signal to the solution. Unfortunately, for low cost solutions, it is impractical for the actual angle of the shaft to be returned as an output signal. For industrial applications, the speed of the motor can be a control variable too.
The motor can be driven by AC or DC. The latter is the preferred choice for small and hobby applications. It is important to understand that the motor component of the servo system is constantly running. The output shaft however stays at the set position.
Pulse Width Modulation
The most elementary way to control the angle of the external shaft in a servo motor uses the technique, known as Pulse Width Modulation (PWM), as shown below:
While a servo motor has a limited angle of rotation (i.e. 180 degrees), full rotation servo motors are gaining in popularity. The shaft position for these servo motors extends to full 360 degrees. The position is attained through clockwise or counter-clockwise rotations as appropriate.
The pins on the Arduino board that support PWM have the tilde character, “~”, as a prefix. The pins are 3, 5, 6, 9, 10, and 11. The method, analogWrite, is available to write to the pin as follows:
Controlling the motor
The Arduino UNO R3 board has the following limitations for the flow of current:
- Under USB power: 500 mA (protected by polyfuse but bypasses onboard 5 V voltage regulators)
- Under external power (barrel connector): 500 mA – 1 A
If both connections are used then the power from the barrel connector is preferred as long as the voltage is above 6.6 V DC.
Since the servo motor may place a current surge for the power, a simple electrolytic capacitor, nominally rated at 470 µF, can smooth the distribution as shown below:
A more robust solution uses a separate power supply for the motor as shown in the schematic diagram below where a common ground connection is required:
SG90 Servo Motor
The most popular servo motor for hobby applications is the TowerPro SG90 model that is available in analog and digital versions. This note is limited to the demonstration of the analog model.
“There are many counterfeit versions of TowerPro servo motors from dealers in China that are being sold on eBay, Amazon, Alibaba and other similar websites. If these dealers removed the TowerPro logo from the corresponding photos and product descriptions, they are selling counterfeit low quality servo motors. Please ensure that the supplier is an authorized licensee selling genuine products before you purchase the corresponding goods. Your cooperation in preserving intellectual property rights of original owners will help us continue to provide superior and strategic solutions for you.” - TowerPro
Sadly, wanton disregard for intellectual property ownership occurs universally – no need to single out companies in Schenzen for this particular product. Obviously, the sentiment “one gets what one pays for” prevails for these types of products. For example, some of the counterfeit versions do not operate over the full rotational range design angle (i.e. 180 degrees). Others are not consistent in their rotation owing to lower stall torque limitations.
The key specifications for the TowerPro SG90 analog servo motor are:
0.91 x 0.48 x 1.14 in
4.8 V DC
0.36 secs for 180 degrees
POM gear set
Dead band width
0 – 55 degrees Celsius
500 – 2400 µs
There is a newer version of this model, TowerPro SG92R, that uses carbon fiber gear set that supports improved performance for torque and speed.
There are two standard libraries at the Arduino site for use with servo motors:
The key functions of this library are:
attach(pin, min, max)
Attach the servo entity to a pin where min & max are pulse widths for 0 and 180 degrees positions of the external shaft
Set the desired shaft angle (in degrees from 0 to 180)
Value ranges from 1000 (counter-clockwise) through 1500 (middle) to 2000 (clockwise). Owing to manufacturing challenges some tolerance in these values should be assumed.
Returns the current angle of the external shaft
Returns a boolean value on the attached state of the servo entity object – true if attached, false if not attached.
Detaches the servo entity object from a previous attachment
Arduino UNO R3 single board micro-computer
TowerPro SG90 servo motor, analog
DuPont, male-to-male, 10 cm approx
Any standard size
Mount computer board and breadboard, optional
Documenting the Build
All projects in this introductory set of basic and elementary projects, the microcomputer board and the breadboard are mounted on a base-plate. This technique has been illustrated in a previous project and for the sake of brevity will not be repeated here.
The final assembly is shown below:
The diagram below illustrates the schematic for the elementary exercise to test the servo motor:
The assembly diagram below illustrates a proposed layout for the exercise:
The following diagram introduces a nominal electrolytic capacitor to reduce the current surge when the motor starts the rotation:
The use of a dedicated power source for the motor isolates the Arduino board further from surge currents. The diagram below is not necessary for the micro server motor in the current set of exercises but may be used for heavier duty servo motors:
Rev Me Up!
- Servo motor
- Matha Goram
- Arduino UNO R3
- Servo motor, preferably micro category
- DuPont connecting wires
- Base-plate for platform and breadboard (optional)
There three different arrangements to connect the servo motor to the Arduino board:
• Direct connection to the board for signal, power and ground terminals
• Direct connection as above but using an electrolytic capacitor to minimize current surge when the motor starts
• A separate power supply drives the servo motor
The rotation of the servo motor relying on the standard Arduino Servo library performs the same operations in two ways:
• The application specifies the angular position in degrees
• The application specifies the pulse width that the servo uses to establish the angular positions
The following code snippet illustrates the iteration from 0 to 180 degrees for the rotational position:
for (int i=0; i<=180; i+=1)// iterate through all positions from 0 to 180 degrees
myServo.write(i); // set the shaft angle to corresponding orientation
delay(100); // 100 milliseconds hold for test use only
The following code snippet illustrates the iteration from minimum to maximum pulse width (which in turn represents the available angle of rotation for the servo motor):
for (int i=pwMin; i<=pwMax; i+=1) // iterate through all pulse width values from pwMin to pwMax
myServo.writeMicroseconds(i); // set pulse width for desired angle and rotate shaft to corresponding position
delay(5); // 100 milliseconds hold for test use only
- Limited to schematics
- No placeholders to get the checklist to 100%
- Not applicable