Project tutorial
Simple Automated Point to Point Model Railroad

Simple Automated Point to Point Model Railroad © CC BY-NC-ND

A basic point to point model railroad layout ideal for trams, light rail, powered cars, and metro trains.

  • 792 views
  • 0 comments
  • 5 respects

Components and supplies

About this project

Arduino microcontrollers are great to automate model railroad layouts. Automating layouts is useful for many purposes like putting your layout up at a display where the layout operation can be programmed to run trains in an automated sequence. The low cost and open source Arduino microcontrollers and the widespread community make it easy and simple to make projects and program them.

So, without further ado, let's get started!

Step 1: Watch the Video

Watching the video can help you get a fair idea of how this works.

Step 2: Get the Parts and Components

Here is what you will need:

  • An Arduino microcontroller board.
  • An L298N motor driver module.
  • 2 'sensored' tracks.
  • 6 male to jumper wires(A set of 3 wires each to connect the sensors pins to the Arduino board's digital I/O pins and power.)
  • 3 male to female jumper wires(To connect the motor driver's input pins to the Arduino board's digital I/O pins.)
  • 2 male to male jumper wires(To connect the motor driver to power and ground connection.)
  • 2 male to male jumper wires(To connect the motor driver's output terminals to power the track rails.)
  • A 12-volt power supply(Current capacity should be at least 1000mA or 1A for N-scale.)

Step 3: Program the Arduino Microcontroller

Get the Arduino IDE from here. You may need to make some adjustments to the Arduino code for your layout. You can find the Arduino program in the attachments.

Step 4: Make a Test Layout

To understand it better, check out the 4th step of the project from here and click on the picture to get detailed information.

Make a layout with bumper tracks at each end. The length of the mainline track between the stations can be made as long as required. Since the train slows down after crossing the 'sensored' track and continues to move for some distance, make sure there is a sufficient length of track between the 'sensored' tracks of each points A and B and their bumper tracks. The above picture might be useful for a reference.

Step 5: Make Wiring Connections for the Motor Driver

Make the following wiring connections:

  • Connect the motor driver's input pin 'IN3' to the Arduino board's digital output pin 'D8'.
  • Connect the motor driver's input pin 'IN4' to the Arduino board's digital output pin 'D9'.
  • Connect the motor driver's input pin 'ENB' to the Arduino board's digital output pin 'D10'.

Connect two male to male jumper wires to the terminals marked 'GND' and '+12-V' and connect them to the pins marked 'GND' and 'VIN' of the Arduino board respectively.

Connect two male to male jumper wires to the output terminals of the motor driver and connect them to the track rails through a power feeder track.

Step 6: Wire Up the Sensors to the Arduino Board

Connect both the sensors' 'VCC' and 'GND' pins of the sensors to the '+5-volt' and 'GND' pin of the Arduino board. You might need to get a bit creative to connect two 'VCC' connection jumpers to a single '5-volt' pin available on the Arduino UNO. Connect the 'OUT' pin of the sensor of station 'A' to the Arduino board's pin A0 and the remaining sensor's pin to the Arduino board's pin A1.

Step 7: Place a Test Locomotive on the Tracks

For testing purposes, place any locomotive or a powered car on the point 'A' of the layout from where the locomotive or the powered car will start.

Step 8: Connect the Setup to Power Source and Power It Up

Connect the Arduino board's power input connector to a 12-volt power source and turn it on.

Step 9: Watch Your Train Operate Autonomously

If everything was done correctly, you should see your test locomotive or powered car start from point 'A', speed up after crossing the first 'sensored' track it approaches, slow down and stop at point 'B' after crossing the second 'sensored' track, start again after a few seconds in the opposite direction, speed up after crossing the first 'sensored' track it approaches, and slow down and stop at point A after crossing the 'sensored' track installed near point 'A'. It will wait for a few seconds before starting the entire process again.

If the locomotive starts to move in the wrong direction, interchange the wires connected to the track power from the output of the motor driver.

Step 10: What's Next

Try tweaking the Arduino code to run the trains as per your wish, try adding more functions to the layout by combining my previous projects with this one. Whatever you do, all the best!

Code

Simple_automated_point_to_point_layout.inoArduino
/*
 * Arduino program to control a model train running a point to point layout with the help of feedback sensors.
 * 
 * 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 s;

int ta = 2;//Ineger variable to store time delay of the train between crossing the 'sensored' track and stopping at point A.

int tb = 2;//Ineger variable to store time delay of the train between crossing the 'sensored' track and stopping at point B.

int sa = 5;//Integer variable to store how long will the train stop at station A(in seconds).

int sb = 5;//Integer variable to store how long will the train stop at station B(in seconds).

int MidSpeed = 70;//Integer variable to store the train's initial speed(when leaving or arriving at the station).

int MaxSpeed = 110;//Integer variable to store the train's maximum speed(after leaving the station).


void motor_go(){
 if(s>=1&&s<=255){
  digitalWrite(9,LOW);
  digitalWrite(8,HIGH);
  analogWrite(10,s);
 }
 if(s<=-1&&s>=-255){
  digitalWrite(8,LOW);
  digitalWrite(9,HIGH);
  analogWrite(10,-s);
 }
 if(s==0){
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  analogWrite(10,s);
 }
}
void setup() {
  // put your setup code here, to run once:
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
}

void loop() {
      /*
       * The train will start from point A to proceed towards point B.
       */
      for(s=0;s<=30;s++){//Applying low-voltage across the track rails to make the train's lights turn on(This might not work for all trains!).
        motor_go();
        delay(60);
      }

      delay(1000);//Waiting for a second.

      for(s=s;s<=MidSpeed;s++){//Starting the train from the station at point A.
        motor_go();
        delay(125);
      }

      while(digitalRead(A0)!=HIGH);//The train will keep moving at the set 'MidSpeed' until crossing the 'sensored' track.

      for(s=s;s<=MaxSpeed;s++){//Increasing the speed of the train.
        motor_go();
        delay(125);
      }

      while(digitalRead(A1)!=HIGH);//The train will keep moving at the set 'MaxSpeed' until crossing the next 'sensored' track.

      for(s=s;s!=MidSpeed;s--){//Reducing the speed of the train.
        motor_go();
        delay(125);
      }

      delay(tb*1000);//Wait for set amount of time(in seconds) before stopping the train at the station at point B.

      for(s=s;s!=30;s--){//Bring it to a halt, keeping the lights turned on(This might not work for all trains!).
        motor_go();
        delay(125);
      }

      delay(1000);//Wait for a second.

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

      delay(5000);//Wait for set amount of time at the station before startinf again. 
      /*
       * The train will now start to go back to point A the same way it went from ppint A to B.
       */
      for(s=s;s!=-30;s--){
        motor_go();
        delay(60);
      }

      delay(1000);

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

      while(digitalRead(A1)!=HIGH);

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

      while(digitalRead(A0)!=HIGH);

      for(s=s;s<=-MidSpeed;s++){
        motor_go();
        delay(125);
      }

      delay(ta*1000);

      for(s=s;s<=-30;s++){
        motor_go();
        delay(125);
      }

      delay(1000);

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

      delay(5000);
      
}

Comments

Similar projects you might like

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 3,675 views
  • 1 comment
  • 11 respects

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 7,272 views
  • 10 comments
  • 37 respects

Keyboard Controlled Model Train | PS/2 Interface

Project tutorial by Kushagra Keshari

  • 887 views
  • 1 comment
  • 10 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 1,777 views
  • 0 comments
  • 6 respects

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 1,412 views
  • 1 comment
  • 6 respects

Arduino Simple "Camera Slider" Electronics

Project tutorial by Ian Cumming

  • 7,156 views
  • 3 comments
  • 18 respects
Add projectSign up / Login