In this project, we will be controlling three-wire turnouts (I use Bachmann) with Arduino dual-relays. Relays are essentially switches that can handle high voltages that the Arduino cannot. This can be useful if you want to automate the process of switching turnouts, for things like detecting when locomotives enter a specific isolated area.
Arduino (Mega/Uno)
Dual 5v Relays
AC Transformer (Or DC if that's what's needed to control your turnout)
Bachmann Turnout (Or any other turnout with three wires coming out to control it) Check the First Image Here
Terminal Buses/Strips to Connect Multiple Wires To
Extra Male to Male Wires
Relays are switches to control high AC or DC voltages that an Arduino cannot output. In this case, we will be using high voltages from a transformer to control a turnout. The relay has a DC+ port (5v), DC- Port (GND), IN1 Port (Digital Pin 5), and an IN2 Port (Digital Pin 6) on the Arduino side. On the other side, there is a COM, NC, and NO port for each relay. COM is the voltage that you want to control with the relay. In this case, it will be positive AC voltage. NC is the normally closed port. When a circuit is closed, it runs and functions correctly. NO is the normally open port. When a circuit is open, it doesn't work. When the Arduino is not controlling the relay through the IN1 and IN2 pins, NC is closed, making the circuit connect the COM port to the NC port, acting as a switch. The circuit between the COM port and the NO port is open, which makes the circuit not run. When the Arduino does control the relay through the input pins, the circuit between the NO port is closed, which allows voltage to travel through the circuit, and vice versa with the NC port. Check out this tutorial for a better explanation of relays.
DC+ on Relay → 5v on Arduino
DC- on Relay → GND on Arduino
IN1 on Relay → Digital Pin 5 on Arduino
IN2 on Relay → Digital Pin 6 on Arduino
Connect the positive from the AC Transformer to a terminal bus/strip. From the bus, connect two wires to the COM ports of the relay. You can also do it as shown in the second picture below.
Connect the NO ports to each side of the turnout wires shown in the third image below.
Connect the negative from the AC Transformer to the COM port of the turnout wire, again shown in the third image below.
Upload the code shown here to the Arduino. You can also visit this website to detect when a DC train is in an isolated area, and automate the relay process then. For DCC locomotives, visit here.
A Bachmann turnout comes with this green wire. Clip off the adapter and strip the ends to create this. Here, you can attach the wires more easily.
Connect the positive from the transformer to two wires connecting to the COM ports of the relays.
Attach the negative wire from the transformer to the COM (middle) wire, and the two NO wires to the two other wires shown here.
void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
while(Serial.available() == 0){
}
String input = Serial.readString();
input.trim();
Serial.println(input);
if(input == "turn") {
digitalWrite(5, HIGH);
Serial.println("Turning...");
delay(500);
digitalWrite(5, LOW);
} else if (input == "straight") {
digitalWrite(6, HIGH);
Serial.println("Going Straight...");
delay(500);
digitalWrite(6, LOW);
}
}
The turnout uses solenoids to switch from straight to turning. Activating these with the relay for more than a second can burn them out, making them not function. Be sure to keep the delays in the code to 500, no more than that.
If this was a bit confusing, feel free to visit my YouTube channel and watch the video tutorial for this when it comes out in a couple days.