Project tutorial
Control Your Model Railroad with the Slide of Your Finger!

Control Your Model Railroad with the Slide of Your Finger! © CC BY-NC-ND

Use an Arduino microcontroller to control a model railway layout with a PS/2 touchpad.

  • 2 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino UNO
Adafruit Motor Shield V2
Adafruit 12V 5A switching power supply
A PS/2 touchpad
You can get one from an old laptop or from a computer repair shop.

Necessary tools and machines

26w6260 40
Multitool, Screwdriver
A screwdriver with a small chisel tip for tightening screws of the motor shield's terminal blocks.

Apps and online services

About this project

A laptop's touchpad is one of the great devices to use as an input for microcontroller projects. So today, let's implement this device with an Arduino microcontroller to control a model railroad. Using a PS/2 touchpad, we will be able to control 3 turnouts and track power.

Keeping the touchpad with its width at the bottom(Sort of like portrait mode), sliding the finger along each of the two diagonals will be used to control two turnouts, sliding the finger horizontally will be used to control another turnout and sliding the finger vertically will be used to control the locomotive's speed and direction.

So, without further ado, let's get started!

Step 1: Watch the Video

Watch the video to understand all the controls and know more about how it works.

Step 2: Get All Parts and Supplies
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['4110c6f852'] = [{ URL: '', caption: '', type: 'image' }];

For this project, you will require:

  • An Arduino microcontroller compatible with Adafruit Motor Shield V2(UNO, Leonardo, basically almost any microcontroller with ananI2C interface)
  • An Adafruit Motor Shield V2
  • A PS/2 touchpad
  • 4 wires with male Dupont connectors on one side(To connect the touchpad to the Arduino board)
  • 2 wires each for connecting track power and turnouts(3 max) to the motor shield
  • A 12-volt DC power supply with a current capacity of at least 1A.

Step 3: Get the Ps2 Library

Download the folder of the ps2 library from here. Move the downloaded folder to the desktop as it will be easier to find. Open the Arduino IDE and click Sketch>Include Library>Add.ZIP Library... and then select the ps2 folder from the desktop. The library will be included and you now be able to use the ps2 library.

Step 4: Figure Out the Connections of the Touchpad
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['39c78d31b8'] = [{ URL: '', caption: '', type: 'image' }];

If you have a Synaptics touchpad like the above one, the pad 'T22' is +5V(Orange), 'T10' is 'Clock'(Yellow), 'T11' is 'Data'(Green) and 'T23' is 'GND'(Blue). You can also solder the 'GND' wire to a large exposed copper as shown above.

Click on the above image to know more. If you have a different touchpad, try searching for its part number on the internet with 'pinouts' or you can ask the r/Arduino community on Reddit if you get stuck.

Step 5: Test the Touchpad

Make sure the correct connections are made to the touchpad. To test the touchpad, upload the ps2 mouse code on the Arduino microcontroller from Examples>ps2. Connect 'Clock' wire to D6, 'Data' wire to D5, GND to GND, and +5V or VCC to +5V pin of the Arduino board respectively. Reconnect the Arduino board to the computer and open the serial monitor. If you see the numbers changing as you move your finger across the touchpad, the touchpad is working properly and you can proceed.

Step 6: Program the Arduino Microcontroller
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['ed5dd19d40'] = [{ URL: '', caption: '', type: 'image' }];

It is recommended to go through the code before uploading it to the Arduino microcontroller to understand what all is going on.

Step 7: Set Up the Layout
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['fa286f9926'] = [{ URL: '', caption: '', type: 'image' }];

Set up a layout to test the track power and all three turnout controls. Make sure all track joints are made properly and the tracks are clean. Cleaning the tracks and locomotives' wheels periodically is recommended to prevent the locomotives from stalling.

Step 8: Install the Motor Shield on the Arduino Board
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['290bde11d7'] = [{ URL: '', caption: '', type: 'image' }];

Carefully align the pins of the motor shield with the female headers of the Arduino board and push the shield on top of the Arduino board. Make sure the shield fits securely on the Arduino board and no pin gets bent.

Step 9: Connect Track Power and Turnouts to the Motor Shield
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['a8d562c47b'] = [{ URL: '', caption: '', type: 'image' },{ URL: '', caption: '', type: 'image' },{ URL: '', caption: '', type: 'image' },{ URL: '', caption: '', type: 'image' }];

Make the following connections:

  • Connect the track power to the output connector of the shield labeled 'M1'.
  • Connect the turnouts to the rest three output connectors 'M2', 'M3', and 'M4'.

