Power source for servos

I need to power 8 servos for the four legged robot I'm building, and the servos are glitching out. It's coded with an Arduino Uno R3 I'm assuming the problem is the power source since its just 3 AA alkaline batteries and it only started glitching when multiple servos were being powered. Also this is put together via bread board and I want to know if there is a better way to connect all these servos to the power source without all these stupid wires.

I was thinking maybe 4 rechargeable AA batteries would do, and I was looking into protoboards. Is this the right direction?

I tried taking pictures of my breadboard setup but theres like no good angle so I'm sorry ;-;



Also here is my code:

#include <Servo.h>

//////////////////////////////////////////////////
// States
//////////////////////////////////////////////////

enum RobotState { IDLE, WALK };

RobotState currentState = IDLE;

//////////////////////////////////////////////////
// Gait Settings (SAFE VALUES)
//////////////////////////////////////////////////

float phase = 0.0;
float speed = 0.04;        // smaller = slower walk
float stepLength = 20;     // hip swing amount
float stepHeight = 25;     // knee lift amount

int neutralHip = 90;
int neutralKnee = 95;

//////////////////////////////////////////////////
// Servo Offsets
//////////////////////////////////////////////////

int offsetFLHip   = 0;
int offsetFLKnee  = -9;
int offsetFRHip   = 0;
int offsetFRKnee  = 4;
int offsetBLHip   = 11;
int offsetBLKnee  = 0;
int offsetBRHip   = -5;
int offsetBRKnee  = 15;

//////////////////////////////////////////////////
// Servo Setup
//////////////////////////////////////////////////

Servo frontLeftHip, frontLeftKnee;
Servo frontRightHip, frontRightKnee;
Servo backLeftHip, backLeftKnee;
Servo backRightHip, backRightKnee;

void setupServos() {
  frontLeftHip.attach(2);
  frontLeftKnee.attach(3);
  frontRightHip.attach(4);
  frontRightKnee.attach(5);
  backLeftHip.attach(6);
  backLeftKnee.attach(7);
  backRightHip.attach(8);
  backRightKnee.attach(9);
}

//////////////////////////////////////////////////
// Safe Write (clamped)
//////////////////////////////////////////////////

void writeServo(Servo &s, int angle, int offset) {
  int corrected = constrain(angle + offset, 0, 180);
  s.write(corrected);
}

//////////////////////////////////////////////////
// Smooth Trot Gait
//////////////////////////////////////////////////

void updateLeg(
  Servo &hip,
  Servo &knee,
  int hipOffset,
  int kneeOffset,
  float legPhase
) {
  // Hip swings full sine wave
  float hipAngle = neutralHip + stepLength * sin(legPhase);

  // Knee lifts only during forward swing
  float lift = max(0.0, sin(legPhase));
  float kneeAngle = neutralKnee - stepHeight * lift;

  writeServo(hip,  (int)hipAngle,  hipOffset);
  writeServo(knee, (int)kneeAngle, kneeOffset);
}

//////////////////////////////////////////////////
// IDLE
//////////////////////////////////////////////////

void Idle() {
  writeServo(frontLeftHip,  neutralHip, offsetFLHip);
  writeServo(frontLeftKnee, neutralKnee, offsetFLKnee);

  writeServo(frontRightHip, neutralHip, offsetFRHip);
  writeServo(frontRightKnee,neutralKnee, offsetFRKnee);

  writeServo(backLeftHip,   neutralHip, offsetBLHip);
  writeServo(backLeftKnee,  neutralKnee, offsetBLKnee);

  writeServo(backRightHip,  neutralHip, offsetBRHip);
  writeServo(backRightKnee, neutralKnee, offsetBRKnee);
}

//////////////////////////////////////////////////
// WALK (Diagonal Trot)
//////////////////////////////////////////////////

