Automated Railway Reverse Loop

Automated Railway Reverse Loop © Apache-2.0

How to automate reversing tracks on your DC two-rail railway layout using Arduino.

  • 2,314 views
  • 3 comments
  • 22 respects

Components and supplies

Ard nano
Arduino Nano R3
×1
Infrared Module (Generic)
See my update for IR-module: https://youtu.be/Zixbz4O1xgM
×1
61pby065esl  sx679  tnr8syww5d
HC-05 Bluetooth Module
×1
Motor-Driver L298 (Generic)
×1
ULN2003 Stepper Motor Driver Board Module
Or chip ULN 2003 - an Array of seven NPN Darlington transistors
×1
ELECTRIC SWITCH MACHINE
Or any other Switch Machine
×1

Apps and online services

About this project

One of the mysteries that some railway modelers face is the "reverse loop." This is where you have a a section of the track that forms a complete loop so that the train will go round it and come back in the opposite direction on the same rails.

The major problem when trying to achieve this is the fact that the loop puts a short circuit on the track. This could be prevented by putting a break in each of the rails.

Train on the reverse loops are controlled by manual switches and/or manipulated throttle lever, the train operator must flip them in proper sequence. A moment of inattention results in a short circuit or derailment. This is especially likely (and embarrassing) when operating the layout for visitors.

For automate DCC railway control systems have the auto-reverse module. But there's nothing comfortable for DC control systems. This instruction is another attempt to create such device.

One direction entrance

There are several options for passing the reverse loop. My instructions provide for the following option – the train entering to loop in one direction only.

You can change the speed and direction of your train inside the loop and even stop it. The only limitation of this option is that your train must pass from point C to point A without changing the direction and the last car of the train must go this distance before the countdown time expires.

IR sensor

A way to modify the standard infrared module for Arduino is shown in the video. After this modification, the sensor trigger signal is inverted up to HIGH level.

IR sensor Module

Switch Machine

The ULN2003 chip is used to control the turnout, but you can also use a ready-made stepper motor control module. Connection method is exactly the same.

A classic switch machine for change direction the turnout is connected to the outputs of this chip. Some, especially old switch machines, will require an increase in voltage to 14 - 16 volts. To increase the voltage, you can use the DC-DC Voltage Step-Up Converter Module for Arduino.

Control system

This manual uses train control through the Arduino Train DEMO 2 mobile app.

If you are not mistaken when connecting the wires, then your result will be similar to that presented in the video.

Update 4/18/19

The free application has been updated to version 2. The application now supports the data transfer Protocol version 2.1, which means 9-step traction control.

Code

Reverse Loop Arduino
//  LOOP FOR APP DEMO //

// L298
#define L298_ENA 6
#define L298_IN1 7
#define L298_IN2 5
#define L298_IN3 4
#define L298_IN4 2
#define L298_ENB 3

// SWITCH MACHINE
#define STRAIGHT 9
#define BRANCH 10

// SENSOR
#define SENSOR 11

// VARIABLES //
bool stringComplete = false;
String inputString = ""; 
byte speedArray [] = {60, 80, 100, 120, 140, 170, 200, 230, 255};
byte speedTrain = 0;
bool directionForward = false, directionBackward = false;
bool sensorLatch = false;
bool flagTimer = false;
unsigned long millisLoop;

void setup() {

// Initializing Serial
  Serial.begin(9600);
  inputString.reserve(4); 

// Initializing Motor-Driver
  pinMode(L298_ENA, OUTPUT); 
  pinMode(L298_IN1, OUTPUT); 
  pinMode(L298_IN2, OUTPUT);
  pinMode(L298_IN3, OUTPUT); 
  pinMode(L298_IN4, OUTPUT); 
  pinMode(L298_ENB, OUTPUT);  

// Initializing Switch Machine 
  pinMode(STRAIGHT, OUTPUT); 
  pinMode(BRANCH, OUTPUT);

// Initializing Sensor 
  pinMode(SENSOR, INPUT); 

// SET TURNOUT TO DEFAULT POSITION  
  digitalWrite(STRAIGHT, HIGH);
  delay(600);
  digitalWrite(STRAIGHT, LOW);

}

