Can you use more than 12 servos with the servo library on an uno

A question popped in my mind.

I am aware that the servo lib can only control 12 servos.

However, if I

  • create 20 servo objects
  • detach the servos after reaching positions (I am aware they become unpowered, that is no problem for me)
  • and make sure that no more than 12 servos are ever attached at the same moment / make sure that there are no more than 12 servos moving at the same time

Can I than use the servo library to control all 20 servos?

For instance, I have a ServoSweep library. Every ServoSweep object has a private Servo object. The library slowly turns a servo, 1 degree per time interval. When a servo is in position, the library detaches the pin.

const int nServo   = 20 ;
const int maxServo = 12 ;
ServoSweep servo[ nServo ] ; // every ServoSweep object uses one private Servo object.

void moveServos()
{
  uint8_t activeServo = 0 ;

  for( int i = 0 ; i < 20 ; i ++ )
  {
    if( servo[i].sweep() )  // sweep detaches and attaches the servo object to a pin when needed
    {                       // if sweep returns true, the servo is moving and thus attached
                            // if sweep retuns false, the servo is not moving and thus detached
      if( ++ activeServo == maxServo ) return ; // if 12 servo motors are attached, the remaining ones will have to wait
    }
  }
}

Kind regards,

Bas

 0  1  2  3  4  5  6  7 //  8
 8  9 10 11 12 13 A0 A1 // 16
A2 A3 A4 A5             // 20

I count 20 servo capable pins.

Regards,

Bas

Here's a Mega servo simulation... adjust for number of pins (20).

How will You debug that dinner?

You could use an I2C based pca9685 modul, or more than one if you need more than 16 servos.

https://de.aliexpress.com/item/33057485742.html

Well for the fun of it, I am copying the content of my servoSweep library in that online software test thingy as we speak.

I think for the first test I will let all motors rotate 90 degrees in lets say 1 seconds

I will toggle the states (state being either the lower or the upper limit) of each servoSweep object with a different interval. The interval times will be doubled every time. So on a given moment more than 12 servo's have to move at the same moment.

How do I exchange that arduino mega for an uno? I clicked on the + but I am not seeing an uno anywere. I have not used wokwi before ..

nvm, found it

When you need more than a couple of servos, then it's time to move to a PCA9685 servo board.
Two of those boards can do 32 servos, and that only takes two Arduino pins.
Leo..

Ok it seems this method does not work. motor 13 and higher will not show any movement whatsover. That is consistent with your library explanation.

I am however convinced now that it can be done.

With the following KISS code example I used a single servo object to control 2 servo's by simply detaching the first pin and attaching an other pin. I knew this would work, but I still tested it :nerd_face:

  servo.attach( 1 ) ;
  servo.write( 0 ) ;
  delay(2000) ;
  servo.write( 180 ) ;
  delay(2000) ;
  servo.detach( ) ;

  servo.attach( 2 ) ;
  servo.write( 0 ) ;
  delay(2000) ;
  servo.write( 180 ) ;
  delay(2000) ;
  servo.detach( ) ;

The next thing I want to do, is to alter my servoSweep library by using a pointer to a servo object instead of the private servo object. Than I create an array of 12 servo objects and I dynamically allocate any available/detached servo object of that array to one of my 20 servoSweep objects.

It will propably take some time, before it is finished. But it sounds completely viable.

You could use an I2C based pca9685 module

I know all about that. I have also seen that you can drive 10 servo's using 2 IO pins and a decade counter, but for more than one reason (self made PCB to name one). I really want a single atmega 328 to drive 16 servo's without additional hardware like a pca9685.

Kind regards,

Bas

Many problems start with the words "I want".

Good luck with your project.

You could probably drive more servos if you decrease the refresh rate. That may or may not cause less desirable servo behavior.

Or maybe use another timer…

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