Project tutorial
Model Railway Layout with Automated Passing Siding (V2.0)

Model Railway Layout with Automated Passing Siding (V2.0) © CC BY-NC-ND

An Arduino-controlled model railroad layout with a passing siding where the locomotive picks up and drops off rolling stock autonomously.

  • 37 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

This project is an update of one of the previous model railroad automation projects, The Model Railway Layout with Automated Siding. This version adds the feature of coupling and decoupling of the locomotive with the rolling stock. The operation of the railway layout is as follows:

  • The locomotive will start from the mainline and proceed into the siding to couple with the rolling stock.
  • The locomotive will couple and take the train out of the siding on to the mainline.
  • The train will start to move, speed up, take a couple of loops around the layout and slow down.
  • The locomotive will take the train back to the siding in the final loop where it will decouple from the rolling stock and proceed further.
  • The locomotive will make one loop around the track, slow down and stop where it started from in the beginning.
  • The locomotive will wait for a set amount of time and the whole operation will be repeated again.

So without further ado, let's get started!

Step 1: Watch the Video

Watch the video to get a complete idea of how the entire railroad operation explained in the previous step takes place.

Step 2: Get All the Parts and Components

So now you know how stuff is going to go, so get all of the parts and components listed below to get started!

  • An Arduino microcontroller(Any Arduino board can be used but take care of pin connections.)
  • An L298N motor driver module(This type of motor driver is recommended, regarding its capacity and price.)
  • 5 male to female jumper wires(To connect the motor driver's input pins to the digital output pins of the Arduino board.)
  • Set of 3 male to female jumper wires, a total of 6(To connect the sensors to the Arduino board.)
  • 6 breadboard jumper wires(Two to connect track power to one output of the motor driver and four to connect two turnouts of the siding to the other output of the motor driver.)
  • Two 'sensored' tracks.
  • A 12-volt power supply(Current capacity of at least 1A.)
  • An appropriate USB cable to connect the Arduino board to a computer(For programming).
  • A computer(Obviously : )
  • Tracks to make the layout.

Step 3: Upload the Arduino Program to the Arduino Microcontroller

Get the Arduino IDE from here. Go through the code to understand how the operation will work.

Step 4: Make the Layout

The layout will contain a passing siding with a magnetic uncoupler track at the exit of the siding to let the locomotive uncouple from the rolling stock before leaving the siding. A 'sensored' track will be installed just after the siding to let the microcontroller know when the locomotive leaves the siding or crosses that particular section of the track.

Another 'sensored' track will be installed before the siding such that the length of track between this 'sensored' track and the siding with respect to the train's direction of movement is greater than the length of the train.

After setting up the layout, make sure the track rails are clean to ensure the smooth running of the train.

Step 5: Connect the Turnouts to the Motor Driver

Connect both the turnouts in parallel(+ve and -ve of one to the +ve and -ve of the other respectively). Connect the parallel wired turnouts to the output pins of the motor driver module marked 'OUT1' and 'OUT2'. You might need to reverse the turnout connection to the motor driver's output if they switch to the wrong direction after powering up the setup.

Step 6: Connect the Motor Driver to the Track Power Feeder

Connect the track power feeder's wires to the output pins of the motor driver marked 'OUT3' and 'OUT4'. You might need to reverse the polarity of the wiring connection if the locomotive starts to move in the wrong direction after powering up the setup.

Step 7: Connect the Motor Driver to the Arduino Board

Remove the jumper connector from the pin of the motor driver marked 'ENB'. Connect the '+12-V' terminal of the motor driver module to the 'VIN' pin of the Arduino board. Connect the 'GND' pin of the motor driver module to the 'GND' pin of the Arduino board. Make the following connections between the motor driver and the Arduino board:

Motor driver -> Arduino board

IN1 -> D12

IN2 -> D11

IN3 -> D9

IN4 -> D8

ENB -> D10

Step 8: Connect the 'sensored' Tracks to the Arduino Board

Connect the 'VCC' pins of the sensors to the '+5-volt' pin of the Arduino board. Connect the 'GND' pins of the sensors to the 'GND' pin of the Arduino board.

Connect the 'OUT' pin of the sensor at the exit of the siding to the pin 'A1' of the Arduino board. Connect the 'OUT' pin of the remaining sensor to the pin 'A0' of the Arduino board.

Step 9: Connect the Arduino Board to Power

Connect the Arduino board to a 12-volt DC power source through the power jack.

Step 10: Place the Rolling Stock and the Locomotive on the Tracks

Using a rerailing tool, position the locomotive on the mainline and the rolling stock in the siding.

Step 11: Check All the Wiring Connections and the Trains

Make sure the locomotive and the rolling stock are not derailed. Double-check all of the wiring connections and take care of the polarity of the power connections.

Step 12: Turn on the Power and Get the Train Running

If everything went well, you should see your locomotive starting to move and run as in the video. If the locomotive starts to move in the wrong direction or the turnouts switch in the wrong direction, reverse the polarity of their wiring connection with the output terminal of the motor driver module.

Step 13: Modify the Project

Go ahead and tinker with the Arduino code and the design to add more functions, run more trains, add more turnouts and so on. Whatever you do, all the best!

Code

Model_train_layout_with_automated_passing_siding_V2.0.inoArduino
/*
 * Arduino program to control a basic model train layout with a passing siding with the help of optical feedback mechanism.
 * 
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * 
 * Feel free to tinker with the code and customize it for your own layout. :)
 */

int sensor1 = A0;//Pin connected to the first sensor, this sensor will be installed before 
                   //the passing siding, taking the direction of the train into account.

int sensor2 = A1;//Pin connected to the second sensore, this sensor will be installed after
                   //passing siding, taking the direction of the train into account.

int s = 0; //Integer to store the locomotive's speed at a scale from 0 to 255.

int motor_go(int i){//Custom function made to control the speed and direction of the locomotive based on the ineger value and sign(+ve or-ve).
 if(i>=1&&i<=255){
  digitalWrite(9,LOW);
  digitalWrite(8,HIGH);
  analogWrite(10,i);
 }
 if(i<=-1&&i>=-255){
  digitalWrite(8,LOW);
  digitalWrite(9,HIGH);
  analogWrite(10,-i);
 }
 if(i==0){
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  analogWrite(10,i);
 }
}

void switch_to_pass(){//Custom fuction made to switch the turnouts to the passing siding.
  digitalWrite(11,LOW);
  digitalWrite(12,HIGH);  
  delay(200);
  digitalWrite(12,LOW);
}

void switch_to_main(){//Custom function made to switch the turnouts to the mainline.
  digitalWrite(12,LOW);
  digitalWrite(11,HIGH);    
  delay(200);
  digitalWrite(11,LOW);

}
void setup() {
  pinMode(sensor1,INPUT);
  pinMode(sensor2,INPUT);
  
  pinMode(8,OUTPUT);//Pins 8 and 9 are for direction control of the locomotive.
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);//Pin 10 is a PWM pin on the Arduino UNO and is connected to the 'ENB' pin of the motor driver.
  pinMode(11,OUTPUT);//Pns 11 and 12 are for turnout control.
  pinMode(12,OUTPUT);
  
}

