Electronic – Arduino UNO resets when driving servo

arduinoservo

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);
  }

Best Answer

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).