Project tutorial
Keyboard Controlled Model Train | PS/2 Interface

Keyboard Controlled Model Train | PS/2 Interface © CC BY-NC-ND

A variation of the keyboard controlled model train, controlled by a PS/2 keyboard.

  • 474 views
  • 1 comment
  • 10 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
11026 02
Jumper wires (generic)
Always keep some extra, they might be needed.
×8
826 04
Male/Female Jumper Wires
×3
Adafruit 12V DC 1000mA (1A) regulated switching power adapter
Use this for small N-scale layouts.
×1
Adafruit 12V 5A switching power supply
Use this for larger scales and/or larger layouts.
×1
Female PS/2 connector
Use a connector like this one. You might find one for cheap from a computer repair shop.
×1
PS/2 keyboard
You can use almost any keyboard as long as it follows the set standards and works.
×1
L298N Dual H Bridge
×1

About this project

In one of my previous tutorial, I showed you how to control a model railway layout using a keyboard. It did great but had a drawback of requiring a computer to operate. In this tutorial, let's see how to control a model train using a keyboard through Arduino. So, without further ado, let's get started.

Step 1: Watch the Video

Step 2: Get All the Required Stuff

For this project you will need:

  • An Arduino microcontroller
  • A PS/2 keyboard
  • A female PS/2 connector(Get the one as shown in the picture, it will make your life easier.)
  • An L298N motor driver module
  • A 12-volt DC power source with a current capacity of at least 1A(1000mA).
  • 3 male to female jumper wires(To connect the motor driver's inputs to the Arduino board's output pins.)
  • 4 male to male jumper wires(To connect the motor driver to power and the tracks.)
  • 4 male to male jumper wires(To connect the PS/2 connector to the Arduino board.)

Step 3: Program the Arduino Microcontroller

Step 4: Identify the Pins of the PS/2 Connector

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.

Step 5: Connect the Motor Driver to the Arduino Board

Make the following wiring connections:

  • Connect the input pin 'ENB' to pin 'D10' of the Arduino board.
  • Connect the input pin 'IN4' to pin 'D9' of the Arduino board.
  • Connect the input pin 'IN3' to pin 'D8' of the Arduino board.
  • Connect two male to male jumper wires to the output terminals 3 and 4 to be later connected to the track power feeder.
  • Connect the 'VIN' pin of the motor driver to the 'VIN' pin and the 'GND' pin to the 'GND' pin of the Arduino board respectively.

Make sure no wiring connections are loose.

Step 6: Connect the PS/2 Connector to the Arduino Board

Make the following wiring connections:

  • Connect 'VCC' to the '+5-volt' pin of the Arduino board.
  • The storyo board.
  • Connect 'CLOCK' to pin 'D2' of the Arduino board.
  • Connect 'DATA' to pin 'D3' of the Arduino board.

Double check the pinout diagram of the PS/2 connector before making connections.

Step 7: Set Up a Test Layout

Make a simple loop of track to test the setup. Make sure the tracks are cleaned properly to prevent the locomotive from stalling.

Step 8: Connect the Motor Driver's Outputs to the Track Power Feeder

Connect the male to male jumper wires to the power feeder track terminals previously connected to the output terminals of the motor driver.

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

Step 10: Place the Locomotive on the Tracks

Place the locomotive on the tracks, aligning the wheels properly with the rails.

Step 11: Connect the Setup to 12-volt Adapter and Power It On

Double-check all of the wiring connections and make sure everything is connected to the right place and no wiring connections are loose. Plug in the power supply adapter and turn it on.

Step 12: Sit Back With You Keyboard and Run Your Train

Step 13: What's Next?!

After completing this project, don't stop here and try adding more functions to the setup. Whatever you do, all the best!

Code

PS2_keyboard_controlled_model_train.inoArduino
/*
 * Arduino program to control a model train with a PS/2 keyboard.
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * Watch this project in action:https://youtu.be/94yJJhv92OE
 */
#include <PS2Keyboard.h>

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

PS2Keyboard keyboard;


int s=0,i=125;

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 motor_halt(int i){ //Function to stop and/or turn off the locomotive.
  if(s>30){
    for(s=s;s>30;s--){
    motor_go();
    delay(i);
    }
 }
  if(s<=30&&s>0&&i<=60){
    for(s=s;s!=0;s--){
    motor_go();
    delay(60);
    }
  }
  if(s<-30){
    for(s=s;s!=-30;s++){
    motor_go();
    delay(i);
    }
  }
  if(s>=-30&&s<0){
    for(s=s;s!=0;s++){
    motor_go();
    delay(60);
    }
  }
}
void setup() {
  // put your setup code here, to run once:
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
}

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<30&&s>=0){
        for(s=s;s<30;s++){
          motor_go();
          delay(60);
        }
        break;
      }
      if(s==30){
        for(s=s;s!=0;s--){
          motor_go();
          delay(60);
        }
      }
      break; 
      
      case ' ':
      motor_halt(125);
      break;

      case PS2_ENTER:
      motor_halt(60);
      break;
     
    }
    
    
}
    //Serial.println(s);
    motor_go();
}

Comments

Similar projects you might like

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 1,447 views
  • 1 comment
  • 6 respects

Simple Automated Model Railway Layout | Arduino Controlled

Project tutorial by Kushagra Keshari

  • 3,646 views
  • 1 comment
  • 11 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 1,703 views
  • 0 comments
  • 6 respects

Automated Model Railway Layout with Passing Siding

Project tutorial by Kushagra Keshari

  • 6,955 views
  • 10 comments
  • 36 respects

Electronic Piano Keyboard With Preset Songs

Project tutorial by Lindsay Fox

  • 80,862 views
  • 65 comments
  • 161 respects

Model Train Control

Project in progress by Masters

  • 6,931 views
  • 3 comments
  • 14 respects
Add projectSign up / Login