# Arduino UNO resets when driving servo

I'm trying to control two servos at once using Arduino UNO and Arduino Motor Shield. My servos are modified, so that they can work with constant movement.

When I send the first order myservoP.write(180); (full speed) it works fine; however, when I try to order my servo to change direction (myservoP.write(0);), immediately my Arduino crashes and resets.

At first I though it may be a problem with the power supply, but after testing it with my program (listed below), I am starting to think that it's a problem with buffer or memory. Without delays, the Arduino crashes after about 10 iterations. With delay(10), it will crash after about 90 iterations. With delay(20), Arduino is able to repeat a full cycle 5 times, from start to finish. During the 6th attempt, it crashed again.

Here is my code:

#include <Servo.h>

Servo myservoL;
Servo myservoP;
int i;

void setup()
{
delay(2000);
myservoL.attach(9);   // attaches the servo on pin 9 to the servo object
myservoP.attach(10);  // attaches the servo on pin 10 to the servo object
Serial.begin(9600);
}
void loop()
{
for(i=180; i>=0; i--)
{
delay(20);
Serial.println(i);
myservoP.write(i);
myservoL.write(i);
}
for(i=0; i<=190; i++)
{
delay(20);
Serial.println(i);
myservoP.write(i);
myservoL.write(i);
}

• An obvious test would be to run the program without the servo connected. If that stops it from crashing, it probably is electrical noise or supply loading from the servo at fault. But if it still crashes, it is probably an error in your program. – Chris Stratton Dec 26 '12 at 17:12
• Ok. So i checked, and i CAN use two servo's at once. And i run my test program without servos and it didn't crashed. So its electrical... I have to figure out how to check what makes problem... Thx for help, guys. – Marcin Dec 26 '12 at 17:28
• You might want to look into an external power supply for the servos, or at least not running them off the USB through the Uno. – Chris Stratton Dec 26 '12 at 17:55
• I actually plugined 4 AA bettery and it's still not enough. – Marcin Dec 26 '12 at 18:17
• Yeee. 4 battery IS enough. I had to screw something with arduino servo pins, cause when i plugined my servo to regular 5v and GND on board, and signal to normal digital pin, it started works! (Of course this 4 battery i plugined to external power supply on arduino morot shield.) – Marcin Dec 26 '12 at 18:43

If the Arduino does not reset when the servos are not plugged in, then likely what's happening is that the servos draw a lot of current and so the battery voltage drops enough that the Arduino resets.

Try using two separate battery packs: one to power the Arduino, and the other to power the motors (make sure to connect the grounds of the two battery packs together).

Complement to Nate's answer: if you want to keep one battery for both the motors and the arduino, you should limit the current drawn by the servos by having some kind of ramp-down before changing direction, e.g. half speed after 20ms, 0 rpm after another 20ms, before changing direction. If you are changing both motors' direction at the same time, you should delay one by say, 20ms to reduce further the peak current.

Before you do, you can monitor the voltage of the arduino's supply, preferrably with a 'scope set to "single", trigger ~4.5V downward slope. That should tell you if the arduino's supply drops when the motors change direction.

try to increase the delay time to 5000 instead of 20 , so the servos can take enough time to complete the rotation :

#include <Servo.h>

Servo myservoL;
Servo myservoP;
int i;

void setup()
{
delay(2000);
myservoL.attach(9);   // attaches the servo on pin 9 to the servo object
myservoP.attach(10);  // attaches the servo on pin 10 to the servo object
Serial.begin(9600);
}
void loop()
{
for(i=180; i>=0; i--)
{
delay(5000 );
Serial.println(i);
myservoP.write(i);
myservoL.write(i);
}
for(i=0; i<=190; i++)
{
delay(5000 );
Serial.println(i);
myservoP.write(i);
myservoL.write(i);
}


## protected by Community♦Oct 26 '18 at 13:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).