Servo motors jittering randomly when i try to control with potentiometers

Hi i am new to using arduinos and am currently trying to make a robot arm using 4 micro servos.
I am using a PCA9685 board to connect the servos to arduino and i have 4 potentiometers too control them but whenever i run the code, my servos randomly jitter and move about

code I am using:

#include <Wire.h>
 
// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>
 
#define MIN_PULSE_WIDTH       650
#define MAX_PULSE_WIDTH       2350
#define FREQUENCY             50
 
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
 
// Define Potentiometer Inputs
 
int controlA = A0;
int controlB = A1;
int controlC = A2;
int controlD = A3;
 
// Define Motor Outputs on PCA9685 board
 
int motorA = 0;
int motorB = 4;
int motorC = 8;
int motorD = 12;
 
void setup() 
{
  pwm.begin();
  pwm.setPWMFreq(FREQUENCY);
}
 
 
void moveMotor(int controlIn, int motorOut)
{
  int pulse_wide, pulse_width, potVal;
  
  // Read values from potentiometer
  potVal = analogRead(controlIn);
  
  // Convert to pulse width
  pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
  
  //Control Motor
  pwm.setPWM(motorOut, 0, pulse_width);
 
}
 
void loop() {
 
  //Control Motor A
  moveMotor(controlA, motorA);
  
  //Control Motor B
  moveMotor(controlB, motorB);
    
  //Control Motor C
  moveMotor(controlC, motorC);
  
  //Control Motor D
  moveMotor(controlD, motorD);
 
 
}

When servos jitter, it is almost always a problem with the power supply.

For four small servos, you need a separate 4.8 to 6V power supply capable of supplying at least 4 Amperes.

Don't forget to connect the grounds.

I am using a 4.8v 2400mAh power supply

do you think it is the current that is causing it?

That's a capacity - what sort of current is it capable of delivering?

mAh is battery capacity. Please post more details, and a photo of your setup.

Also, breadboards cannot handle the current required by motors and servos, so if you are using a breadboard, that could be a problem.

IMG_3278
here is the setup im using

i dont know the current of the power supply i am using but this is the amazon link
https://www.amazon.co.uk/gp/product/B085WVHKS6/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

The first things to try are to

(1) replace that small battery pack with a 4.8 - 6V power supply capable of providing at least 4 Amperes.

(2) MAKE SURE that all the grounds are connected.

Do you have an example of a power supply that would be suitable?

A 5V power brick or charger capable of providing at least 4 Amperes, or a high current battery pack for radio controlled vehicles would work.

Servos are power hungry.

Battery shown should be ample.
We use similar with 1/4 of the shown mAh capacity on 6 channel rc aircraft with no drama.
I'd go more for the breadboard and the flimsy jumper leads everywhere.

How does it behave if you disconnect three of the servos?

1 Like

Hi,
Welcome to the forum.
Thanks for using code tags.

Do you have a DMM?
If so, what do you measure the battery volts at as your servos jitter?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

You might also replace the pot variable with a constant and see if they still jitter. And disconnect all but one servo looking for jitter then add another servo .....etc

Hi,
Another thing to try;
In your void moveMotor, add an extra

 potVal = analogRead(controlIn);

after the first one.

Like this;

void moveMotor(int controlIn, int motorOut)
{
  int pulse_wide, pulse_width, potVal;
  
  // Read values from potentiometer
  potVal = analogRead(controlIn);
  potVal = analogRead(controlIn);

  // Convert to pulse width
  pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
  
  //Control Motor
  pwm.setPWM(motorOut, 0, pulse_width);
 
}

Tom.... :grinning: :+1: :coffee: :australia:

when i disconect all but one the issue is still present

No, if such servos jitter, it is nearly always a matter of the controlling pulses. If these pulses are not stable, the servo will jitter.

That should be done to see the cause of the jitter. Probably they will not jitter if you do so.
Reading an analog value will usually not give stable result without filtering. Calculate an average value over several measurements.

Hi,

Also changing from one analog input to the other with Arduino controllers can cause this problem.
Because there is only one ADC it is switched from input to input, it takes time, due to an input capacitor, for the new voltage level to stabilise.
It has been found that reading the same input twice and only using the last reading, can negate this.
That is why I suggested the code change in post#14.

Tom.... :grinning: :+1: :coffee: :australia:

We'll have to see what the OP reports.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.