void loop() {
     
     switch_to_main();//Switching the turnouts to the minline.

     for(s=0;s<=30;s++){//Starting the locomotive.
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s<=90;s++){
      motor_go(s);
      delay(125);
     }
    
     while(digitalRead(A1)!=HIGH);//Wait till the locomotive reaches the second sensor.

     for(s=s;s!=30;s--){//Stop the locomotive.
      motor_go(s);
      delay(125);
     }

     delay(2000);

     for(s=s;s!=0;s--){
      motor_go(s);
      delay(60);
     }

     delay(1000);

     switch_to_pass();
      
     for(s=s;s!=-30;s--){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=-90;s--){
      motor_go(s);
      delay(125);
     }

     while(digitalRead(A0)!=HIGH);//Wait till the train reaches the first sensor.

     for(s=s;s!=-30;s++){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=0;s++){
      motor_go(s);
      delay(60);
     }

     switch_to_main();
     //Switching the turnouts to the mainline.

     for(s=s;s!=30;s++){
      motor_go(s);
      delay(60);
     }

     delay(2000);

     for(s=s;s!=90;s++){
      motor_go(s);
      delay(125);
     }

     while(digitalRead(A1)!=HIGH);

     for(s=s;s!=110;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);

     for(s=s;s!=180;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);//Two while statements for checking train's position mean
     while(digitalRead(A0)!=HIGH);//the train will cover one loop.

     while(digitalRead(A1)!=HIGH);//The above set of statements is repeated again to make the
     while(digitalRead(A0)!=HIGH);//train cover another loop.

     for(s=s;s!=110;s--){
      motor_go(s);
      delay(250);
     }     

     while(digitalRead(A0)!=HIGH);

     switch_to_pass();


     for(s=s;s!=90;s--){
      motor_go(s);
      delay(250);
     }

     delay(5000);

     for(s=s;s!=70;s--){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A1)!=HIGH);


     for(s=s;s!=110;s++){
      motor_go(s);
      delay(250);
     }

     while(digitalRead(A0)!=HIGH);

     switch_to_main();

     delay(2000);

     for(s=s;s!=80;s--){
      motor_go(s);
      delay(250);
     }

     delay(6000);
     
     for(s=s;s!=30;s--){
      motor_go(s);
      delay(250);
     }

     delay(2000);

     for(s=s;s!=0;s--){
      motor_go(s);
      delay(60);
     }

     delay(5000);
          
}

Comments

Similar projects you might like

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 7,787 views
  • 10 comments
  • 38 respects

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 4,221 views
  • 1 comment
  • 14 respects

Simple Automated Point to Point Model Railroad

Project tutorial by Kushagra Keshari

  • 1,070 views
  • 1 comment
  • 7 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 1,924 views
  • 0 comments
  • 7 respects

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 1,621 views
  • 1 comment
  • 6 respects

Keyboard Controlled Model Train | PS/2 Interface

Project tutorial by Kushagra Keshari

  • 979 views
  • 1 comment
  • 10 respects
Add projectSign up / Login