void loop() {

// ---- START PARSING INCOMING APP COMMANDS
  if (stringComplete) {

    if (inputString.charAt(0) =='a') {

      // Speed 
      if (inputString.charAt(1) =='0') {
        if (inputString.charAt(2) =='0') speedTrain = 0;
        if (inputString.charAt(2) =='2') speedTrain = speedArray[0];
        if (inputString.charAt(2) =='4') speedTrain = speedArray[1];
        if (inputString.charAt(2) =='6') speedTrain = speedArray[2];
        if (inputString.charAt(2) =='8') speedTrain = speedArray[3];
      } 
      if (inputString.charAt(1) =='1') {
        if (inputString.charAt(2) =='0') speedTrain = speedArray[4];
        if (inputString.charAt(2) =='2') speedTrain = speedArray[5];
        if (inputString.charAt(2) =='4') speedTrain = speedArray[6];
        if (inputString.charAt(2) =='6') speedTrain = speedArray[7];
        if (inputString.charAt(2) =='8') speedTrain = speedArray[8];
      }

      // Direction and Stop
      if (inputString.charAt(1) =='d') {
        if (inputString.charAt(2) =='f') { // (f) Forward
          digitalWrite(L298_IN1, HIGH);
          digitalWrite(L298_IN2, LOW);
          directionForward = true;
          directionBackward = false;
        }
        if (inputString.charAt(2) =='b') { // (b) Backward
          digitalWrite(L298_IN1, LOW);
          digitalWrite(L298_IN2, HIGH);
          directionForward = false;
          directionBackward = true;          
        }
        if (inputString.charAt(2) =='s') { // (s) Stop button
          digitalWrite(L298_IN1, LOW);
          digitalWrite(L298_IN2, LOW);
          speedTrain = 0;
          directionForward = false;
          directionBackward = false;          
        } 
      }
      analogWrite(L298_ENA, speedTrain); // Throttle
      analogWrite(L298_ENB, speedTrain); // Throttle      
    }

    inputString = "";
    stringComplete = false;
  }

// ---- LOOP 
  // Train in sensor area
  if (digitalRead(SENSOR) == HIGH) {
    // To check STOP state
    if (directionForward != directionBackward) {
      directionForward = !directionForward;
      directionBackward = !directionBackward;
      // To change polarity of Main Line
      if (directionForward) {
        digitalWrite(L298_IN1, HIGH);
        digitalWrite(L298_IN2, LOW);        
      } 
      else {
        digitalWrite(L298_IN1, LOW);
        digitalWrite(L298_IN2, HIGH);          
      }
      // Switching turnout to branch
        digitalWrite(STRAIGHT, LOW);       
        digitalWrite(BRANCH, HIGH);         
    }
    sensorLatch = true;
  }  

  // The train left the sensor
  if (sensorLatch && digitalRead(SENSOR) == LOW) {
    millisLoop = millis();
    sensorLatch = false;
    flagTimer = true;
  }

  // Delay time is up (5000 - 5 sec.) 
  if (millis() > millisLoop + 5010) flagTimer = false;

  if ((millis() > millisLoop + 5000) && flagTimer) { 
    Serial.println("Delay time is up");
    // Switching turnout to straight
    digitalWrite(BRANCH, LOW);       
    digitalWrite(STRAIGHT, HIGH);  
  }      

  // To check STOP state
    if ((directionForward != directionBackward) && digitalRead(SENSOR) == LOW) {

    // To change polarity of Loop Line
    if (directionForward) {
      digitalWrite(L298_IN3, HIGH);
      digitalWrite(L298_IN4, LOW);        
    } 
    else {
      digitalWrite(L298_IN3, LOW);
      digitalWrite(L298_IN4, HIGH);          
    }
  }  
}


//// FUNCTIONS ////

void serialEvent() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == 'z') {
      stringComplete = true;
    }
  }
}

Schematics

Reverse Loop Circuit
Hacksterloop1 enqyq3sbqy

Comments

Similar projects you might like

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 7,272 views
  • 10 comments
  • 37 respects

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 3,333 views
  • 1 comment
  • 11 respects

Advance Automatic Railway Crossing System

Project showcase by Pawan singh and Akansh Saxena

  • 2,763 views
  • 1 comment
  • 13 respects

The URB Unit – Making Arduinos Network

Project tutorial by Steve Massikker

  • 4,948 views
  • 3 comments
  • 29 respects

Pill Me Please - The Automated Pill Packaging Machine!

Project tutorial by KevinZhangHK

  • 3,239 views
  • 0 comments
  • 20 respects

Automated Snake Enclosure with Camera

Project showcase by hagakure

  • 6,408 views
  • 9 comments
  • 17 respects
Add projectSign up / Login