New to Arduino and need help with servos!

Hey everyone,

I am new to working with Arduino and am just beginning my first project. Eventually, the final product is going to be a robotic arm that can pick objects up. It is going to include 5 different servos, and this is where my questions begin. I am easily able to control one servo with one potentiometer using the Knob code, but when I try to connect a second servo and a second potentiometer, it doesn't work. Both servos vibrate, or pulse, but do not turn to the desired rotation. Is this because of a lack of power? I am using an Arduino Uno and this is just using the USB power supply. Here is my code:

#include <Servo.h>

Servo myservo; // servo
Servo myservo2;

int potpin = 0; // analog pin
int potpin2 = 2;

int val; // value of analog pin variable
int val2;

void setup()
{
myservo.attach(9); // servo on pin 9
myservo2.attach(11); // servo on pin 11
}

void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(5);

val = analogRead(potpin2);
val = map(val, 0, 1023, 0, 179);
myservo2.write(val2);
delay(5);

}

Next, how would I go about connecting 5 servos? Would I need additional power? If so, how could I do this?

Thanks in advance! Any help would be MUCH appreciated! :slight_smile:

Also, if anyone needs more information please ask!

-Will

Never power even one servo from the USB power - servos contain motors and motors and logic chips shouldn't be sharing supplies.

A rough rule of thumb is 1A for each servo that's moving simultaneously for your power
supply not to droop under load. USB is 0.5A so it will have just been dropping out
all the time with 2 servos.

What's worse if you were unlucky inductive spikes from the motor in the servo could
have damaged any/all of the chips connected to 5V USB, including your computer...
Not worth the risk.

A wise precaution is therefore never to power motors, servos or relays from the 5V Arduino rail.

A secondary precaution that seems worthwhile is to use a powered USB hub to supply
the Arduino if doing anything that might risk putting bad voltages on the USB 5V rail,
then you only risk damaging the hub, not your computer or laptop...

how would I go about powering this then? I have a 4x AA battery pack and two 2 x AA battery packs.

You will need a 2 amp, 5 or 6 volt regulated power supply to run 2 servos. For something like a robot arm, where several servos may be working together, as MarkT suggested budget 1 amp/servo. So you might need a 5 amp, 5-6 volt regulated power supply.

Alkaline AA battery packs can't supply much more than about 1 ampere before the pack voltage starts to drop and at a 1 ampere discharge rate, won't last more than an hour or so.

what kind of batteries/power supply could be used to get that amount of power needed?

Also, the diagram that I am using to base my robotic arm on is attached. Can someone explain to me how the power for it works?

Sorry for all the dumb questions about power, I am having a hard time understanding how it works.

Replace the idiotic "AA batteries" with something like this, observing proper +/- polarity: Shop by Category | eBay

If you are following someone's recommendations to build this robot arm, and that person suggests using AA battery packs, you are being badly misguided.

jremington:
Replace the idiotic "AA batteries" with something like this, observing proper +/- polarity: Shop by Category | eBay

If you are following someone's recommendations to build this robot arm, and that person suggests using AA battery packs, you are being badly misguided.

To power servos in a robotic arm, one should use 6v, not 5v. At 5v the arm might not be able to hold much load or even lift itself..

Lifting power depends on the torque that the servos can provide and the torque varies dramatically from one servo type to another. For example, Pololu sells servos with torques ranging from 6 oz. in. ( 0.4 kg-cm) to 1600 oz. in (115 kg-cm), with the typical servo providing about 40 oz. in (1.9 kg-cm). Obviously if lifting power is an issue, you will need to choose the right servos!

Torque is proportional to motor current. Given that 266:1 range in torque, whether the servo power supply is 5 or 6 volts is not a major issue. More importantly, the power supply has to provide the required current. Most beginners vastly underestimate the current requirement and underpower their projects. This leads to forum posts like "my servos are all twitching", which results from the power supply voltage dropping to the point that the electronics don't function correctly.

Have another look at your (alleged) sketch code.

Where are you calculating the value of "val2" which you are providing to the second servo ?

jremington:
Given that 266:1 range in torque, whether the servo power supply is 5 or 6 volts is not a major issue.

It is self-evident that there is going to be a big difference between big servos and little servos.

But with a specific, actual, servo, the performance does depend significantly on the voltage. That is why the datasheet for almost every servo you see, will quote a torque value at 4.8V or 5V, and another torque value at 6V, and it often more than 20% different.

Correct. What is not self-evident is that there is also a very large range of torques for servos of the same physical size. So, one should take a careful look at the manufacturer's specifications.

Edit: as long as we are on the topic of comparing servos of the same physical size, there is often a tradeoff on speed versus torque; so high torque servos may be slower. The data sheets give a transit time for some range of arm motion, like seconds/60 degrees. If high torque is needed then also check that the servo has metal gears and ball bearings, which will provide more torque for the same power.

Just for fun here is a comparison of a few standard size servos (40x20x40 mm, MG = metal gears, BB=ball bearing)

manufacturer and type transit time, torque @ 6V, torque @ 4.8V
GWS S03N XF Standard Servo 0.12 35 31
GWS S03N 2BB Standard Servo 0.18 58 47
GWS S03T STD 0.27 110 100
Power HD High-Torque Servo 1501MG 0.14 240 215
Power HD Standard Servo 6001HB 0.14 93 81
Power HD Standard Servo 3001HB 0.12 61 49

The Power HD 1501MG is amazing!

There is a lot of detailed information here. You certainly need adequate current capability from your servo power supply, whatever it is. Since you're starting in all this, it may be worthwhile to read an overall article on Servos, like the one on the ArduinoInfo.Info WIKI HERE:

A single servo of the smallest type like these can normally run OK on the Arduino regulator and USB:
http://yourduino.com/sunshop2/index.php?l=product_detail&p=238

I often use those small servos to control a large lightweight pointer for things like a large temperature display dial. ONE of those works OK on USB. More than one or larger servos is definitely a No-No!

DISCLAIMER: Mentioned stuff from my own shop...

could I use a 9V battery that has an adaptor to be put into the AC jack to power the 5 servos? In case it helps, my servos are all standard size except for one, which is a micro.

could I use a 9V battery that has an adaptor to be put into the AC jack to power the 5 servos?

No, you need a SEPARATE power source to power the servos. It needs to be 5 or 6 volts DC and enough current to handle the servos. You need to find out how much current they use. Also can more than one servo be 'moving" at the same time? Servos take a larger current to accelerate and move, and much less when they are in position.

what about a battery like this? All of my servos require 4.8-6 volts.
http://www.amazon.com/American-Hunter-Lantern-Rechargeable-Battery/dp/B003WHDSDY

what about a battery like this?

That would work.. That's a " 6 volt Gel Cell Lead Acid Battery". There are others, often used in those emergency lights you see in stairwells etc. Check Home Depot etc??

Good advice. Silly to think a 9V battery would even come close. The Sealed Lead Acid (SLA) is an economical choice if you want to be battery powered. A LiPo would do it too, but then you would be at 7.4V which might be a little high. Also the LiPo's are a little fussier and need a specific charger designed to charge LiPos.

My suggestion is you do a power budget of how many servos and other motors, lights, etc. you want to run. Even the 6V SLA batteries come in different ampere ratings. Simple math: Amps * hours of run time is how big a battery you want to design in. If you can be plugged in though, Jameco has a large selection of "wall warts" in the 5-6V range.