Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

Im 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); (thats mean 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...

Someone have any idea? I don't know how to deal with this problem...

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);
  }
share|improve this question
3  
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

3 Answers

Arduino developer wrote that first baudrate for controller is set at 9600 baud. Therefore you must use this baudrate for first communication. After is possible change this baudrate to high speed

share|improve this answer
The original poster seems to be setting the baud rate for 9600bps already, maybe you could expand on your answer to explain why it may solve the problem? – PeterJ Mar 10 at 7:22

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);
     }
share|improve this 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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.