Latest Post

Reversing Loop – Arduino controlled

A ‘reversing loop’ is a quite common feature in a model railway. What needs to be accomplished is to first switch the DCC-A and -B track power in the loop to the same polarity as the track where the train comes from … and later to switch it to the polarity of the track where it heads towards.

With a simple loop to and from the same turnout this can be done by switching 2 relays simultaneously with the turnout. With a less obvious ‘loop’ between two turnouts, like a diagonal connection inside an oval or a Y shape, it becomes less obvious when to reverse A and B. A method that always works is to use 4 sensors to monitor where the train is and switch the relays accordingly.

How it works

When a train enters the loop, sensor S1 detects it and the Arduino switches the relays such that inner rail = A and outer rail = B. Before the train leaves the loop, it reaches sensor S4 and the relays are switched to inner rail = B and outer rail = A. If the train enters the loop from the other side, the relays are switched via S3 and S2. Any kind of sensor can be used: current sensing, reed switches, optical sensors, or other.

(Click the image to enlarge.)


The hardware and wiring

The logic needed could be built with some transistors and resistors soldered onto a PCB. I prefer to use an Arduino though, because it’s almost no work to put together, it’s cheap, it looks quite neat, and because the logic now is in the software, which is super flexible in case you like to add or change something.

What we need is an Arduino Uno, a dual relay module and 4 sensors (I prefer reeds but any other sensor will do). Total costs for a reversing loop: around €5,-. It is perfectly possible to control more than one loop with the same Arduino, max 4 with high impedance or reed sensors (the S1,2 and S3,4 wires can be combined), max 2 otherwise. Just add the extra sensors and relays and add the code to control them.

(Click the image to enlarge.)


The code

Apart from the I/O declarations it’s just 2 lines of code. Is it realiy that simple? Yes, it’s amazingly simple: read the sensors and switch the relays is all that needs to be done.

Note1: The software assumes the sensors are open / high impedance when not activated, which is why the inputs are declared with an internal pull up resistor (INPUT_PULLUP). If your sensors have a hard 0V – 5V output, change the INPUT_PULLUP into INPUT (without pullup).

Note2: The software assumes sensors that are LOW when activated. If your sensors are HIGH when activated, delete the ‘!’ exclamation marks in the ‘!digitalRead(n)’ instructions.

// Ruud Boer, October 2018
// Control for Model Railway Reversing Loop
// 4 sensors are read, 2 relais switch the rails to DCC A/B or B/A

void setup() {
  pinMode(2,OUTPUT); // relay 1
  pinMode(3,OUTPUT); // relay 2
  pinMode(4,INPUT_PULLUP); // sensor 1
  pinMode(5,INPUT_PULLUP); // sensor 2
  pinMode(6,INPUT_PULLUP); // sensor 3
  pinMode(7,INPUT_PULLUP); // sensor 4

void loop() {
  if (!digitalRead(4) || !digitalRead(5)) {digitalWrite (2, LOW); digitalWrite (3, LOW);}
  if (!digitalRead(6) || !digitalRead(7)) {digitalWrite (2, HIGH); digitalWrite (3, HIGH);}


It works!

The video shows how the relays switch twice, when the train passes by while running inside the loop.





Welcome at this website / blog

This website / blog contains all kinds of info on the model railway hobby project I'm working on.

This homepage is where new posts appear when a major step has been made, or when I came across something noteworthy.

The other pages in the topmenu and the sidebar store information to be easily found back.

The railway hobby to me is more about electronics and (micro) computer control than about scenery. A lot can be found here on the use of Arduino as a DCC decoder and on Traincontroller PC layout control.

Who knows these pages may be helpful not only to myself, but also to other model train hobbyists..

There's also a Youtube Channel.