Reduce servo speed with arduino

Hi all,

I have written a code for my arduino nano with shield to control switches on my model railway track. I have connected 6 servo’s to it and I’m controlling these by pull up through 6 relay, but the sg90 are going full speed which is not that realistic and noisy.

Does anyone know how to reduce the speed of the sg90 servo (speed per servo not necessary, but ok, one parameter for all servo’s would be great)

This is the code I’m using:

#include <Servo.h>
// a maximum of eight servo objects can be created 

Servo myservo1;  // create servo object to control a servo 
 int pos1 = 0;    // variable to store the servo position 
 int button1 = 1;  // The button will be on Pin 1 
 
Servo myservo2;  // create servo object to control a servo 
                
 int pos2 = 0;    // variable to store the servo position 
 int button2 = 2;  // The button will be on Pin 2 
 
Servo myservo3;  // create servo object to control a servo 
 int pos3 = 0;    // variable to store the servo position 
 int button3 = 3;  // The button will be on Pin 3 
 
Servo myservo4;  // create servo object to control a servo 
 int pos4 = 0;    // variable to store the servo position 
 int button4 = 4;  // The button will be on Pin 4

Servo myservo5;  // create servo object to control a servo 
 int pos5 = 0;    // variable to store the servo position 
 int button5 = 5;  // The button will be on Pin 5  
 
Servo myservo6;  // create servo object to control a servo 
 int pos6 = 0;    // variable to store the servo position 
 int button6 = 6;  // The button will be on Pin 6  
 
 void setup() 
{ 
 myservo1.attach(7);  // attaches the servo on pin 7 to the servo object
 pinMode(pos1, OUTPUT);
 pinMode(button1, INPUT); 
 digitalWrite (button1, LOW);
 
 myservo2.attach(8);  // attaches the servo on pin 8 to the servo object
 pinMode(pos2, OUTPUT);
 pinMode(button2, INPUT); 
 digitalWrite (button2, LOW);
 
 myservo3.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(pos3, OUTPUT);
 pinMode(button3, INPUT); 
 digitalWrite (button3, LOW);
 
 myservo4.attach(10);  // attaches the servo on pin 9 to the servo object
 pinMode(pos4, OUTPUT);
 pinMode(button4, INPUT); 
 digitalWrite (button4, LOW);
 
 myservo5.attach(11);  // attaches the servo on pin 9 to the servo object
 pinMode(pos5, OUTPUT);
 pinMode(button5, INPUT); 
 digitalWrite (button5, LOW);
 
 myservo6.attach(12);  // attaches the servo on pin 9 to the servo object
 pinMode(pos6, OUTPUT);
 pinMode(button6, INPUT); 
 digitalWrite (button6, LOW);
 
 } 
 
 void loop() 

{ 
  if (digitalRead(button1) == LOW)
  for(pos1 = 0; pos1 < 45; pos1 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  {
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button1) == HIGH) 
  for(pos1 = 45; pos1>=45; pos1-=45)     // goes from 45 degrees to 0 degrees 
  { 
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}
{ 
  if (digitalRead(button2) == LOW)
  for(pos2 = 0; pos2 < 45; pos2 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  {
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button2) == HIGH) 
  for(pos2 = 45; pos2>=45; pos2-=45)     // goes from 45 degrees to 0 degrees 
  {  myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}
{ 
  if (digitalRead(button3) == LOW)
  for(pos3 = 0; pos3 < 45; pos3 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  { 
    myservo3.write(pos3);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button3) == HIGH) 
  for(pos3 = 45; pos3>=45; pos3-=45)     // goes from 45 degrees to 0 degrees 
  { 
    myservo3.write(pos3);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}
{ 
  if (digitalRead(button4) == LOW)
  for(pos4 = 0; pos4 < 45; pos4 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  {
    myservo4.write(pos4);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button3) == HIGH) 
  for(pos4 = 45; pos4>=45; pos4-=45)     // goes from 45 degrees to 0 degrees 
  { 
    myservo4.write(pos4);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}
{ 
  if (digitalRead(button5) == LOW)
  for(pos5 = 0; pos5 < 45; pos5 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  { 
    myservo5.write(pos5);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button3) == HIGH) 
  for(pos5 = 45; pos5>=45; pos5-=45)     // goes from 45 degrees to 0 degrees 
  { 
    myservo5.write(pos5);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}
{ 
  if (digitalRead(button6) == LOW)
  for(pos6 = 0; pos6 < 45; pos6 += 45)  // goes from 0 degrees to 45 degrees in steps of  degree 
  { 
    myservo6.write(pos6);              // tell servo to go to position in variable 'pos' 
      delay(50);                        // waits 50ms for the servo to reach the position
  } 
    if (digitalRead(button6) == HIGH) 
  for(pos6 = 45; pos6>=45; pos6-=45)     // goes from 45 degrees to 0 degrees 
  { 
    myservo6.write(pos6);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
}}}}}}                                   // 6x close bracket for each servo

}

Hi All, I already found the VarSpeedServo.h library!

Hi Paul_DMV,

I am interested in doing something similar for controlling my turnouts. I have two questions:

  1. Can you share the schematic to show how you connected the relays to the pins?

  2. Were you able to successfully implement VarSpeedServo.h? Is it possible for you to share the revised sketch?

I drive my turnouts using Digitrax SE8C (I need loconet reporting) but I want to use servos instead of tortoise SM. If your setup works for me I will find a way to connect the SE8C outputs (12V bipolar) to uno/mega inputs. I will be using 8 servos.

Any help is appreciated as this will be my first arduino project.

Thanks,
Joy