You're reading ...
Arduino, Software

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.)

Rev_Loop_txt

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.)

Schematic

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.

 

 

 

About RudyB

Hobbyist

Discussion

19 thoughts on “Reversing Loop – Arduino controlled

  1. I´m a novice in arduino, so I´m trying new experiences continuosly… I’ve been analyzing your project and haven’t found where the arduino feed comes in. Can you explain where? Tks

    Like

    Posted by Nilton Torquato | August 24, 2019, 14:47
  2. Hi Rudy !!! Great work !!!
    I found your project very relevant.
    But I would like to implement an automatic servo drive in turnout. It’s possible ? ?

    Like

    Posted by Demarques Ribeiro da Silva | April 11, 2019, 14:39
    • Of course that is possible. I assume by ‘automatic’ you mean by DCC control? If can wait a couple of weeks, I have planned videos on both a DCC accessory decoder as well as a servo decoder. If you can’t wait, go to rudysmodelrailway.wordpress.com and navigate to the software page, there you candownload a zip file with all knds of Arduino DCC decoders. There’s also a drawing of the hardware you need to connect the DCC to the Arduino. Alternatively you can download software with has added extensive configuration options via the PC from the arcomora.com website.

      Like

      Posted by RudyB | April 11, 2019, 15:22
      • I appreciate your attention.
        Yes I understand a little of the DCC system. And I can wait for whatever is necessary.
        I’d like to know how I can leverage your source code and implement the turnout servo drive automatically when I detect the locomotive. Can you develop and send it to me?

        Thank you very much

        Like

        Posted by Demarques Ribeiro da Silva | April 12, 2019, 20:34
      • OK, I understand that you like to switch the turnout based on the train sensor? Most easy is to always rum through the loop in the same direction. In that case you need only two sensors.

        SENSOR_A should be placed in front of the turnout that enters the loop. This sensor switches the two relays and sets the turnout to ‘straight’.
        if(digitalRead(SENSOR_A) == 0) {
        digitalWrite(RELAY_A_PIN, HIGH);
        digitalWrite(RELAY_B_PIN, LOW);
        setpoint = ANGLE_STRAIGHT;

        SENSOR_B is placed in the loop, in front of the turnout where the train leaves the loop. We now need to switch both relays back and set the turnout to ‘bend’.
        if(digitalRead(SENSOR_A) == 0) {
        digitalWrite(RELAY_A_PIN, LOW);
        digitalWrite(RELAY_B_PIN, HIGH);
        setpoint = ANGLE_BEND;

        That’s all. On how to control the servo with the variable setpoint, see video 18 or 26.

        Like

        Posted by RudyB | April 15, 2019, 08:46
  3. Hi Ruud,

    Thanks for sharing your sketch.
    I have modified it for use on my lay-out. I intend to use only two (IR) sensors and one DP relay.
    When the train passes over one or both of the sensors, the relay will be energized, setting the polarity correct for entrance. When the train is passed, the relay will be down, setting the polarity correct for leaving .

    Also i added a delay in order to be shure that a reading error (maybe cuased by a small coupler) will not directly turn down the relay. The period can be adjusted.

    A test on the testbench is promising.

    Kind regards,
    Juroen

    // Juroen january 2019
    // based on sketch by Ruud Boer, October 2018
    // Control for Model Railway Reversing Loop
    // 2 sensors are read, 1 DP relay switchews the rails A/B or B/A

    long previousMillis = 0;
    const unsigned long period = 350; //period during which relay will be up after passing the train, also to compensate for small couplers

    void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(2,OUTPUT); // relay 1 Double pole
    pinMode(4,INPUT_PULLUP); // sensor 1 outside of rverse loop
    pinMode(5,INPUT_PULLUP); // sensor 2 inside of reverse loop
    }

    void loop()
    {
    unsigned long currentMillis = millis();
    if (!digitalRead(4) || !digitalRead(5))
    {
    previousMillis = currentMillis; // Remember the time
    digitalWrite (2, LOW);
    digitalWrite (LED_BUILTIN, HIGH);
    }
    else
    {
    if
    (currentMillis – previousMillis >= period)
    {
    digitalWrite (2, HIGH);
    digitalWrite (LED_BUILTIN, LOW);
    }
    }
    }

    Like

    Posted by Juroen | January 16, 2019, 20:19
    • you can do it like that if you always enter at the same side. The reason I used 4 sensors is to make it work no matter what side you enter and it also works also in case of a power down. At power up, the situation is not clear and when a train is inside the loop and leaves, you can get a short circuit … but not with 4 sensors.

      Like

      Posted by RudyB | January 16, 2019, 22:08
      • Ruud,

        I think it will work in both directions.

        The two sensors I use are both at one end of the reverse loop. E.g. in your drawing that would be sensor 1 and sensor 2. If one of them ‘sees’ a train the relay will be energized for entering or leaving the loop at that end.

        The relay is normal de-energized. In that situation the polarity is set for leaving/entering the loop on the other side.

        That is why I use an if – else statement.

        However, if it does not work I will keep you informed!
        And thanks again for sharing your experiences on your website.

        Regards, Juroen

        Like

        Posted by Juroen | January 17, 2019, 07:14
      • What if you power on while there already is a train in the loop?

        Like

        Posted by RudyB | January 17, 2019, 08:04
      • Hi Ruud,

        I have also a picture, based on your picture in order to explain how it should work. Currently I see no option to share pictures in this discussion. Is there a way to share the picture with you?

        Regards, Juroen

        Like

        Posted by Juroen | January 17, 2019, 07:40
  4. I’m glad that I found your solution. I am curious as to the use of the reed contacts as opposed to IR sensors. How would I place them on my N Scale track and do I have to modify my locos?

    Like

    Posted by MARK NOACK | November 15, 2018, 20:35
    • The reeds can be placed in two ways:
      – horizontal in the direction of travel, maybe need to make a cut in some sleepers to make room such that it slays low enough.
      – vertical … just drill a hole, bend one wire 180 degrees and place the reed in the hole.
      You’ll need small and strong neomybium magnets, of which plenty sizes and shapes are available at Aliexpress.

      Like

      Posted by RudyB | November 15, 2018, 21:23
  5. Hi Rudy,

    I was wondering how this reversing loop control could be adapted for a reversing triangle (which in effect is another form of reversing loop. Your thoughts would be welcomed.

    Thanks

    Tom

    Like

    Posted by Tom Busza | November 12, 2018, 13:54
    • Examine your layout closely and mark the junction where the A-rail connects to the B-rail. Go back to the junction where you came from. Mount the the 4 sensors, 2 at each side, on rail in between and all should be well.

      Like

      Posted by RudyB | November 27, 2018, 10:55
  6. Hi Rudy !!! Great work !!!

    My mind in going on from your post reading. I think it could be posible to resolve the loop just with two sensors and two relays. I expose this draw https://photos.app.goo.gl/NWWWqAQDrePCSycn7 to share opinions and suggestions. May be some detail has been forgotten by myself.

    Like

    Posted by Santa Cecilia Término | November 8, 2018, 12:41
  7. G’day Rudy,

    Yet another Gem!

    Many thanks for sharing.

    Cheers
    Phil

    Like

    Posted by Phillip | November 3, 2018, 17:39

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Archive of all posts

Categories

%d bloggers like this: