Project tutorial
Keyboard Controlled Model Railway Layout V2.5 | PS/2 Inte...

Keyboard Controlled Model Railway Layout V2.5 | PS/2 Inte... © CC BY-NC-ND

Learn how to make a simple keyboard controlled model railway layout with the help of an Arduino microcontroller.

  • 338 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

Using Arduino microcontrollers, there are a lot of ways of controlling model railway layouts. A keyboard has a great advantage of having a lot of keys to add a lot of functions. Here let's see how can we start with a simple layout with locomotive and turnout control. This is an upgraded version of one of my previous projects. So, without further ado, let's get started!

Step 1: Watch the Video

Step 2: Get All the Stuff

For this project you will need:

  • An Arduino microcontroller board.
  • An Adafruit motor driver shield V2.
  • A female PS/2 connector(Get the one shown in the picture, it will make work easier.)
  • 4 male to male jumper wires(For connecting the female PS/2 connector to the Arduino board.)
  • 4 male to male jumper wires(2 for each turnout.)
  • 2 male to male jumper wires(For connecting the track power.)
  • 12-volt DC power source with a current capacity of at least 1A(1000 mA).
  • A PS/2 keyboard(USB one will not work!)
  • A suitable USB cable for programming the Arduino board.

Step 3: Program the Arduino Board and Plug on the Shield

The story

To install the library for Adafruit motor shield, goto Sketch>Include library>Manage libraries and search for Adafruit motor shield V2 library, install it and you're good to go.

Step 4: Identify the Pin Connections of the PS/2 Connector and Connect It to the Arduino Board

Using a multimeter set to continuity test and using the given picture as a reference, mark the pinouts of the PS/2 connector/extension cable wires and make the following wiring connections between the PS/2 connector and the Arduino board:

  • Connect the 'CLOCK' wire to pin D2.
  • Connect the 'DATA' wire to pin D3.
  • Connect the 'GND' wire to pin 'GND'.
  • Connect the '+5-volt/VCC' wire to +5-volt pin.

Step 5: Connect Wires to the Motor Output Terminals

Step 6: Set Up a Test Layout

Step 7: Connect the Motor Output Wires to the Turnouts and Track Power

Double-check all the wiring connections and make sure no wiring connections are loose.

Step 8: Connect the Keyboard to the PS/2 Connector

Step 9: Place the Locomotive and Some Rolling Stock on the Tracks

Step 10: Connect to Power and Turn It On

Step 11: Sit Back With Your Keyboard and Operate Your Layout

Step 12: Go Further!

There are a lot of buttons left on the keyboard. Go ahead and try adding more turnouts and functions to your layout. Whatever you do, don't forget to keep trying new stuff and happy model railroading!

Code

PS2_keyboard_controlled_model_railway_layout_with_turnouts.inoArduino
/*
 * Arduino program for controlling DC model railway layout with a PS/2 keyboard
   with the 
 * help of an Adafruit V2 motor driver shield.
 * Made by Kushagra, from Tech Build:https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 */
#include <PS2Keyboard.h>//Make sure this library is installed.
#include <Wire.h>
#include <Adafruit_MotorShield.h>//Make sure this library is also installed.

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_DCMotor *loco = AFMS.getMotor(1);
Adafruit_DCMotor *turnoutA = AFMS.getMotor(3);
Adafruit_DCMotor *turnoutB = AFMS.getMotor(4);


const int DataPin = 3;
const int IRQpin =  2;

PS2Keyboard keyboard;

int s=0,i;

void motor_go(){
 if(s>=1&&s<=255){
  loco->setSpeed(s);
  loco->run(FORWARD);
 }
 if(s<=-1&&s>=-255){
  loco->setSpeed(-s);
  loco->run(BACKWARD);
 }
 if(s==0){
  loco->setSpeed(s);
  loco->run(RELEASE);
 }
}

void motor_halt(int i){
  if(s>20){
    for(s=s;s>20;s--){
    motor_go();
    delay(i);
    }
 }
  if(s<=20&&s>0&&i<=60){
    for(s=s;s!=0;s--){
    motor_go();
    delay(60);
    }
  }
  if(s<-20){
    for(s=s;s!=-20;s++){
    motor_go();
    delay(i);
    }
  }
  if(s>=-20&&s<0){
    for(s=s;s!=0;s++){
    motor_go();
    delay(60);
    }
  }
}

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

void turnoutA_straight(){
turnoutA->setSpeed(255);
turnoutA->run(BACKWARD);
delay(100);
turnoutA->setSpeed(0);
turnoutA->run(RELEASE);
}

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

void turnoutB_straight(){
turnoutB->setSpeed(255);
turnoutB->run(BACKWARD);
delay(100);
turnoutB->setSpeed(0);
turnoutB->run(RELEASE);
}
void setup() {
// put your setup code here, to run once:
AFMS.begin();
keyboard.begin(DataPin, IRQpin);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(13,OUTPUT);

digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
}

void loop() {
// put your main code here, to run repeatedly:
if (keyboard.available()) {
  
  // read the next key
  char c = keyboard.read();
  
  // check for some of the special keys
  switch(c){
    case PS2_UPARROW:
    if(s<255){
      s++;
    }
    break;
  

    case PS2_DOWNARROW:
    if(s>-255){
      s--;
    }
      break;
    case PS2_TAB:
      if(s<20&&s>=0){
        for(s=s;s<20;s++){
          motor_go();
          delay(60);
        }
        break;
      }
      if(s==20){
        for(s=s;s!=0;s--){
          motor_go();
          delay(60);
        }
      }
      break; 
    

    case ' ':
    motor_halt(125);
    break;

    case PS2_ENTER:
    motor_halt(60);
    break;

    case '1':
    turnoutA_side();
    break;
 
    case '2':
    turnoutA_straight();
    break;

    case '3':
    turnoutB_side();
    break;
    
    case '4':
    turnoutB_straight();
    break;

    case '5':
    digitalWrite(13,HIGH);
    break;

    case '6':
    digitalWrite(13,LOW);
    break;
    

   Serial.println(i);
  }
  
}
  motor_go();
}

Comments

Similar projects you might like

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 1,268 views
  • 1 comment
  • 6 respects

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 2,940 views
  • 1 comment
  • 10 respects

Keyboard Controlled Model Train | PS/2 Interface

Project tutorial by Kushagra Keshari

  • 971 views
  • 1 comment
  • 10 respects

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 7,750 views
  • 10 comments
  • 38 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 1,919 views
  • 0 comments
  • 7 respects

Keyboard Melodies

Project tutorial by Ava Baker -21 and Natalie Clark -20

  • 2,256 views
  • 2 comments
  • 9 respects
Add projectSign up / Login