Arduino DIY Turnout Motor With Indicator Lights On Each Leg

How to make a servo controlled DIY slow moving turnout motor to control 1 or more turnouts.

 

 

Slow moving turnout on breadboard
The sketch on a breadboard

This tutorial is part of our series Arduino Projects.  There is a video playlist at Playlist

Also note that subscribers our channel get discounts in our shop. Visit the channel here and visit the shop to see what you can order. PolandsBest General Store

This sketch uses momentary contact buttons to cause the turnout to change positions..

The Sketch

[code]
const int button = A0;
int start1=0;
int stop1=80;

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup () {
Serial.begin(9600);
myservo.attach(6);

}
enum SWITCHSTATES
{
ST_OFF1,
ST_OFF2,
ST_STRAIGHT,
ST_TURN,
};
SWITCHSTATES switchState = ST_OFF1;

void loop() {
int buttonRead = analogRead(button);
delay(200);
switch (switchState)
{
case ST_OFF1:
switchoff1(buttonRead); //sets up changes to ST_OFF1
break;
case ST_OFF2:
switchoff2(buttonRead); //sets up changes to ST_OFF2
break;
case ST_STRAIGHT:
switchstraight(buttonRead); //sets up changes to ST_STRAIGHT
break;
case ST_TURN:
switchturn(buttonRead); // sets up changes to ST_TURN
break;
}
}

void switchoff1(int buttonRead) {
myservo.write(start1);

if (buttonRead > 500) {
switchState = ST_TURN; //switches to different state

}
}

void switchturn(int buttonRead) {
for (pos = start1; pos <= stop1; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
switchState = ST_OFF2; //chages to ST_OFF2
}

void switchoff2(int buttonRead) {
myservo.write(stop1);

if (buttonRead > 500) {
switchState = ST_STRAIGHT; //switches to Straight

}
}

void switchstraight(int buttonRead) {
for (pos = stop1; pos >= start1; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
switchState = ST_OFF1; //chages to ST_OFF1
}

}
[/code]

 

 

Video Demonstration

 

DIY Slow Moving Switch Machine
Watch this video showing the switch machine in action.

This link will take you to a video showing you this in operation.  DIY Switch Machine.

 

 

 

 

 

Wiring Instructions For 1 Switch Machine

Slow Moving Turnout Fritzing Diagram
Slow Moving Turnout Fritzing Diagram

You can use either an Arduino Uno , Arduino Mega or nano

The servo that we use is a Tower Pro Micro Servo 9g SG90. You can buy them on eBay, AliExpress or Albaba. They are probably cheap Chinese knockoffs and often they are somewhat erratic.  But they are cheap.

If you wish to use fewer signals, just do not hook up the LEDs.

The servo control wire hooks to pin 9.

The red wire is the power to the servo. Normally, for best operation and so that you do not overload your Arduino board, servos should be connected to a separate 5 V power supply rather than being connected to the Arduino board.

I never power servos from the board.

The servo negative wire can be connected to a separate bus but that also must be connected to the Arduino board ground.

Use a momentary contact button. The negative side of the control button will be connected to the Arduino board pin 8.

A 10 K dropping resistor must be hooked between the negative control button connection and ground.

The control panel LEDs are connected to pins 6 and 7.

The positive side of signal lights hook to pins 10, 11, 14, 15, 16, 17.  (If you wish to use few signal lights, just don’t hook up as many LEDs.)  A resistor should be installed between each pin and the positive side of the LED. Calculate the resistor size after looking at the specs for your LED. Here is a link to a LED resistor calculator.

Power the components from the board 5v and GND pins.

Always keep in the forefront of your mind that Arduino boards have maximum power limits.  And Arduino says that you should not push your board close to the maximum.  That means, if you have your servos powered from an external source, that you cannot have more than 10 lighted LEDs per board. Powering servos from the board reduces the number of usable LED s.

Be safe.  Power servos from a separate source.

 

Notes

You can adjust the arc of movement of the arm by changing these numbers that are in the sketch and then upload the changed sketch. Save under a new name so, should the need arise you can use the old one

const int straight = 90;

const int divergent = 110;

Start by removing the servo arm from the servo and reinstall it in the approximate starting position that you want.  Then adjust by changing the numbers above.

If you don’t know how to upload the new sketch, go to this page. How To Change And Upload An Arduino Sketch.

You can change the speed the arm travels through the arc by changing this number that is in the sketch.

// servo movement step delay, in milliseconds

const int step_delay = 70;

The number determines the millisecond delay between steps the servo takes.  A lower number, the slower the travel.

Once again judiciously rename your changed sketches.

 

Wiring Instructions For 2 Switch Machines

 

Editing in progress

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top