void Walk() {

  phase += speed;
  if (phase > TWO_PI) phase -= TWO_PI;

  float oppositePhase = phase + PI;
  if (oppositePhase > TWO_PI) oppositePhase -= TWO_PI;

  // FL + BR
  updateLeg(frontLeftHip,  frontLeftKnee,  offsetFLHip, offsetFLKnee, phase);
  updateLeg(backRightHip,  backRightKnee,  offsetBRHip, offsetBRKnee, phase);

  // FR + BL
  updateLeg(frontRightHip, frontRightKnee, offsetFRHip, offsetFRKnee, oppositePhase);
  updateLeg(backLeftHip,   backLeftKnee,   offsetBLHip, offsetBLKnee, oppositePhase);
}

//////////////////////////////////////////////////
// Setup & Loop
//////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);
  setupServos();
}

void loop() {

  checkInput();

  switch (currentState) {
    case IDLE:
      Idle();
      break;

    case WALK:
      Walk();
      break;
  }

  delay(15);  // smooth + safe refresh rate
}

//////////////////////////////////////////////////
// Serial Control
//////////////////////////////////////////////////

void checkInput() {
  if (Serial.available()) {
    char cmd = Serial.read();

    if (cmd == 'w') currentState = WALK;
    if (cmd == 'i') currentState = IDLE;

    if (cmd == '+') speed += 0.01;
    if (cmd == '-') speed -= 0.01;
  }
}

What is the voltage and total current requirement of the servos? Are they all the same?

It is advisable to NOT run motors or amperage hungry loads through a breadboard and definitely not directly from the arduino.

Also it's difficult to see the wiring and how it is supposed to be routed. I am unsure if you wiring is supposed to be this way but you do know the wires in the groups I circled are sharing a circuit?

The wires you circled are the power cables to the servos. Is that the issue? Genuine question.

Also the servos all the same, Mg90s. No they are not powered directly from the Arduino, the power is from a battery pack with the only thing connected to the Arduino being the signal wires from the motors and common ground from the breadboard. Recommended voltage for one servo is 4.8-6v, and the total requirement of the servos is around 6v 6.4A.

Please create proper schematics. The pictures show mystery cables not useful. How is the powering done? Not words but schematics please.

So do you then have up to 3.4 amps per “rail” on your breadboard?

3 AA (I assume wired in series, but we don’t know yet) batteries will deliver a nominal 4.5 volts. Your minimum required is 4.8 volts. You might hit 4.8 volts if the batteries are good. BUT if you start powering to many servos at once, there is no way they can handle up to 6.4 amps or current without problems.

If the batteries are wired in series to get the voltage, you only get the current capacity of 1 battery.

If they are wired parallel to get more current capacity, you only get the voltage of one battery.

Ahh, I understand. That's pretty much was I was trying to determine with my original question

If AA's aren't enough, then what do you recommend instead? Also I'm assuming I'm going to have to use something other than a breadboard, what do people usually use in this situation?

I think I saw a power adapter on the end of your breadboard. Can it handle the required amperage?

For proof of concept, you may be able to find a plug in “wall wart” type transformer that can meet your requirements.

Some folks make a battery pack. You can wire several packs in series and then wire those packs in parallel. You may try to step up to “D” size batteries. Or look at a sealed battery LIKE THIS. Some folks use/adapt power tool batteries.

I bet a google search for a rechargeable battery pack that meets your requirements will almost certainly reveal many choices.

A proto board may not be necessary. The servo power wires can be wired to a common rail. The + can have a rail and the - can have a rail, similar concept as the breadboard. The breadboard can still be used for the servo signal wiring. Or, since your arduino has pin headers, you can wire your signal wires directly to the arduino.

Hope this helps…

1 Like

Reading a random SG90 micro servo datasheet, each pulls 250mA in normal use (2A total if used at the same time), but pulls a startup current can be near 1A (500 to 800mA) would require a supply that can supply 8A current, or ensure startup of the servos is sequential.

Random datasheet:

1 Like