Project tutorial
Automatic Train Control

Automatic Train Control © Apache-2.0

Get the trains running on time. Make your schedule. Any Arduino boards and your model railway set.

  • 997 views
  • 0 comments
  • 9 respects

Components and supplies

11026 02
Jumper wires (generic)
×1
A000066 iso both
Arduino UNO & Genuino UNO
You can use ANY Arduino board (UNO, NANO, MEGA and etc)
×1
09670 01
SparkFun Dual H-Bridge motor drivers L298
I recommend using a regular L298 motor driver module, as in the picture of the circuit
×1

Apps and online services

About this project

This very simple scheme allows you to quickly get the train on your schedule. You can adjust the movement time and the train stop time. Train acceleration and braking are also configurable. By changing the schedule parameters in the sketch, you can create any type of automatic movement. Just turn on the power and your train leaves to way.

Changing the time parameters in the sketch:

// 1  | 0 > Time < 5 sec
if (counterScheduler <= 5) { 
......
// 2  | 10 sec > Time < 15 sec
if ((counterScheduler >= 10) && (counterScheduler <= 15)) { 
......
// 3  | Change direction
if (counterScheduler == 16) {
.... etc.  

creates a timetable.

Variables:

int brakingDelta = 5;
 int accelerateDelta = 6;

define the parameters of movement the train

The direction of the train is determined by the state of the pins D6 and D7:

// Set default direction to FORWARD
 digitalWrite(L298_IN1, HIGH);
 digitalWrite(L298_IN2, LOW);  

You can endlessly change this sketch to get new options for automatic train movement.

Welcome aboard!

Code

Swing Arduino
// L298 
#define L298_ENA 5
#define L298_IN1 6
#define L298_IN2 7


// SCRIPTS VARIABLES
int counterScheduler;
unsigned long timerScheduler = 0;
unsigned long timerLocal = 0;
byte speedAuto = 0;


void setup() {

// Initializing pins
  pinMode(L298_ENA, OUTPUT);
  pinMode(L298_IN1, OUTPUT);
  pinMode(L298_IN2, OUTPUT);

// Set default direction to FORWARD
  digitalWrite(L298_IN1, HIGH);
  digitalWrite(L298_IN2, LOW); 

}

void loop() {

  	// Start Scheduler
    if (millis() > (timerScheduler + 1000)) {  // Tick every 1 sec
      counterScheduler++; 
      timerScheduler = millis();
    }  
    
    // ------------- SCRIPT SWING
    int brakingDelta = 5;
    int accelerateDelta = 6;

    // 1  | 0 > Time < 5 sec
    if (counterScheduler <= 5) {  
        // Start train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta;
          else speedAuto = 255;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        }   
    }       
    
    // 2  | 10 sec > Time < 15 sec
    if ((counterScheduler >= 10) && (counterScheduler <= 15)) {  // Stop train after 10 sec
        // Stop train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto > 30) speedAuto = speedAuto - brakingDelta;
          else speedAuto = 0;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }  
    
    // 3  | Change direction
    if (counterScheduler == 16) {  
        digitalWrite(L298_IN1, LOW);
        digitalWrite(L298_IN2, HIGH); 
    }   
    
    // 4  | 20 sec > Time < 30 sec
    if ((counterScheduler >= 20) && (counterScheduler <= 30)) {  
        // Start train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto < 240) speedAuto = speedAuto + accelerateDelta;
          else speedAuto = 255;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }       
    
    // 5  | 31 sec > Time < 40 sec
    if ((counterScheduler >= 31) && (counterScheduler <= 40)) {  // Stop train
        // Stop train
        if (millis() > (timerLocal + 100)) {
          if (speedAuto > 30) speedAuto = speedAuto - brakingDelta;
          else speedAuto = 0;
          analogWrite(L298_ENA, speedAuto); 
          timerLocal = millis();
        } 
    }    
    
    // 6  | Return to Step 1
    if (counterScheduler > 40) {
        counterScheduler = 0;   
        digitalWrite(L298_IN1, HIGH);
        digitalWrite(L298_IN2, LOW); 
  	}
}

Schematics

Connections to Rails
Simplestart1 t6lqwifidk
Circuit
Simplestart2 dcfgepa8og

Comments

Similar projects you might like

Servo Control with TV Remote Control

Project showcase by eldo85

  • 12,627 views
  • 6 comments
  • 34 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 46,526 views
  • 13 comments
  • 44 respects

Automatic Fan Control System

Project tutorial by Muhammad Sheraz

  • 20,757 views
  • 1 comment
  • 13 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 2,378 views
  • 0 comments
  • 8 respects

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 2,015 views
  • 1 comment
  • 6 respects
Add projectSign up / Login