Go Down

Topic: Servo Motor Twitching (Read 104 times) previous topic - next topic

plank8642

Hello!

I am trying to use an Arduino Uno to control 4 servo motors with 4 pots as analog inputs for my DIY small robotics arm project.

I have the servo's running of a separate 5V 5A Power supply.

When I try to run the attached code with even just ONE servo hooked up it twitches several times a second when it should just be idle. When i comment out the code so that i am only servo.write 'ing to my 1 hooked up servo it operates totally normal. When I comment up to servo.write for my second servo, my first servo begins to twitch again (while still being the only servo hooked up mind you.

It seems there is some kind of timing issue going on perhaps? And if so how could I  combat this?

Thanks!

p.s. my code came from this Youtube channel (so credit to him) but it seemed to work just fine for him.

https://www.youtube.com/watch?v=TkA2LJctU1c

Code: [Select]


//add servo library
#include <Servo.h>

//define Servo Motors
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

//define potentiometers

int pot1=A1;
int pot2=A2;
int pot3=A3;
int pot4=A4;

//variables to read values from analog pin (the potieometers)

int valPot1;
int valPot2;
int valPot3;
int valPot4;

 


void setup()
{
  // put your setup code here, to run once:
  //attaches servos -> PWM Pins ( 3, 5, 6, &9)
  servo1.attach(3);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(9);
 

}

void loop()
{
  // put your main code here, to run repeatedly:
  //reads value of potentiometers (value between 0 -> 1023)

  valPot1 = analogRead(pot1);
  valPot1 = map(valPot1, 0, 1023, 0,180); //scales value read from potentiometer to range to be used for servo
  servo1.write(valPot1); //sets servo position according to scaled value
  delay(15);

  valPot2 = analogRead(pot2);
  valPot2 = map(valPot2, 0, 1023, 0,180); //scales value read from potentiometer to range to be used for servo
  servo1.write(valPot2); //sets servo position according to scaled value
  delay(15);

  valPot3 = analogRead(pot3);
  valPot3 = map(valPot3, 0, 1023, 0,180); //scales value read from potentiometer to range to be used for servo
  servo1.write(valPot3); //sets servo position according to scaled value
  delay(15);

  valPot4 = analogRead(pot4);
  valPot4 = map(valPot4, 0, 1023, 0,180); //scales value read from potentiometer to range to be used for servo
  servo1.write(valPot4); //sets servo position according to scaled value
  delay(15);
 

}

zoomkat

"p.s. my code came from this Youtube channel (so credit to him) but it seemed to work just fine for him."

Whew, that video is kind of painful to watch. That being said, most servo issues are due to inadequate power issues. Servos perform better at 6v than 5v (4.8v-6v normal range). Don't wire servo power thru a breadboard. Servos take a lot of power, especially when under load.

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Wawa

I am trying to use an Arduino Uno to control 4 servo motors...
Which ones, and which supply (numbers/pictures/links).

Stall current is drawn every time the servo starts to move, and when the servo is mechanically overloaded.
The bigger ones could draw several amps stall, and your supply could go into protection mode.
Leo..


IoT_hobbyist

did you connect GND pin of the servo power supply to Arduino's GND pin?
If not, connect them together. See the wiring diagram in this tutorial

slipstick

There's nothing much wrong with the code so as said the problem is likely to be in the power supply or the wiring. A good photo of your complete setup and details of the components might give us some ideas.

The other time when servos might twitch is if you are trying to drive them into the end stops. So can you confirm that this twitching happens at all positions of the pot, even when it is centred?

NOTE: servos do NOT need PWM pins, any digital pin is fine. But using those pins won't cause any problems.

Steve

plank8642

Thank you to everyone who responded! It turns out it was an error with my code.... if you look closely at the last part of my code where I am mapping and using the servo.write command I had "servo1.write" for servos's 1,2,3,4....

So I was essentially asserting 4 different values to digital pin 3 (where servo1 was attached) and that was what was causing it to go berserk....lmao

vinceherman

Glad you found the problem and good on you for reporting it back here.
NOW
Any time that you start putting numbers at the end of variable names is a GREAT time to learn about arrays.

This untested code should produce the same result your code does:
Code: [Select]
//add servo library
#include <Servo.h>

const int NumServos = 4;

//define Servo Motors
Servo ServoArray[NumServos];

int ServoPins[NumServos] = {3, 5, 6, 9};
int PotPins[NumServos] = {A1, A2, A3, A4};

//variable to read values from analog pin (the potieometers)
int currentPotValue;

void setup()
{
  // put your setup code here, to run once:
  //attaches servos -> PWM Pins ( 3, 5, 6, &9)
  for (int i = 0; i < NumServos; i++)
    ServoArray[i].attach(ServoPins[i]);
}

void loop()
{
  // put your main code here, to run repeatedly:
  for (int i = 0; i < NumServos; i++)
  {
    currentPotValue = analogRead(PotPins[i]);
    currentPotValue = map(currentPotValue, 0, 1023, 0, 180); //scales value read from potentiometer to range to be used for servo
    ServoArray[i].write(currentPotValue); //sets servo position according to scaled value
    delay(15);
  }
}

Go Up