Project tutorial
Control Your Model Train Layout with a Keyboard

Control Your Model Train Layout with a Keyboard © CC BY-NC-ND

Now you can easily control your model railway layout using a keyboard!

  • 1,733 views
  • 1 comment
  • 6 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Adafruit motor shield(V1)
×1
12V 5A switching power supply
Use any 12-volt DC power source with a current capacity of at least 1A
×1
Adafruit industries ada62 image 75px
USB-A to B Cable
×1
11026 02
Jumper wires (generic)
2 each for two turnouts and track power
×6

Apps and online services

About this project

In one of my previous tutorials, I showed you how you can control your model train with your TV remote. In this tutorial, I will show you how to control a model train layout with a keyboard through a computer using Processing. So, let's get started!

Step 1: Get All the Stuff!

So, before starting with the build, make sure you have all of these;

Any Arduino board compatible with Adafruit motor driver shield(V1)

An Adafruit motor driver shield(V1)

A crosshead screwdriver

3 pairs of male to male jumper wires(2 pairs for the turnouts and the third one for the power feeder track)

A power feeder track

2 turnout tracks(you can add as many as you want by modifying the Arduino and processing code)

A 12 volt DC power supply(Current capacity of at least 1000mA or 1A)

A suitable USB cable to connect your Arduino board to a computer

An external keyboard (Optional)

Step 2: Get the Arduino Code and Upload It to Your Board

Make sure you have the Adafruit motor shield library installed in your computer.

Step 3: Plug on the Motor Driver Shield to Your Board

Make sure the pins of the motor driver board are aligned correctly with the headers of the Arduino board.

Step 4: Connect the Jumper Wires to the Motor Driver Shield

Step 5: Set Up Your Train Layout

Get your tracks and set up your model railway layout.

Step 6: Set Up Your Computer

Step 7: Connect the Power Feeder Track and the Turnout Switches to the Motor Driver Shield

Make sure no wiring connections are loose.

Step 8: Set Up the Locomotive and Rolling Stock on the Tracks

Use a rerailer to ensure all of the wheels of the locomotive and the rolling stock sit perfectly on the tracks to prevent derailments.

Step 9: Connect the Arduino Board to Power

Wait! before powering up your setup make sure there are no loose connections, faulty components or derailed rolling stock or locomotive(s).

Step 10: Connect the Arduino Board to the Computer Via USB

Step 11: Launch the Processing 3 IDE and Open the Code

Download the Processing IDE from here. Download the given code and open it in the IDE.

Step 12: Test Your Layout

Feel free to add more features to this layout by altering the codes, adding more turnouts and motor drivers. It's all up to you. I hope you will enjoy making this project. All the best for your build!

Code

Code to upload on the Arduino boardArduino
/*Arduino program for controlling model train layout with a keyboard via serial communication.
 *Watch the video here:https://www.youtube.com/watch?v=n-PcV4UAa4s
 *Tech Build:https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g
 *This program is expandable, feel free to modify it.
 *
 */
