Arduino Turnout Control With Position Indicator And Warning Lights

Instructions with code and wiring diagrams that show how to operate a servo controlled model railroad turnout.

This code allows for the addition of up to 8 leds whose state is determined by servo position.  You may use fewer LEDs.  With 8 LEDs, there are two on each leg of the turnout plus two on the control panel.

There are other turnout and servo control routines and a lot more in the Free Arduino Sketchbook .

 

This is code to  use.  The fritzing diagram and notes are below.

[ Begin Code ]
*/

Turnout control with LED position indicators on each leg of the turnout plus a remote flashing warning spotlight
that indicates the switch is in a divergent status.

This sketch may be loaded and used on an Arduino Nano. By using the Analog pins as Digital pins, you may
add more lights to be controlled as you wish.

The basic was sketch created by Gary Granai August 16, 2021 an later modified with much appreciated millis
code modification help by Jim Weisenbach and Dave Brown, members of the Facebook Group,
Arduino For Model Railroaders.

Wiring diagrams, video and discussion located at Arduino DIY Switch Machines  https://steamtraininfo.com/arduino-projects/arduino-diy-switch-machines

Many other Arduino projects can be found in the Free Arduino Projects Sketchbook   https://steamtraininfo.com/arduino-projects

You may use this sketch and modify it as long as these credits remain in place and remain unmodified. */

#include <Servo.h>

//attach servo control wire to pin 2

#define cpyellow 3 // yellow light on control panel indication turnout in divergent position.
#define cpgreen 4 // greem light on control panel indication turnout in through position.
#define tfgreen 5 // gren light on turnout entrance indicating straight through passage.
#define tfyellow 6 // yellow light on turnout entrance indicating turnout is in the divergent position.
#define tbgreen 7 // green light on turnout exit indicating straight through passage.
#define tbred 8 // red light on turnout exit indicating turnout in divergent position.
#define divgreen 9 // green light on divergent leg
#define divred 10 // red light on divergent leg
#define warning 11 // a flashing spotlight warning that the upcoming turnout is in the divergent position.
#define toggle 12 // connect on off toggle swithc to this pin. May also use latching puch button

int pos;
Servo myservo;

// millis
int x = 1;
unsigned long t1 = 0;
unsigned long t2;

 

void setup()
{
myservo.attach(2);
pinMode(cpyellow, OUTPUT);
pinMode(cpgreen, OUTPUT);
pinMode(tfyellow, OUTPUT);
pinMode(tfgreen, OUTPUT);
pinMode(tbred, OUTPUT);
pinMode(tbgreen, OUTPUT);
pinMode(divred, OUTPUT);
pinMode(divgreen, OUTPUT);
pinMode(warning, OUTPUT);
pinMode(toggle, INPUT_PULLUP); /*attaching the toggle to pin 12 eliminates the need to add a
dropping resistor to the toggle.*/

digitalWrite(warning, 1);
pos = 0;
delay(50);

}

void loop()
{

if (digitalRead(toggle) == LOW) {
while (pos < 45) { // servo divergent angle. change as you wish.
pos += 1;
myservo.write(pos);
delay(50); // sets speed of servo. change as you wish.
}
}

else {
while (pos > 0) { //servo though position. change as you wish.
pos -= 1;
myservo.write(pos);
delay(50);

}
}

// LED Control
//Blinks warning LED
if (pos > 0)
{
t2 = millis();
if (t2 – t1 >= 1000) { // sets blink rate. Change as you wish.
x = 1 – x;
t1 = millis();
digitalWrite(warning, x);
}
}
// Turns off blinking warning LED
else {
digitalWrite(warning, LOW);

}
//Turns on LEDs when turnout is in divergent position
if (pos > 0) {
digitalWrite(tfyellow, HIGH);
digitalWrite(tfgreen, LOW);
digitalWrite(tbred, HIGH);
digitalWrite(tbgreen, LOW);
digitalWrite(divred, LOW);
digitalWrite(divgreen, HIGH);
digitalWrite(cpyellow, HIGH);
digitalWrite(cpgreen, LOW);
}
//Turns on LEDS when turnout is in divergent position
else {
digitalWrite(tfgreen, HIGH);
digitalWrite(tfyellow, LOW);
digitalWrite(tbgreen, HIGH);
digitalWrite(tbred, LOW);
digitalWrite(divred, HIGH);
digitalWrite(divgreen, LOW);
digitalWrite(cpgreen, HIGH);
digitalWrite(cpyellow, LOW);
}
}

[ End Code}

Fritzing Diagram

Notes

The servo may be powered from an external power supply.

Use an On/Off toggle or latching push button switch.

The led colors we use are in the code. Change them to suit your needs.

You can reduce the number of leds simply by not wiring them.

 

There are other turnout and servo control routines and a lot more in the Free Arduino Sketchbook .

 

 

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