Arduino Model Railroad Scale Speedometer – Finished

Share on facebook
Share on google
Share on twitter
Share on linkedin
A VPN is an essential component of IT security, whether you’re just starting a business or are already up and running. Most business interactions and transactions happen online and VPN

We finally finished the Scale Speedometer Project, and are heading off to the Charleston Area Model Railroad Club to install it. We used the Adafruit I2C 7 Segment backpack, a Arduino Nano clone, and two IR transceivers, for  total project cost of around $20. The code below is fully functional, all you need to do is adjust your scale, and distance between the sensors. Make sure your sensors do not extend above the height of the rails, or couplers could catch on them. I used a dremel to route out the holes, and superglue to fix them in place, but superglue leaves a white residue, so you may want to try a different adhesive. Watch the video!

Get the IR Sensor Board at

Arduino Nano Clone
7 Segment BackPack
IR sensors

Use the Adafruit Tutorial to assemble and connect your 7 segment backpack. Use the IR Sensor Tutorial to connect the IR sensors.

Update: Decreasing the resistance of the IR LED resistor (we finally used 100 Ohm) or increasing the resistance of the pullup resistor on the photo-transistor (we finally used 100k) can make the circuit more sensitive (up to 15mm depending on reflectance of car bottom), especially for larger scales with larger gaps. Do not burn out the IR LED with too low of a resistance (no less than 68 Ohm, 1/2w at 5v input).

#include  “Wire.h”
#include “Adafruit_LEDBackpack.h”
#include “Adafruit_GFX.h”

Adafruit_7segment matrix = Adafruit_7segment();

//user variables
float distance = 24; //inches between sensors
int scale=87;

int sensor1 = 4;
int sensor2 = 5;
bool s1Covered;
bool s2Covered;
float start, finish, elapsed, miles, hours, mph, scaleMPH;

int started=0, finished=0;
int scale=87;

void setup() {
// put your setup code here, to run once:
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);


void loop() {
// put your main code here, to run repeatedly:
//Serial.println(“Ready ….”);
if (s1Covered==0 && started==0){


if (s2Covered==0 && started==1){

elapsed = finish-start; // millis

elapsed = elapsed /1000; // seconds
Serial.print(“Seconds: “);
miles = distance / 63360; // miles
hours = elapsed / 3600; // hours
mph = miles / hours;
scaleMPH = mph * scale;
Serial.print(“Scale MPH: “);

// print a floating point


Steve Spence

Steve Spence

Leave a Reply

24 thoughts on “Arduino Model Railroad Scale Speedometer – Finished”

  1. Hi. I am interested in building this speedometer, but I am a real novice at electronics. I see that the parts needed are the Nano, backpack and IR sensors. What is the IR sensor board you have a link for ? And is it also needed for the basic speedometer ? I have seen these built with the sensors mounted in a small enclosure that is portable, you just set it next to the track. Is that possible with this build ? Also will this setup run off of a 9 volt battery ? Thank you in advance for any help with my questions. Don

  2. The sensor board is to make it easy to connect the IR sensors to the Arduino. The adjustable pot makes it easy to adjust sensitivity for ambient light conditions.Yes, this build can also work with cross track detectors. It will run off a 9v, but not for long.

  3. Thank you for the information. I am going to attempt to build this. I model in N-Scale, so when I input the code for scale do I write 160 instead of the 87 for HO-Scale ?

  4. Which pins on the Nano connect to the Adafruit packpack? All of the Nano docs I can find list D5 and D4 as the I2C pins but these are the same pins the sensors are hooked to

    1. Hi Steve,
      Is there a schematic for the circuit available from you or Bobot? Bobot modified you circuit to use on O scale.
      Thanks Jud

  5. Thank you for sharing ! Has anyone tried using some larger display and what did you use? I would like to get 1" + numbers to be able to read this from a few angles and at a distance.

  6. Hi Steve
    I built your original circuit and loaded your code and I got it to work perfectly via the serial monitor.

    Adding the Ada fruit display and your new code, I get it to work vi the serial monitor but no display on the 7 segment display. I tried another brand new display and nothing.
    I have clock on A5 and IO on A4.

    Any ideas?

    Also do you have an updated circuit diagram so I can compare it to mine?

    Thanks so so much

  7. Steve thanks for this excellent peoject.
    I built the circuit and loaded your first sketch and it works via serial monitor.

    If I load your latest code however I cant get the display to work.

    Almost as though its dead.

    I have tried another brand new ada fruit display and same result.
    Like I say the circuit works because the serial monitor displays the time and speed.

    Have you got a circuit diagram for connection of the display via I2C? I need to double check my circuit.

    Thanks again.

  8. Building a T-TRAK single module in N Scale. I see the one variable to change for the scale, but was wondering what would I have to change to operate the sensors 10" apart?

  9. Hi Steve

    I am planning on building the speedometer but have a question. I model N Scale, so I see where to change that variable. My question is about the float distance of 24" – I am planning on doing a T-TRAK module, but am limited to approx 10 inches, is there a way to make this work?
    Thanks Jim

Leave a Comment

Your email address will not be published. Required fields are marked *