Go Down

Topic: Servos convulsing randomly (Read 974 times) previous topic - next topic

arctel

I have 4 servos running on an arduino as a robotic arm.  They are being powered by the usb, with only 1 servo operating at a time.

At certain points two of the servos start convulsing and shaking and the usb/com port connects and disconnect every second, then it requires unplugging/resetting the arduino several times just to get it back to normal.

I'm not sure if the servos can't handle the weight of the arm, if it's a power issue, or perhaps the serial connection with something in my code.
(note: the serial connection is with a C# program giving the 1-8 commands).

Anyone have any ideas?

Thanks in advance.

Code: [Select]

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
Servo myservo2;
Servo myservo3;
Servo myservo4;


byte incomingByte;      // a variable to read incoming serial data into
uint8_t commandIn;
int spd = 15;
int servo1_pos = 0;
int servo2_pos = 0;
int servo3_pos = 0;
int servo4_pos = 0;



int pos = 0;    // variable to store the servo position

void setup()
{
  Serial.begin(57600);
  myservo1.attach(9);  // attaches the servo on pin 8 to the servo object
  myservo2.attach(8);  // attaches the servo on pin 9 to the servo object
  myservo3.attach(10);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(11);  // attaches the servo on pin 9 to the servo object

//  delay(500);
//  myservo1.write(90);
//  delay(500);
//  myservo2.write(90); 
//  delay(500);
//  myservo3.write(90);
//  delay(500);
//  myservo4.write(90);
//  delay(500);

}


void loop()
{
  if (Serial.available() > 0)
  {
    commandIn = Serial.read();
    switch (commandIn)
    {
      case '1':
      {
        for (pos = servo1_pos; pos < servo1_pos + 5; pos++)
        {
          myservo1.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }       
        servo1_pos += 5;
        break;
      }
      case '2':
      {
        for (pos = servo1_pos; pos > servo1_pos - 5; pos--)
        {
          myservo1.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }
        servo1_pos -= 5;
        break;
      }
      case '3':
      {
        for (pos = servo2_pos; pos < servo2_pos + 5; pos++)
        {
          myservo2.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }       
        servo2_pos += 5;
        break;
      }
      case '4':
      {
        for (pos = servo2_pos; pos > servo2_pos - 5; pos--)
        {
          myservo2.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }   
        servo2_pos -= 5;
        break;

      }
      case '5':
      {
        for (pos = servo3_pos; pos < servo3_pos + 10; pos++)
        {
          myservo3.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }   
        servo3_pos += 10;
        break;
      }
      case '6':
      {
        for (pos = servo3_pos; pos > servo3_pos - 10; pos--)
        {
          myservo3.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }   
        servo3_pos -= 10;
        break;
      }
      case '7':
      {
        for (pos = servo4_pos; pos < servo4_pos + 10; pos++)
        {
          myservo4.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }   
        servo4_pos += 10;
        break;
      }
      case '8':
      {
        for (pos = servo4_pos; pos > servo4_pos - 10; pos--)
        {
          myservo4.write(pos);              // tell servo to go to position in variable 'pos'
          delay(spd);
        }   
        servo4_pos -= 10;
        break;
      }
    }
  }
}

zoomkat

Quote
They are being powered by the usb


Bad! Servos usually require an external power supply to prevent the servos from causing the arduino to reset.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

MarkT

Just one small servo will typically demand more than 500mA, so you cannot
expect them to run off USB at all.
[ I won't respond to messages, use the forum please ]

arctel

Will try a new power source and see if that fixes it.  Thanks.

jefuchs

I am having the same problem, but my servo runs off a separate power source.  I have the Arduino running off usb, and the servo running off a 6v bank of AA batteries.

Everything ran fine when I first got it.  But after just a few days, the servo started going haywire.  It swings back and forth despite receiving no commands from the processor.

I'm a noob, and I'm just practicing and testing code as I learn.  So to be sure I wasn't at fault for this, I used the standard "knob" and "sweep" sketches that come with the Arduino software.  Same thing happened.

I suspect this could be a counterfeit servo.  After ordering my TowerPro MG995, I started seeing several articles warning that they've been so widely counterfeited that it isn't recommended to use them at all.  I do not know if this is the case with mine. 

This seems to be a widespread problem, if what I've read is true. How do you know you're ordering a genuine product?
I need to develop my first project quickly.  The world is running out of words that can end in "-duino".

jremington

You have the same problem. Your power supply (AA batteries) can't supply the needed current unless very fresh. You should budget one ampere of current per active servo, so if four are moving at once, the power supply needs to supply at least four amperes at 5-6V.

zoomkat

Quote
Everything ran fine when I first got it.  But after just a few days, the servo started going haywire.  It swings back and forth despite receiving no commands from the processor.


Make sure you have a good ground between the servo and the arduino. Loss of the ground can cause erratic behavior.

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

jefuchs


You have the same problem. Your power supply (AA batteries) can't supply the needed current unless very fresh.


Bingo!  I bought new AA batteries and now everything works fine.  My final project (if it ever happens) will be powered by a hefty marine battery (with a converter for correct voltage).  But for working on the prototype, I use household batteries.

Thanks.
I need to develop my first project quickly.  The world is running out of words that can end in "-duino".

jefuchs

Nope.  Acting crazy again.  Worked normally for no more than a full minute.  Set it aside, and tried it later with the same fresh new batteries, and it's acting cray.
I need to develop my first project quickly.  The world is running out of words that can end in "-duino".

zoomkat

What is the measured voltage output of your batteries? If they are rechargeable they may only output 1.2v each when fully charged.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

jremington

If your power supply is inadequate to power the servos, you are simply wasting your time.
Alkaline AA cells can output 1 ampere FOR A FEW MINUTES and then they are useless for servos.
NiMH AA cells (use 5 for a 6 volt power source) do a bit better, but not much.

TomGeorge

Hi, jefuchs, post a pic of your project and a CAD or pic of handdrawn circuit , also your sketch using # code tags.
This will help us immensely.

Tom...... :)

(I should copywrite that first line of this post too..must check with zoomkat...)
Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Go Up