#include <AFMotor.h>
AF_DCMotor loco(1);//The terminal block pins to connect the power feeder track.
AF_DCMotor turnout1(3);//The terminal blocks to connect the turnouts.
AF_DCMotor turnout2(4);
int i=0;
int c;
int p=0;
void halt(){
  if(i>=30){
    loco.run(FORWARD);
    for(i=i;i!=30;i--){
      loco.setSpeed(i);
      delay(12);
    }
   }
   if(i<=-30){
    loco.run(BACKWARD);
    for(i=i;i!=-30;i++){
      loco.setSpeed(-i);
      delay(12);
    }
   }
}
void turn_off(){
    if(i>=0){
    for(i;i=0;i--){
      loco.setSpeed(i);
      delay(15);
    }
   }
   if(i<=0){
    for(i;i=0;i++){
      loco.setSpeed(-i);
      delay(15);
    }
   }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("START!");
  turnout1.setSpeed(500);
  turnout2.setSpeed(500);
  delay(500);
}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()){
  c=Serial.read();
  if(c == '1'&&i<=254){
    i=i+1;
  }
  if(c == '2'&&i>=-254){
    i=i-1;
  }
  if(c == '3'){
    turnout1.run(FORWARD);
    delay(50);
    turnout1.run(RELEASE);
  }
  if(c == '4'){
    turnout1.run(BACKWARD);
    delay(50);
    turnout1.run(RELEASE);
  }
  if(c == '5'){
    turnout2.run(FORWARD);
    delay(50);
    turnout2.run(RELEASE);
  }
  if(c == '6'){
    turnout2.run(BACKWARD);
    delay(50);
    turnout2.run(RELEASE);
  }
  if(c == '7'){
    halt();
    delay(1000);
    turn_off();
  }
  if(c == '8'){
   halt();
  }
   
}
   if(i>=0&&i<=254){
      loco.run(FORWARD);
      loco.setSpeed(i);
   }
   if(i<=0&&i>=-254){
      loco.run(BACKWARD);
      loco.setSpeed(-i);
   }
}
Code for control applicationJava
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import processing.serial.*;

Serial port = null;
String portname = null;
  
boolean streaming = false;
float speed = 1;
int i=0;

void openSerialPort(){
  if(portname == null) return;
  if(port != null) port.stop();
  
  port = new Serial(this, portname, 9600);
  
  port.bufferUntil('\n');
}
void selectSerialPort(){
  String result = (String) JOptionPane.showInputDialog(frame,
  "Select the serial port to which your Arduino board is connected to",
  "Select serial port",
  JOptionPane.QUESTION_MESSAGE,
  null,
  Serial.list(),
  0);
  if (result!= null) {
    portname = result;
    openSerialPort();
  }
}
void setup(){
  size(400, 300);
  openSerialPort();
}
void draw(){
  background(3);
  fill(200);
  int y = 24, dy = 16;
  text("USAGE INSTRUCTIONS", 100, y); y +=dy;
  text("----------------------------------", 100, y); y +=dy;
  text("P:  select the serial port", 12, y); y += dy;
  text("Up arrow key:  Move locomotive forward", 12, y); y += dy;
  text("Down arrow key:  Move locomotive backward", 12, y); y += dy;
  text("Enter key:  Stop the locomotive", 12, y); y += dy;
  text("Shift key:  Power down the locomotive", 12, y); y += dy;
  text("Enter:  Stop the locomotive", 12, y); y += dy;
  text("Q:  Switch turnout1 to straight", 12, y); y += dy;
  text("W:  Switch turnout1 to right", 12, y); y += dy;
  text("E:  Switch turnout2 to left", 12, y); y += dy;
  text("R:  Switch turnout2 to straight", 12, y); y += dy;
  y = height - dy;
  text("current serial port: " + portname, 12, y); y -= dy;
}
void keyPressed(){
  if(keyCode == UP) port.write("1");
  if(keyCode == DOWN) port.write("2");
  if(key == 'w') port.write("3");
  if(key == 'q') port.write("4");
  if(key == 'e') port.write("5");
  if(key == 'r') port.write("6");
  if(keyCode == SHIFT) port.write("7");
  if(key == ENTER) port.write("8");
  if(key == 'p') selectSerialPort();
  delay(10);
}

Comments

Similar projects you might like

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 2,091 views
  • 0 comments
  • 7 respects

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 8,655 views
  • 10 comments
  • 41 respects

Model Train Control

Project in progress by Masters

  • 6,588 views
  • 3 comments
  • 12 respects

Servo Control with TV Remote Control

Project showcase by eldo85

  • 5,649 views
  • 5 comments
  • 18 respects

Health Kit: Humidity and Temperature Control

Project tutorial by Diana Khalipina

  • 4,764 views
  • 2 comments
  • 41 respects
Add projectSign up / Login