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.

  • 1,277 views
  • 0 comments
  • 2 respects

Components and supplies

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

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: 'https://hackster.imgix.net/uploads/attachments/1104740/FNCJLRKK8T202SR.jpg?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291121/img_2104_V9ovbsSlGM.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291122/ps2_touchpad_controlled_model_railroad___arduino_1_8_13_28-04-2021_10_54_57_t6I5OwaZFL.png?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291123/img_2156_YUkUg2LWbC.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291124/img_2167_cK31Ocuhve.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291128/img_2166_DPyf4EQtXN.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', caption: '', type: 'image' },{ URL: 'https://hackster.imgix.net/uploads/attachments/1291125/img_2158_B9HQXopCSV.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', caption: '', type: 'image' },{ URL: 'https://hackster.imgix.net/uploads/attachments/1291126/img_2159_3YsTCn3L2I.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', caption: '', type: 'image' },{ URL: 'https://hackster.imgix.net/uploads/attachments/1291127/img_2160_AROsCW6SAh.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291129/img_2162_9cVPlw9azf.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', caption: '', type: 'image' },{ URL: 'https://hackster.imgix.net/uploads/attachments/1291130/img_2163_2aKx3DLSJ2.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291131/img_2165_902EJVqwqh.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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: 'https://hackster.imgix.net/uploads/attachments/1291132/img_2190_hWydvfw1fm.JPG?auto=compress%2Cformat&w=1280&h=960&fit=max', 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!

Code

PS2_touchpad_controlled_model_railroad.inoArduino
/*
 * 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:-https://youtu.be/CZaF_8HFH-k
 * 
 * Made by Tech Build:-https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * 
 * Feel free to modify the code to adapt it to your application.
 */
#include<Adafruit_MotorShield.h>
#include<ps2.h>

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)
  {
    loco->setSpeed(sp);
    loco->run(FORWARD);
  }

  if (sp < 0)
  {
    loco->setSpeed(-sp);
    loco->run(BACKWARD);
  }

  if (sp == 0)
  {
    loco->setSpeed(sp);
    loco->run(RELEASE);
  }

}

void turnoutA_straight()
{
  turnoutA->run(FORWARD);
  delay(25);
  turnoutA->run(RELEASE);
}

void turnoutA_side()
{
  turnoutA->run(BACKWARD);
  delay(25);
  turnoutA->run(RELEASE);
}

void turnoutB_straight()
{
  turnoutB->run(FORWARD);
  delay(25);
  turnoutB->run(RELEASE);
}

void turnoutB_side()
{
  turnoutB->run(BACKWARD);
  delay(25);
  turnoutB->run(RELEASE);
}

void turnoutC_straight()
{
  turnoutC->run(FORWARD);
  delay(25);
  turnoutC->run(RELEASE);
}

void turnoutC_side()
{
  turnoutC->run(BACKWARD);
  delay(25);
  turnoutC->run(RELEASE);
}

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

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


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

  AFMS.begin(960);

  pinMode(statLED, OUTPUT);

  //Initializing the PS/2 touchpad
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
  
  turnoutA->setSpeed(255);
  turnoutB->setSpeed(255);
  turnoutC->setSpeed(255);

}

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

  Read();
  
  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();

  loco_run();

  if (m == 9)
  {
    if (s > 0) for (s = s; s != 0; s--)
      {
        Read();
        if(m != 8) break;
        sp = map(s, -limit, limit, -255, 255);
        loco_run();
        delay(20);
      }

    if (s < 0) for (s = s; s != 0; s++)
      {
        Read();
        if(m != 8) break;
        sp = map(s, -limit, limit, -255, 255);
        loco_run();
        delay(20);
      }
  }


}

Comments

Similar projects you might like

Simple Automated Model Railroad Loop with Yard Siding

Project tutorial by Kushagra Keshari

  • 5,211 views
  • 0 comments
  • 12 respects

Automated Point to Point Model Railroad with Yard Siding

Project tutorial by Kushagra Keshari

  • 3,654 views
  • 1 comment
  • 15 respects

Automated Model Railroad Layout With Reverse Loops

Project tutorial by Kushagra Keshari

  • 1,776 views
  • 0 comments
  • 3 respects

Simple Automated Point to Point Model Railroad

Project tutorial by Kushagra Keshari

  • 6,549 views
  • 4 comments
  • 14 respects

Control Your Model Train Layout with a Keyboard

Project tutorial by Kushagra Keshari

  • 5,513 views
  • 2 comments
  • 9 respects

Control Your Model Train Layout With Your Mobile Phone!

Project tutorial by Kushagra Keshari

  • 5,185 views
  • 1 comment
  • 10 respects
Add projectSign up / Login