Retail Servo Landing Page


Overview:

Congratulations on your purchase of a SparkFun servo! Servos are motors that allow you to accurately control the rotation of the output shaft, opening up all kinds of possibilities for robotics and other projects.

Requirements:

Servo motors can be controlled with a multitude of microcontrollers and motor driver boards. However, for this guide you will find the following helpful:

What is a Servo:

Most motors will spin continuously when you apply power to them. That's OK for many purposes, but sometimes you want exact position control, such as "move to 180 degrees and stop." Doing this requires a feedback loop, which consists of a sensor on the motor shaft that measures what it is actually doing and something that can take that feedback and makes decisions based on that information. For example, if the motor is currently pointing at 10 o'clock, and you'd like it to point at 12 o'clock, then the decision-making circuitry would compare 10 o'clock and 12 o'clock and decide to move the motor clockwise until it's pointing at 12 o'clock.

You could spend a lot of time (and money) adding these parts and smarts to a motor, but the good news is that a servo has all these things built into it! Inside the tiny package is a DC motor, a gear train to trade fast speed for slower torque ("strength"), a potentiometer to measure the position of the output shaft and an "error amplifier" that compares the true position against the desired positions (the "error") and moves the motor so that the error is eliminated. Cool, huh?

How to Use it:

Normal motors have two wires; you just apply voltage to them and off they go. For DC motors you can also reverse the polarity of the two wires and have it run in reverse. A servo is a little different. It has three wires, one for power, one for ground and one for commands. Commands? Let's talk about the power and ground first.

Hooking it Up:

Hobby servos are designed for use in radio control (RC) aircraft, which typically use 4.8V batteries. Since they will work fine supplied with up to 6V, we usually run them at 5V, which is what many electronics systems use. The red wire goes to 5V, and the brown wire goes to ground. Unlike DC motors, you should NOT reverse the polarity to get the motor to run in reverse. Always give it 5V on the positive line, and the internal control circuitry will run it in reverse when required.

For the example in this guide, you will need to hook up a servo to an Arduino. There are numerous ways to accomplish this. You could simply attach a jumper wire between the female header on the servo and the headers on the Arduino. You could also cut and solder the ends of the servo to the controller. Another temporary approach is to solder two 3x1 male header pieces together so you can attach the servo to a breadboard.

 

Wire colors: red = power, brown = ground, yellow or white = control

 

How do I control it?

Servos understand a simple electrical language based on Pulse-Width Modulation (PWM). This means you send it 5V pulses on the control wire, and the width of the pulse will tell the servo to move to a specific position. Don't worry if this sounds complicated; there's software and hardware available to do the hard work for you.

The servo's position is controlled by the width of the pulse you send to the servo. If you want the servo to move to its minimum position (0 degrees), you'd send it a pulse that's 1ms wide (ms is milliseconds, or one 1000th of a second). If you want the servo to move to its maximum position (180 degrees), you'd send it a pulse that's 2ms wide.  Anything between 1.0ms and 2.0ms will correspond to all the positions between minimum and maximum. Servos like to be regularly updated, so you'll typically send these commands every 20ms or so (50 times per second), even if there's no change in the servo's position.

 

Various input pulses and resulting shaft positions

These are the standard values, but you can usually get a servo to turn further by sending it even narrower (or wider) pulses. Note that the servo cannot spin a full 360 degrees! If you try to make it go further than its allowed rotation, the shaft will hit internal stops (probably with a grinding noise), and the servo will draw quite a bit more power. (This shortens the life of the servo, so don't do this on purpose). There is a type of servo called "continuous rotation" that can spin 360 degrees, but you lose the positional control - in continuous rotation servos, the pulse width controls the rotation speed (forwards, backwards, and stopped at 1.5ms).

Show me the library!

As is common with the Arduino, someone else has done the hard work for you. The Arduino IDE contains a built-in library, appropriately enough called Servo, that makes controlling servos quite easy. Follow this example from the Arduino website to sweep your servo from minimum to maximum position. If you need help installing the Arduino software or uploading the sketch, their website has all the information you need.

Tips and Tricks

  • Note that servos can draw quite a bit of power when under heavy loads. For full strength, you should be sure that your power supply can handle up to one Amp of power (per servo).
  • In some circumstances, if your servos are running off the same power supply as your processor, the servos can draw so much current (or put so much noise on the line) that it may cause your processor to reset or misbehave. The simplest solution to this issue is to run your processor and servos off separate power supplies (but be sure to have a common ground between them). More complex solutions involve power supply noise filtering techniques; Google for advice.
  • Servos have a maximum speed. If your servo is acting erratically, you may be trying to get it to switch from one position to another too quickly (add a delay after your commands if you think this may be the problem).
  • The Servo library uses some of the same resources as the PWM pins (for analogWrite), and will disable those pins. Be aware of this if you want to use the Servo library and PWM at the same time. See the library documentation for more information.
  • Due to manufacturing variation, different servos may have slightly different ranges from each other. If this will affect your project, note that the Servo library's attach() command allows you to fine-tune each servo's min and max position.
  • If you need to drive a lot of servos and are running out of pins, you might look into external hardware controllers such as the Serial Servo Controller (ROB-08897). These take serial commands as input, and can drive up to 8 servos per board.

Have fun!

Enjoy your new servo! If you have questions, problems or want to show off your project, don't hesitate to contact us at techsupport@sparkfun.com.

Comments 0 comments