Project tutorial
Automated Point to Point Model Railroad with Yard Siding

Automated Point to Point Model Railroad with Yard Siding © CC BY-NC-ND

An upgraded version of a previous project which now has a yard siding to house the train.

  • 1,418 views
  • 1 comment
  • 9 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Adafruit motor driver shield v2
×1
IR proximity sensor
Use appropriate sensors which can be used to make 'sensored' tracks for this project: https://www.instructables.com/id/Make-a-Low-Cost-Sensored-Track-in-Minutes/
×2
11026 02
Jumper wires (generic)
Two for connecting the turnout and the other two for the track power feeder to the outputs of the motor shield.
×4
Adafruit 12-volt DC power source
×1
826 04
Male/Female Jumper Wires
Two sets of these wires containing three of them are required to connect the sensors' power, ground, and signal connection to the Arduino microcontroller.
×6

Apps and online services

About this project

Arduino microcontrollers open great possibilities in model railroading, especially when it comes to automation. This project is an example of such an application. It is a continuation of one of the previous projects. This project comprises of a point to point model railroad layout with a yard siding to house a train. All of the operations are controlled by an Arduino microcontroller board with the help of feedback mechanism and the train and the turnout is controlled by an Adafruit motor shield.

Step 1: Watch the Video

Watch the video to get an idea of how this works. So, now you know what's going on, let's get started!

Step 2: Get All the Stuff

Here is what you will require for the build:

  • An Arduino board compatible with Adafruit motor shield v2.3.
  • An Adafruit motor shield v2.3.(Click here for more information.)
  • An expansion shield(Optional, recommended to expand the +5V and GND pins of the Arduino board in order to connect the sensors.)
  • 3 'sensored' tracks.
  • 4 male to male jumper wires(2 to connect track power and others to connect the turnout.)
  • 3 sets of 3 male to female jumper wires(A total of 9 wires used for connecting the 3 pins of each sensor to the Arduino board and power.)
  • A 12-volt DC power source with a current capacity of at least 1A(1000mA).
  • A suitable USB cable to connect the Arduino board to the computer.
  • A computer to program the Arduino microcontroller.
  • A screwdriver.

Step 3: Program the Arduino Microcontroller

Make sure you have the Adafruit motor shield library installed in your Arduino IDE. You can get full documentation about the motor shield and necessary software from this link.

Step 4: Make a Test Layout

Kato Unitrack is great for making temporary layouts, especially for testing purposes. Make a layout as shown in the above image. The track branching out is the yard siding. The point A is at the left end of the track and the point B is on the right end of the track in the mainline. The length of the track in the mainline(Between points A and B can be made of any length possible.) Make sure all the rail joints are properly made and the track rails are cleaned properly.

Step 5: Install the Motor Driver Shield on the Arduino Board and Connect It to Track Power and Turnout

Install the shield carefully on the Arduino board by aligning the pins of the shield with the headers of the Arduino board. Do it gently and make sure no pins of the shield get bent.

Connect the output pins of the shield marked as M4 to the track power wires and those marked as M3 to the turnout wires. Make note that the setup is compatible with only two wire solenoid type turnouts.

Step 6: Wire Up the 'sensored' Tracks

Install the expansion shield on the motor shield and connect the sensors' GND and power wires to GND and +5V rails of the shield respectively. Make the following pin connections:

  • Connect the output of the sensor in the yard to the Arduino board's pin A0.
  • Connect the output of the sensor in the point A track to the Arduino board's pin A1.
  • Connect the output of the sensor in the point B track to the Arduino board's pin A2.

Make sure no pins are loose to avoid malfunctioning of the system.

Step 7: Connect the Arduino Board to Power and Turn It On

Connect a 12V DC power source to the Arduino using a barrel jack and power it up.

Step 8: Place Your Locomotive in the Siding and Slide It Over the 'sensored' Track

The Arduino board is programmed to start the layout operation only after the locomotive has been placed in the yard and it gets to 'know' this only through the feedback from the 'sensored' track. Make sure you watch the video, in the first step, to understand it better.

After the 'sensored' track detects the locomotive, you should see that the turnout will switch to the siding if it is not and the locomotive will start to move forward.

If the turnout switches to a wrong direction, reverse the polarity of the wires connecting the turnout to the motor shield. Do the same for track power if the locomotive starts to move in the wrong direction.

Step 9: Watch Your Train Go!

If everything was done correctly, your train should start to move from the yard siding on the mainline and proceed as shown above.

Step 10: Done for Now?

Got your train running? Don't stop here! Try upgrading the setup to run the train between more points, change the acceleration and deceleration rate of the train, tweak with the Arduino code, there is a lot to do. All the best!

