I am trying to control 4 servos but experiencing servo Jitter

I am trying to use a mpu 9250 to control 4 servos. This code works when I control 1 servo at a time however when I connect all 4 servos, they become VERY jittery. Does anyone see any flags within my code or may it be a hardware problem. There is no jitter when the gyroscope is disconnected. I am using an Arduino nano clone.

Here is my code.

```cpp


#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

Adafruit_MPU6050 srituhobby;



void setup(void) {
  Serial.begin(115200);
  servo1.attach(5);
  servo2.attach(6);
  servo3.attach(7);
  servo4.attach(8);


  Wire.begin();
  srituhobby.begin();
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);


  srituhobby.setAccelerometerRange(MPU6050_RANGE_8_G);//2_G,4_G,8_G,16_G
  srituhobby.setGyroRange(MPU6050_RANGE_500_DEG);//250,500,1000,2000
  srituhobby.setFilterBandwidth(MPU6050_BAND_21_HZ);

  delay(100);
}

void loop() {

  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  srituhobby.getEvent(&a, &g, &temp);

  int value1 = a.acceleration.x;

  value1 = map(value1,  -10, 10, 180, 0);
  servo1.write(value1);  
  Serial.println(value1);
  //delay(10);

   int value2 = a.acceleration.y;

  value2 = map(value2,  -10, 10, 180, 0);
  servo2.write(value2);  
  Serial.println(value2);
  //delay(10);

   int value3 = a.acceleration.x;

  value3 = map(value3,  -10, 10, 0, 180);
  servo3.write(value3);  
  Serial.println(value3);
  //delay(10);

   int value4 = a.acceleration.y;

  value4 = map(value4,  -10, 10, 0, 180);
  servo4.write(value4);  
  Serial.println(value4);
  //delay(10);

}

Welcome to the forum

Which Arduino board are you using ?
Where do the servos get their power from ?

1 Like

I am using an Arduino Nano clone and the servos are getting 5v from a benchtop power supply. The board is receiving power from the usb.

Do the servos and the Nano have a common GND connection ?

2 Likes

They do not. The servos ground to the power supply. would it be worth trying to add a ground from the board to the power supply?

Worth? Vital!

2 Likes

Ok let me give that a try! In the end the servos and board will be powered by 2 different batteries.

Very surprising if you don't have a common ground between the Arduino and the servos...

2 Likes

It probably worked because when testing 1 servo at a time it was powered by the board! I switched the servos to a power supply when adding 3 more so I didn't overload it. Im going to try adding the ground and will report back. Thank you all!

That explains it.

But not a good idea to run even one servo from the Arduino's 5V pin.

1 Like

Circuit: an unbroken path that returns to its starting point.

Think about this: if the only connection between your Arduino and your servos is the signal connection, where do the electrons that flow along the signal line return to the Arduino? That's why there's a common ground: to complete the circuit.

1 Like

I added a ground from the nano board to the power supply and that did not help. will be keeping it there since having a common ground makes complete sense but the jitters persist.

I'm going to figure out how to make a wiring diagram so you all can better see what I'm working with.

How much are the values from the sensors changing ? Maybe only write to the servos when the value has changed by a significant amount

1 Like

Step 1, instead of writing computed values to the servos, write static values; if the servos don't jitter, then it's not a wiring issue.

3 Likes

Im going to have to try this in a few!


Updated wiring diagram.

Sensor powered from Vin should be powered from 5V pin.

2 Likes

Servos briefly draw the start/stall current every time they start moving, so the power supply needs to be substantial.

Experienced people budget 1 Ampere per servo for small ones (e.g. SG90) and 2.5 A/servo for larger ones like MG996R. If you don't, expect the servos to twitch.

A 5V/1A phone charger certainly won't do for four servos.

2 Likes

Will switch this!