Make sure all the wiring connections are tight.

Step 10: Connect the Touchpad to the Setup
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['b3b39eeacd'] = [{ URL: '', caption: '', type: 'image' },{ URL: '', caption: '', type: 'image' }];

Connect the touchpad to the Arduino board by making the following connections between the touchpad and the Arduino board:

  • +5-volt or 'VCC' to +5-volt of the Arduino board
  • 'GND' to 'GND' of the Arduino board
  • 'Clock' to 'D6' of the Arduino board
  • 'Data' to 'D5' of the Arduino board

Step 11: Place the Locomotive(s) on the Track
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['b6206b694b'] = [{ URL: '', caption: '', type: 'image' }];

Place a locomotive to be tested. You can also place multiple locomotives as per wish.

The use of a rerailing tool is recommended. Make sure the locomotives are properly placed on the tracks to prevent derailments.

Step 12: Connect the Setup to Power and Turn It On
if (typeof(lightBoxImages) == 'undefined') { lightBoxImages = {}; } lightBoxImages['9b7793ecc6'] = [{ URL: '', caption: '', type: 'image' }];

Connect the 12-volt power supply to the setup and turn it on.

Step 13: Test the Controls

Test all the controls. Refer to the above video again to understand the controls.

Step 14: Expand It Furthur

Try adding more features and functions to this project, maybe add another touchpad. Whatever you do, all the best!


 * Arduino code to control a model railroad layout using a PS/2 touchpad.
 * Using a tochpad, three turnouts and one track power can be controlled.
 * The code can be modified to control more than one track power at a cost of a turnout.
 * Watch the video:-
 * Made by Tech Build:-
 * Feel free to modify the code to adapt it to your application.

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

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

PS2 mouse(6, 5);//PS2 mouse(Clock, Data);

#define statLED 13//LED on pin 13 will light up whenever touchpad registers any difference in reading.

char mstat;
char mx;
char my;
int m, x, y;
int s = 0;
int sp;
int limit = 1020;//Value proportional to the maximum value of the duty cycle of PWM waves
                 //for track power(255).

void loco_run()
  if (sp > 0)

  if (sp < 0)

  if (sp == 0)


void turnoutA_straight()

void turnoutA_side()

void turnoutB_straight()

void turnoutB_side()

void turnoutC_straight()

void turnoutC_side()

void Read()//Function to read data from touchpad.
  mouse.write(0xeb);  // give me data!;      // ignore ack
  mstat =;
  mx =;
  my =;

  m = (int)mstat;
  x = (int)mx;
  y = (int)my;

void setup() {
  // put your setup code here, to run once:


  pinMode(statLED, OUTPUT);

  //Initializing the PS/2 touchpad
  mouse.write(0xff);  // reset;  // ack byte;  // blank */;  // blank */
  mouse.write(0xf0);  // remote mode;  // ack


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

  if (m != 8) digitalWrite(statLED, HIGH);
  else digitalWrite(statLED, LOW);

  sp = map(s, -limit, limit, -255, 255);

  if (x > 0 && y > -4 && y < 4 && s < limit) s++;

  if (x < 0 && y > -4 && y < 4 && s > -limit) s--;

  if (y > 20 && x > -4 && x < 4) turnoutA_side();

  if (y < -20 && x > -4 && x < 4) turnoutA_straight();

  if (x > 20 && y > 20) turnoutB_side();

  if (x < -20 && y < -20) turnoutB_straight();

  if (x > 20 && y < -20) turnoutC_side();

  if (x < -20 && y > 20) turnoutC_straight();


  if (m == 9)
    if (s > 0) for (s = s; s != 0; s--)
        if(m != 8) break;
        sp = map(s, -limit, limit, -255, 255);

    if (s < 0) for (s = s; s != 0; s++)
        if(m != 8) break;
        sp = map(s, -limit, limit, -255, 255);



Similar projects you might like

Simple Automated Model Railroad Loop with Yard Siding

Project tutorial by Kushagra Keshari

  • 12 respects

Automated Point to Point Model Railroad with Yard Siding

Project tutorial by Kushagra Keshari

  • 1 comment
  • 15 respects

Automated Model Railroad Layout With Reverse Loops

Project tutorial by Kushagra Keshari

  • 3 respects

Simple Automated Point to Point Model Railroad

Project tutorial by Kushagra Keshari

  • 14 respects

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 9 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 1 comment
  • 10 respects
Add projectSign up / Login