Controlling Model Railway Points or Turnouts with Servos and Arduino

Controlling Model Railway Points or Turnouts with Servos and Arduino

Controlling model railway points or turnouts with readily available RC servos is really simple. In this guide we take you through it step-by-step

What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based onWiring), and the Arduino Software (IDE), based on Processing. 
You can read more here
What else can Arduino do on a model railroad? 
The list is endless, from lights, to signals, to DCC controller. Arduino is a low cost, yet powerful system that is relatively easy to learn once you get started. In this article I’ll show you how to control several servos that can be used to throw points easily. Servos are cheap, reliable, quiet, and easy to configure. 
I decided to build several turnouts for my new layout but I also needed to be able to throw the points easily, at the flick of a switch you might say. 

What is a Servo 
A Servo is a small device that incorporates a two wire DC motor, a gear train, a potentiometer, an integrated circuit, and an output shaft. Of the three wires that stick out from the motor casing, one is for power, one is for ground, and one is a control input line.
Servo are readily available in a variety of sizes. My preference from past experience in RC aircraft is the well-known HXT900. Super reliable and it they even stock spare gears for this model
Servos are used in a wide variety of applications ranging from the RC hobby industry, industrial, medical and even military. The servo is essentially controlled by signal of variable width known as PWM (Pulse Width Modulation). The pulse is sent via the signal wire on the servo with the other two wires for positive/ negative current.
What hardware is required? 

1 x Arduino Board Uno Board

1 x Toggle Switch.

1 x Prototype Expansion board (optional but makes things easier)

1 x Soldering iron and rosin flux solder

2 x short lengths of wire

1 x Bag of servo extension leads


What Software is required? 
1 x Download the Arduino IDE software.

Installing Arduino. 
1)    Install the Arduino software, follow the prompts etc
2)    Open Arduino, and from the menu bar select tools, boards, and select Arduino/Genuino Uno.                                                      
3)    Copy and paste the code below into Arduino and upload to the board. To upload press the Forward arrow located at the top, far left, next to the check tick  

Connecting the hardware
1)    Solder a pin on to each wire of the switches. This is so they can be pushed into the connectors on either the Arduino or the prototype expansion board.
2)    If using the expansion board, insert header pins into ground, 5v+ and signal positions on the board. You can check the image below for reference. Solder the header pins into the board. These will allow you to connect the servos to the board easily, along with servo extension wires. 
3)    If you’re using the prototype expansion board we will start work on this             
4)    Solder pins onto one end of suitably long pieces of wire. These will go from the signal pins of the Uno board to the expansion board so we can select different output pins in the future. Wire choice is not really important, and I simply chose to use whatever I had on hand
5)    Tin the other end of the wire and then solder onto the expansion board so that it joins the header pins. The header pins should now be connected to 5v+, ground and the signal wire.  
6)    Solder pins to the toggle switch ends. Insert one end to ground (For this demonstration I’ve used a ground located on the top and bottom rows. If you’re going to use more switches then simply make a ground rail that you can plug more switches into. You can connect to the rail to either of the ground connections or both if you like.
7)    Plug the other end of toggle switch into ports 4 and 5. Polarity is not important
8)    Plug the signal header wires into pins, 9,10, and 11 if you made a third like I did (This is not referenced in the code but you only need copy the existing code to make more switches 
9)    Plug the servos into the header pins where you soldered those earlier. Polarity is important so be sure to observe, Ground, 5+ and Signal.
10)    You can now power the board externally or through the USB. Remember that the servos only take up to 6v so do not exceed this. 
11)    You can now toggle the switches and the servos will move accordingly. 
12)    Exciting right! Now I’m sure your dreaming up other ideas.
13)    If you need to drive more than one servo off one switch you can use a y lead


The Arduino Sketch (Simply copy and past this into Arduino and write the sketch to your Board)


#include <Servo.h>
const int switchPin1 = 1; //active low
const int servoPin1 = 7;

const int switchPin2 = 2; //active low
const int servoPin2 = 8;

const int switchPin3 = 3; //active low
const int servoPin3 = 9;

const int switchPin4 = 4; //active low
const int servoPin4 = 10;

const int switchPin5 = 5; //active low
const int servoPin5 = 11;

const int switchPin6 = 6; //active low
const int servoPin6 = 12;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

void setup() {
  pinMode(switchPin1, INPUT_PULLUP);

  pinMode(switchPin2, INPUT_PULLUP);
  pinMode(switchPin3, INPUT_PULLUP);

  pinMode(switchPin4, INPUT_PULLUP);

  pinMode(switchPin5, INPUT_PULLUP);
  pinMode(switchPin6, INPUT_PULLUP);

void loop() {
  if (digitalRead(switchPin1))
    servo1.write(90);  // HIGH (switch is off)
    servo1.write(100);  // LOW (switch is on)

  if (digitalRead(switchPin2))
    servo2.write(90);  // HIGH (switch is off)
    servo2.write(100);  // LOW (switch is on)

  if (digitalRead(switchPin3))
    servo3.write(90);  // HIGH (switch is off)
    servo3.write(100);  // LOW (switch is on)

  if (digitalRead(switchPin4))
    servo4.write(90);  // HIGH (switch is off)
    servo4.write(100);  // LOW (switch is on)
  if (digitalRead(switchPin5))
    servo5.write(90);  // HIGH (switch is off)
    servo5.write(100);  // LOW (switch is on)

  if (digitalRead(switchPin6))
    servo6.write(90);  // HIGH (switch is off)
    servo6.write(100);  // LOW (switch is on)

  delay(500);  //delay for debounce


Your switches are conected to ground then and pins 1-6

Your servo signal wires are plugged into pins 7-12 and the power is drawn from 5v+ and Gnd

This will give you 6 individual servos to control 

You can also adjust the servo travel by editing the write value. 
    servo6.write(90);  // HIGH (switch is off)
    servo6.write(100);  // LOW (switch is on)

So, what are you waiting for! It is entirely possible to expand on this with more servos and switches. Using the above examples you simply copy the code and using available pins assign those where appropriate. You may run out of room with the Uno board. Imagine the possibilities of a Arduino Mega 2560 R3 ATmega2560-16AU with 54 digital input / output terminals (14 of which have programmable PWM outputs) and 16 analog inputs

Stay tuned for more projects using Arduino!


About the Author
gozerian Profile