My question is about something that is happening ONLY when I use external power supply. When I use the power from the Arduino Mega directly, there is no problem. However, I need to use an external power supply, because I need to drive 42 servos. I believe this is related to the Arduino and external power supply not being powered on at exactly the same time, which is rather difficult given that the Arduino has some boot-up time.
When I connect a single 1 of these servos to any Arduino board directly (I have tried multiple boards), everything works as expected, in the sense that when I kill power to the Arduino, the servo stops, and when restarted, the program runs again and the servo continues working from the exact same position as when stopped. There is no default or initial sentinel value that the servo "jumps" to. Here is the sketch of this circuit working just fine w/ 1 servo:
However, when I connect an external power supply this setup even with a single servo, the servo "jumps" immediately to position ~90 (which would be the middle given that it has a 0-180 range). I need external power for my real project because I have 42 such servos that I am driving. After this initial "jump to position", the Arduino program continues as normal and all 42 servos are working as expected. But this "jump to position" is very disruptive! There are various parts connected to these servos. When I power up the system, the whole thing jumps to position immediately sometimes causing some damage to the system that I have to repair manually. It is extremely painful to watch 42 servos snap to position so quickly and so much of the system move so fast as a result.
I believe this has something to do with the control wires still connected to the Arduino, but the power and ground connected to the external power supply. So if the entire system (both Arduino and external power supply) is not powered on at exactly the time, then the servos are getting power without control or control without power and the incoming voltage sets them to some initial position, either by design or by undefined behavior.
But I don't know how to wire a circuit that allows the external power exactly after the pins have been attached to the servos in the setup()
function of the Arduino program, so the servos have control and power at the same time... Should I be trying to do this? How can I make sure all these servos and their control pins are all powered and attached at the same time?
Here is a rough wiring diagram of my system with only 1 servo (problem still exists):
I am using an Arduino Mega 2560 (I think rev3).
Here is the external power supply (5V 30A DC):
https://www.amazon.com/dp/B01HJA3OUG
Here are the servos:
https://www.adafruit.com/product/169
I am using the Arduino Servo
library, which does work w/ these servos and my board.
I used appropriate gauge wire for the DC amperage to drive these servos. They all seem to have enough power to run after this initial "jump to position". But I really want to avoid this!
UPDATE 10/7:
I put this delay in the setup()
function to give me time to plug in the external power supply at a specific point after bootup:
// LONG DELAY WAITING FOR EXTERNAL POWER TO BE APPLIED; LED blink
pinMode(LED_BUILTIN, OUTPUT);
start = curr = millis();
while (curr - start <= 10000)
{
curr = millis();
if (curr - prev >= 1000)
{
prev = curr;
if (state == LOW)
state = HIGH;
else
state = LOW;
}
digitalWrite(LED_BUILTIN, state);
}
If I put this code after the calls to attach the servo pins (i.e. servo1.attach(1)
, then the jump happens immediately when I plug external power supply in. If I put this code before the servos are attached, then there is no jump at first when external power supply is plugged in, but then immediately after the delay code above finishes. This tells me that the jump is happening when the control wires are attached (i.e. servo1.attach(1)
is called). However, this still doesn't give me any solution... If external power supply is applied either before or after when the servo control pins are connected, it jumps!
Why do these servos NOT jump when connected w/ power from the board, but jump when connected w/ external power supply?!
This must mean that either the actual amount of external power supplied to these servos is different than the Arduino is supplying (and thus causing different behavior) or it's the fact that the power ground and control pins are not all connected at the exact same time (and thus causing different behavior).
Servo
sattach()
the pins, then it jumps. However, if I wait for the program to boot-up, then I don't know when I can attach power in time for the program to run. I tried putting a 10 second "startup" delay, but it didn't seem to work as expected. Perhaps I put it before the pins were attached, not after... I will try all of this tomorrow. Thank you.