Kushagra Keshari
Published © CC BY-NC-ND

Simple Automated Model Railway Layout | Arduino Controlled

A simple and basic model railway layout controlled by an Arduino microcontroller.

BeginnerFull instructions provided1 hour24,546

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
Jumper wires (generic)
Jumper wires (generic)
×4
Male/Female Jumper Wires
Male/Female Jumper Wires
×6
Adafruit 12-volt 1A DC power supply
Use this for N-scale layouts which are small.
×1
Adafruit 12-volt 5A DC power supply
Use this for larger scales and/or larger layouts.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Simple_Automated_Oval_Layout.ino

Arduino
/*
 * Arduino program to control a model train running in a basic oval loop with the help of a feedback sensor.
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * 
 */

int s; //Integer variable to store train's speed in the range from 0 to 255.
int maxSpeed = 140;//Integer variable to store the maximum speed the train will reach.
int t = 5; //Time delay(pause, in seconds) between each loop of operation, from start to stop.

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

  delay(2000);
}

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

  for(s=0;s<=30;s++){ //Providing low voltage to the tracks to just turn on th locomotive's lights.
    motor_go();
    delay(10);
  }

  delay(4000);

  for(s=s;s<=50;s++){ //Starting the train.
    motor_go();
    delay(500);
  }

  delay(5000);

  for(s=s;s<=80;s++){ //Speeding up a bit.
    motor_go();
    delay(500);
  }

  delay(1000);

  while(digitalRead(A0)==LOW); //Wait for the train to cross the 'sensored' track.

  for(s=s;s<=maxSpeed;s++){ //Speeding up the train to maximum speed value set in the beginning.
    motor_go();
    delay(500);
  }

  delay(1000);
  while(digitalRead(A0)==LOW); //Wait for the train to cross the 'sensored' track.

  delay(2000);

  for(s=s;s!=100;s--){ //Slow down the train a bit.
    motor_go();
    delay(1000);
  }

  delay(4000);
  while(digitalRead(A0)==LOW); //Wait for the train to cross the 'sensored' track.

  for(s=s;s!=80;s--){ //Slow down the train furthur.
    motor_go();
    delay(500);
  }

  delay(4000);

  for(s=s;s!=60;s--){ //Slow down the train, preparing to stop.
    motor_go();
    delay(500);
  }
  delay(1000);
  while(digitalRead(A0)==LOW); //Wait for the train to cross the 'sensored' track.

  delay(1000);
  
  for(s=s;s!=20;s--){ //Reduce the voltage on the tracks to just let the locomotive lights turned on.
    motor_go();
    delay(250);
  }

  delay(1000);

  for(s=s;s!=0;s--){ //Stop the power supply to the track and power down the locomotive.
    motor_go();
    delay(125/2);
  }

  delay(1000*t); //Wait for the set time before starting all over again.
}

Credits

Kushagra Keshari
19 projects • 70 followers
A casual electronics and a model railway hobbyist.
Contact

Comments

Please log in or sign up to comment.