Hey Folks. Can i ask you to have a look at this video?
I've been trying to get some stable predictable motion out of my servos. I've tried several different libs, including the new MegaServo lib. Here is the code that the Arduino was running in the video.
#include <MegaServo.h>
MegaServo Servos[3];
void setup()
{
Servos[0].attach(2, 800, 2200);
Servos[1].attach(3, 800, 2200);
Servos[2].attach(4, 800, 2200);
}
void loop()
{
for( int i =0; i < 180; i++)
{
Servos[0].write(i);
Servos[1].write(i);
Servos[2].write(i);
if (i==180)i=0;
delay(10);
}
}
The intention of the code is to have all three servos sweep from 0 to 180 degrees then snap back to 0. Repeat for ever. As you can see from the video, the motion is fairly random, sometimes it manages a full sweep, but eventually it will lock up.
I'm running this on 6 fresh AA cells, and have also tried on USB power too. Two of the servos are Acoms AS-16, the other is a cheep micro servo. The Board is a Freeduino, and i'm using Version 15 of the Arduino software. You can also see the green pin13 LED flashing, although there is nothing in the code to make this happen. Is this indicative of the board resetting?
Looks like you're powering the three servos from the 5V output of the Arduino. Is that right? If so, you are definitely exceeding the maximum current rating of the power supply. USB power will also be definitely too weak for three servos. I think the Arduino is crashing, as you suspected. Can you power the servos fromo a bigger supply, such as a mains powered 5V PSU? Just as a test, if you're thinking of making a mobile, battery-powered device!
Excellent! yes, i'm pulling them through the 5v pin, I had assumed that was directly connected to the voltage regulator which had a max current capable of taking on 3 servos easily. I can certainly power the servos direct from the battery pack, that should fix it.
As a related note is there a simple way of getting the arduino to report the voltage coming into the regulator? so that I could get early warning of the batteries dying? It would also be useful to have the Arduino measure the current that is being drawn buy the servos, even though they will be powered direct by the batteries rather than the 5v pin. Is there a resource that explains how to do this?
To get the Arduino to read the input voltage, you'll need to reduce it from the 6V or so of the battery pack to less than 5V, and feed that to one of the Arduino's analog inputs. Use a voltage divider: