3 servo with a battery

please help I'm trying to make 3 servos run with this code but it is not working when I connect it to the battery
I'm using arduino uno

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
int i=0;



void setup() {
  servo1.attach(9);
servo2. attach(10);
servo3. attach(11);
}

void loop() {

  for (i=0; i < 180; i++);
    
  {
    servo1.write(i);
    servo2.write(i);
    servo3.write(i);
    delay(10);
  }


  for (i=180; i > 0; i--); {
    servo1.write(i);
    servo2.write(i);
    servo3.write(i);
    delay(10);
  }
}

Welcome to the forum

Which Arduino board do you have, which type of battery are you using and how is it connected to the Arduino ?

Feeding power to the Arduino

Have you tried your sketch with only one servo connected?

Depending on the size of servo your battery may have to provide 3A or more. That's not going to happen with some types of battery, so details matter.

I am using Arduino Uno which is also connected to a 9V Carbon battery. I am currently testing only one servo and it is connected to pin 10, the other pin is connected to GND and the servo I'm using is an SG90 servo motor(micro servo)

this is the sketch

#include <Servo.h>

Servo servo1;
int i = 0;



void setup() {
  servo1.attach(10);
 
}

void loop() {

  for (i = 0; i > 180; i++)
    ;

  {
    servo1.write(i);
    delay(10); 
  }


  for (i = 180; i < 0; i--)
    ;
  {
    servo1.write(i);
    delay(10);
  }
}

Hello jared2006

Welcome to the world's best Arduino forum ever.

I´ve made a code review:

#include <Servo.h>
Servo servo1;
int i = 0;
void setup() 
{
  servo1.attach(10);
}
void loop() 
{
  for (i = 0; i > 180; i++)
  {
    servo1.write(i);
    delay(10); 
  }
  for (i = 180; i < 0; i--)
  {
    servo1.write(i);
    delay(10);
  }
}

You need to supply the servos with a proper PSU.

Have a nice day and enjoy coding in C++.

That sounds like aPP3 battery. If that is what you are using then it cannot supply enough current at the required voltage long enough to be useful

the code is not working but i found another code that can make it work

#include <Servo.h>

Servo servo;

void setup() {

  servo.attach(10);
  
}

void loop() {

  servo.detach();
  delay(80);
  servo.attach(10);
  servo.write(180);
  delay(80);
  servo.detach();
  delay(80);
  servo.attach(10);
  servo.write(0);
  delay(80);

now can i ask how can i add the other 2 servos
and make them spin at the same time

Before adding more servos, can I ask whether there is a reason for detaching/attaching the servos in the loop() function ?

Please clarify which type of battery that you are using

If you are using this type of battery


Such a battery has a capacity of 200 mAh.
Your arduino without any servo active is drawing 10 mA
This means withing 20 hours of operation the battery is empty.

If you drive servos the current will go up to 50 to 100 mA per Servo.
This means your battery will be empty within 2 to 4 hours.

If you try to supply your servo from the Arduino board 5V pin.
At least if your servos have to deal with some weight this will make the onboard-voltage-regulator go into thermal shutdown = everything stops

For a demonstration without any mechanical load on the servo-horns and tiny small servos it might be sufficient to use 0,5A per servo.

In any case you should look up the datasheet of the servo how much current the servos are drawing for 2 cases:

  1. normal operation
  2. stall current

Your powersupply should be able to deliver the stall current otherwise you are in danger of damaging the power-supply

I am currently using the battery for testing and for the prototype, after some time I'm gonna shift it to an AC power supply the only thing I need to know now is how can I make the 3 servos spin at the same time

I just saw this code on yt and tried it and worked, I'm still figuring out why does it need detaching/attaching

It doesn't

Attach the servos once in setup()
Don't detach() them

I have it working now all three of them rotating at the same time, I thank you guys for helping me in this.

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