Setup Homing Servo Speed

Hi All,

Extremely new to robot programming, so I appreciate your kind help.

I’m currently working with a complete DIY robot project. I’ve assembled the robot using 5 DOF: waist, elbow, wrist elevation, wrist rotation and gripper. I am working with an Arduino Uno with a DFRobot I/O Expansion Shield V7.1 to connect my 5 DS3218MG servos.

I’m just experimenting with a simple code to learn the basics – which I’ve posted below. What I would like help with is in the void setup, I set initial servo positions. Then I go into a simple routine, exercising the joints in a for loop.

The issue I’m experiencing is that when it loops back to the void setup locations, the speed appears to be at a super max speed. It’s too fast and I would like a gentle speed like I have in the void loop. I tried adding delays and creating a return speed variable to set. But to no avail.

How can I slow down the void setup servo speed upon return?

Thanks in advance!

#include <Servo.h>

Servo myservoA;
Servo myservoC;
Servo myservoD;
Servo myservoE;
Servo myservoF;
int i, pos, myspeed, returnspeed;

void setup()

  // Attach each servo to a specific pin
  myservoA.attach(10);    // Waist servo at port 10
  myservoC.attach(9);     // Elbow servo at port 9
  myservoD.attach(6);     // Wrist elevation servo at port 6
  myservoE.attach(5);     // Wrist rotation servo at port 5
  myservoF.attach(3);     // Gripper servo at port 3

  returnspeed = 1500;

  // Position the waist in the center
  // Position elbow to a higher angle for better arm balance
  // Position the wrist elevation in the center
  // Position the wrist rotation in the center
  // Default to a wide open gripper

void loop()

  myspeed = 1500;
  for (pos = 0; pos <= myspeed; pos += 1)
    myservoA.write(int(map(pos, 1, myspeed, 90, 45)));
    myservoC.write(int(map(pos, 1, myspeed, 140, 90)));
    myservoD.write(int(map(pos, 1, myspeed, 90, 135)));
    myservoE.write(int(map(pos, 1, myspeed, 90, 0)));
    myservoF.write(int(map(pos, 1, myspeed, 30, 90)));



If you want the same speed in both directions then you need to use for loops for the moves in both directions as is done in the Sweep example code.

It's actually nothing to do with setup() which only runs once at the start of the program.


Hi @slipstick.

Thanks! I actually just tried that and I got a nice smooth motion back and forth. I think what I need to research now is a soft startup. Or is that something everyone has a deal with....a max speed start?


If you mean the initial move in startup() then there's nothing practical you can do to slow it down. But the way you're doing it means a couple of the servos will move to the default 90 position and then move to the chosen position (140 or 30). Although it seems counter-intuitive if you put those initial writes BEFORE the .attach() commands then that overrides the default so at least they only make a single movement.


Hi Steve!

Thanks for the update. I guess there's not much I can do if it is a common occurrence with everyone.

I'll try putting the writes before the attaches and see what you're describing.

Thanks again so much!

With common hobby servos there is nothing you can do about it. You can get (more expensive) servos that can report their position and then you can control a slower move to where you want it.

@wildbill Thank you for your input. Stinks that we can't control it, but at least I know I'm not missing something.