When using a servo, does it bounce around a bit? Move when it’s not supposed to? There are a few things you can try to fix this problem.
A servo can pull 500mA or more current, especially just as movement starts. If you have a multi-meter, you can put it inline and figure out exactly how much current the servo pulls. If your power source can’t provide 500mA of current, it may cause the circuit to brown out and reset.
If your Arduino is plugged into a computer or wall outlet, this likely isn’t your problem unless you have multiple servos or other current-hungry components.
When using batteries, avoid the rectangular 9V batteries. They can rarely supply enough current. I generally will use 3 or 4 AA batteries connected to a boost converter. This will provide enough current for one servo to move along with a few LEDs and sensors. If you’re interested in more exotic batteries, lithium-polymer (LiPo) and lead-acid batteries will also supply plenty of current, but I have no experience with them. LiPo batteries can catch fire, which makes me a bit paranoid to have them out in the woods unattended. You can read more about my experience with power supplies here.
Standard servos can be set between 0 and 180 degrees. Some lower-quality servos have issues keeping position at the extreme ends of this range. To test if this is the problem you’re seeing, have your servo stay in the range of 10 to 170 degrees. I usually do this just to avoid problems down the road.
A servo uses one of the digital PWM pins. PWM stands for Pulse Width Modulation, which means the Arduino send a rapid on/off signal of various lengths to simulate an analog signal. You can tell if a digital pin has PWM capabilities because it will often have a tilde (~) next to it. If there is no tilde, PWM is often active on digital pins 3, 5, 6, 9, 10, and 11.
Based on how often the signal is on vs. off, the servo will move to the corresponding position. A problem can arise if there are other components on your circuit using interrupts – it can create noise and make the servo uncertain if the PWM signal is on or off.
You may not know if another component is using PWM, but there’s a reasonably simple solution to fix a jittery servo: place a capacitor directly across the 5V and ground pins to “clean” the signal. I use a 470μF capacitor. The next smallest I have is a 47μF, and it did not work. Here’s a diagram of how to set it up; the top photo of this blog entry also shows the capacitor in place.
Additional Specialty Components
Some components require a very steady data transfer rate. A good example is Adafruit’s NeoPixels, shown to the right. Your sketch is likely calling interrupts often – a small bit of behind-the-scenes code which executes between your lines of code. This is especially common if you’re using any libraries. These interrupts interfere with the precise data transfer rate required by the specialty component, causing it to fail. Or, vice versa, the specialty component messes up the signal to your servo, causing it to shake. Adafruit does a much better job than me explaining the problem, as well as suggesting a possible solution.
I hope you find this information useful. If you come up with another solution, please let me know!