Code

Point_to_point_automated_model_railroad_with_yard_siding.inoArduino
/*
 * Arduino program for controlling a train in a point to pointlayout with a yard siding.
 * 
 * 'Sensored' tracks used for feedback:https://www.instructables.com/id/Make-a-Low-Cost-Sensored-Track-in-Minutes/
 * 
 * Made by Tech Build:https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g/
 * 
 */
#include<Wire.h>
#include<Adafruit_MotorShield.h>//Make sure this library is installed in your Arduino IDE.

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *loco = AFMS.getMotor(4);//Track power to be connected to the output of the motor shield marked M4.
Adafruit_DCMotor *turnout = AFMS.getMotor(3);//Turnout to be connected to the output of the motor shield marked M3.

int s, n;// Integer s for storing the speed and direction of the train. 
         // Integer n for storing the number of trips made by the train between points A and B.        
int m = 2;// Integer to determine the number of trips the train will make between points A and B.
int MaxSpeed = 80;// Integr to store the maximum speed at which the train will move in the mainline(0-255).

void loco_go(int i){// Custom function made to control the speed and direction of the locomotive based on the input integer and its sign(+/-).
 if(i>=1&&i<=255){
  loco->setSpeed(i);
  loco->run(FORWARD);
 }
 if(i<=-1&&i>=-255){
  loco->setSpeed(-i);
  loco->run(BACKWARD);
 }
 if(i==0){
  loco->setSpeed(i);
  loco->run(RELEASE);
 }
}

void turnout_side(){
turnout->setSpeed(255);
turnout->run(FORWARD);
delay(100);
turnout->setSpeed(0);
turnout->run(RELEASE);
}

void turnout_straight(){
turnout->setSpeed(255);
turnout->run(BACKWARD);
delay(100);
turnout->setSpeed(0);
turnout->run(RELEASE);
}
void setup() {
  // put your setup code here, to run once:

  AFMS.begin();

  pinMode(A0, INPUT);//Pin connected to the 'sensored' track in the yard.
  pinMode(A1, INPUT);//Pin connected to the 'sensored' track in the point A track.
  pinMode(A2, INPUT);//Pin connected to the 'sensored' track in the point B track.

  while(digitalRead(A0) != HIGH);//Wait until the train is positioned at the starting point.

  turnout_side();//Switch the turnout to connect the yard to the mainline.
}

void loop() {
  // put your main code here, to run repeatedly:

  for(s=0;s!=30;s++){//Powering up the locomotive in forward direction.
    loco_go(s);
    delay(60);
  }

  for(s=s;s!=MaxSpeed;s++){//Speeding up the locomotive.
    loco_go(s);
    delay(250);
  }

  while(digitalRead(A1) != HIGH);//Waiting for the locomotive to cross the 'sensored' track installed before point A.

  for(s=s;s!=30;s--){//Slowing down the locomotive.
    loco_go(s);
    delay(250);
  }

  turnout_straight();//Switching the turnout to connect the point B track to the mainline.

  for(s=s;s!=0;s--){//Turning off the locomotive.
    loco_go(s);
    delay(60);
  }

  delay(5000);//Delay(in milliseconds) for the locomotive to halt at point A.

 for(n=0;n!=m;n++){//Number of trips made by the train will be stored in 'n'.

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

  for(s=s;s!=-MaxSpeed;s--){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(A2) != HIGH);

  for(s=s;s!=-30;s++){
    loco_go(s);
    delay(250);
  }

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

  delay(5000);

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

  for(s=s;s!=MaxSpeed;s++){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(A1) != HIGH);

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

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

  delay(5000);
  
  }//End of the for() loop.

  /*
   * The trips made by the train between the points A and B is nnow over. The loocmotive is now at point A.
   */

  turnout_side();//Switching the turnout to connect the yard to the mainline.

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

  for(s=s;s!=-MaxSpeed;s--){
    loco_go(s);
    delay(250);
  }

  while(digitalRead(A0) != HIGH);

  for(s=s;s!=-30;s++){
    loco_go(s);
    delay(250);
  }

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

  delay(5000);//Delay after which the entire operation will repeat.

}

Comments

Similar projects you might like

Simple Automated Point to Point Model Railroad

Project tutorial by Kushagra Keshari

  • 1,286 views
  • 1 comment
  • 7 respects

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 8,317 views
  • 10 comments
  • 41 respects

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 4,835 views
  • 1 comment
  • 14 respects

Keyboard Controlled Model Train | PS/2 Interface

Project tutorial by Kushagra Keshari

  • 1,043 views
  • 1 comment
  • 10 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 2,033 views
  • 0 comments
  • 7 respects
Add projectSign up / Login