You're reading ...
Arduino, Software

A € 6,- model train speed measurement device. Part 2: the Arduino software.

This is part 2 of 3 on a € 6,- DIY model train speed measurement device.

Part 1: The hardware
Part 2: The Arduino software
Part 3: Speed profiling an engine in Traincontroller 9 with an external device

If you don’t have the Arduino IDE (Integrated Development Environment) installed on your PC yet, go to, download the latest version and install it. Instructions are provided on the page.

Download the Measurement Device software from this link and unzip it. (To all who downloaded before September 2018, please download again. There was a lil bug in the Arduino code that made the device could ‘hang’ and you needed to reset it. That is cured in this new release.) You should now have four folders. First go into the CH341 folder and install the drivers. This is needed in case your Arduino uses the 340/341 USB/serial chip (most Aliexpress ones do). Now start the Arduino IDE and try if you can upload the ‘Blink’ example from the menu ‘File > Examples > 01.Basic’ without errors being generated on the IDE screen.

Images can be clicked to enlarge.

Arduino_IDEIf it won’t upload, it might be due to a COM port issue. Go to menu ‘Tools > Ports’ and one by one try every port listed. If it is grayed out, or if none of the COM ports work, the drivers have not loaded properly. What to do? You’re seldom the only one with a problem … Google for arduino com port and you’ll find multiple links that may help out.

If it works, close the Arduino IDE and move the Speed_Measurement_Device folder into your Arduino ‘sketches’ folder. Move the SSD1306 and the GFX folders into the ‘libraries’ folder. Now open the Arduino IDE again, the new libraries will now be active. Open the Speed_Measurement_Device sketch and upload it to the Arduino. You may get a low memory warning, but that is OK, it’s not a problem.

Device_WorkingThe OLED display should light up. If you don’t have a display, open the Serial Monitor from the ‘Tools’ menu on the IDE and your PC screen should show text that your device is waiting for sensor input. (If needed, set the Baud rate of the monitor to 9600).

We’re ready to go! Place the wooden bars with the Transmitters and the Receivers opposite of each other, check if both the sensor LEDs light up (if not, see part 1 on how to tune the potmeters) and run your finger, or a train, trough the IR beams to do your first speed measurement. The speed is displayed on the OLED as well as on the serial monitor on the PC.


Congratulations! Almost done … the final step is to fine tune the software. The first lines of code are:

// To select the model scale, use a wire between GND and A0:O45, A1:OO76, A2:TT120, A3:N160
// If no wire is used the scale defaults to HO87
The scale  factor can be set to O, OO, HO, TT or N by connecting A0, A1, A2 or A3 to GND with a wire. If no wire is used, the scale is HO. Reset the Arduino (the red push button) after changing the wire to activate the new scale.

// Use a wire between GND and pin8 to toggle between km/h and MPH
#define UNITS 0 // 0:km/h, 1:MPH // sets the default units with no wire
By default speed is displayed in km/h. To display MPH, change the line to #define UNITS 1. Units can also be changed externally by connecting a wire between GND and pin8. Reset the Arduino to activate the new units.

#define SENSOR_DISTANCE 200000 // [um] measured distance between the two IR beams
To maximize accuracy, measure the distance between the 1 mm holes and enter the value  in the code, in micrometers. Example: if the measured distance is 201.3 mm, enter 201300. It’s not too critical … if your measurement is 0.5 mm inaccurate, the accuracy of a speed measurement is still better than 0.25%.

#define LOCO_LENGTH 250 // [mm] used to calculate how long to disable sensor input when finished
To prevent a new measurement to start prematurely if a sensor is re-triggered while the train is still running, the software uses a waiting time before the next measurement can start. This is not a fixed time; the train length is used to calculate how long it takes until the train is fully past the sensor. The default value of 250 can be changed into the actual length of the train under test.

#define SENSOR_L_PIN 7
#define SENSOR_R_PIN 6
Run your finger through the left beam. If the display shows L >>> R all is OK. If it shows L <<< R, exchange pin numbers 6 and 7 to correct that.

All done! You’re ready to perform calibrated measurements. Enjoy …

Next: Part 3: speed profiling an engine in Traincontroller 9. (Of course using our new device 🙂)

About RudyB



25 thoughts on “A € 6,- model train speed measurement device. Part 2: the Arduino software.

  1. Hallo Ruud,

    Ik volg al je Blogs al jaren zeker de uitleg video’s van Train controller.
    I worstel al jaren om al mijn locs op een vaste plek in het block te l aten stoppen, dat is natuurlijk de
    Ik heb N spoor en heb al vanalles geprobeerd maar niet erg succesvol. Zo na lezen van je Speed Measure Device enthousiast geworden om dit eens te proberen.
    Hardware aangeschaft en wat Arduino study gedaan. Heb al je instructies gevolgd maar krijg geen goed resultaat. Op mijn serial Monitor (Notebook) krijg ik het volgende resultaat te zien.

    (Krijg de printscreen niet in de text, graag e-mail waar ik het naar toe zou kunnen sturen)

    Volgens mij werken de Opto-couplers, die test lukt.
    Mijn vraag is natuurlijk kun je me een beetje opweg helpen met wat ik fout doe.
    Met vriendelijke groeten uit een warm en zeer vochtig Thailand, Ad de Groot.


    Posted by Adrianus de Groot | September 17, 2021, 10:49
  2. Hello Rudy
    thank you for sharing your work. I just started to “play” with this. I do not have the sensors yet, but trying it out manually.
    One think I saw immediately is an issue when the speed is above 99 (KM or MPH). The display of speed about 99 is overflowing on the line below. Any chance this can be handled. like remove a digit for values between 100 and 999 + shift the KM to the right.
    Anything above 999 should display “Error”


    Posted by Cyril | May 11, 2021, 19:52
    • It’s software, which means anything can be done. 🙂 I never drive with speeds over 99 so for me this never was an issue. What could be done is leave off the 1 with speeds over 99. Or halve the font size. Or any other solution that would work. It’s just that right now I lack the time to make changes to this sketch. If you’d read the code you’ll find it’s reasonably self explanatory … maybe you can experiment some yourself?


      Posted by RudyB | May 11, 2021, 20:33
  3. Hi, I’m Davide, I followed and copied your project on my layout. The only thing that I don’t understand, is why my oled display doesn’t show the speed in H0 neither the train direction; i can only see the speed in KM.

    thanks a lot


    Posted by Davide | January 18, 2021, 11:59
  4. hi there i have spoke to you on YouTube about this and not seeming to get it working 100%


    Posted by Alain Turner | June 5, 2020, 15:44
  5. Hi Rudy, I have just tried to download your Arduino sketch from the link but dropbox is telling me that the link is no longer there. Could you please re-upload it to mail.
    Thank you my friend


    Posted by Joer | July 20, 2019, 10:03
  6. Hi Rudy, I have just tried to download your Arduino sketch from the link but dropbox is telling me that the link is no longer there. Could you please re-upload it to mail.
    Thank you my friend


    Posted by Joer | July 20, 2019, 09:49
  7. Hi Rudy,
    congrats for this nice tool. I want to do a similar Arduino project like this, but more like a mobile gadget. I run also a website and YouTube channel for model railway hobbyists and want to ask you for your permisson, to use and modify your Arduino sketch, and repost it on my site, free for use? I would you Rudy, name in my blog, as the basic creator of the sketch and can set a link to your projekt and website as well. But first, I have to built it up 😀
    If you want, please feel free to check out my site here on wordpress at

    I’m looking forward to your answer



    Posted by Jörg | November 11, 2018, 19:31
  8. Hi Rudy.

    Thanks very much indeed for providing this project. I’m completely new to Arduino and without your detailed instructions I would not have given it a go! Worked first time and I’ve now used it to with new locos in TrainController. I’ve just had one minor problem – it’s been a bit inconsistent in resetting to start the next reading. I’ve got round this by simply resetting the Arduino manually between each reading. A bit inelegant but foolproof! Thanks again.



    Posted by Robert Watson | May 22, 2018, 11:24
    • Hi Robert, thanks for your nice feedback. I hope you have fun with it. Actually allowing the next measurement to start is done in an ‘intelligent’ way. We don’t want to accidentally start a new measurment before the train is fully past. You can enter the train length in the code. A new measurement can only be started once the train is fully past. Obviously, when it runs at very low speed this can take a while, maybe up to half a minute. This is done to prevent a new measurement to be triggered before the train is fully past. Resetting the Arduno of course always is a good workaround if things got mixed up.


      Posted by RudyB | May 22, 2018, 15:43
  9. ello rudy my name is Peter and I follow you from Italy I built the speedmeter but I have a problem on your video on the display you also see the direction of travel I see only the speed of the display and SSD1306 OLED 128×64 where is the error? thank you, peter


    Posted by pietro | May 19, 2018, 17:11
  10. Hi Rudy, I have just tried to download your Arduino sketch from the link but dropbox is telling me that the link is no longer there. Could you please re-upload it to dropbox.
    Thank you my friend


    Posted by Mac McManus | April 29, 2018, 13:35
  11. Simple to build. So far I only have 1 IR detector ( will purchase the other), but I see cool stuff happening on the display. Sweet.


    Posted by diehard | April 5, 2018, 14:17
  12. great. Very similar to my set-up. I use an Arduino nano and a larger LED display – last year OLED were comparatively expensive. And since I use Märklin M Tracks I just took two standard tracks with built-in mechanical switches. Makes a perfect integration into the track layout without the need for special sensors.

    One problem I could not solve perfectly: if you run a train with cars the speed measurement may get confused…. I put a delay between different measurements in order to avoid that the speed will be measured for the cars too. However if I have very slow and long trains, the measurement will start again. It does not help to shorten the delay: otherwise very fast lokos on my small layout will not be measured every turn…

    Greetings from Darmstadt, Germany Reinhard



    Posted by reinhardhund | March 30, 2018, 20:46
    • Hello Reinhard. The issue is solved by entering the length of the train under testin the software. The waiting time before a new measurement can start is dynamically calculated from the measured speed and the given length. Alas this does not work when permanently installed along the track where trains of different length pass by. In that case a fixed waiting time is the easiest option, but that is indeed not fail safe with very slow trains. Maybe some ‘pattern recognition’ on the two sensor inputs may help out, but that could quickly become complicated and probably also is not 100% fail safe.

      Liked by 1 person

      Posted by RudyB | March 30, 2018, 21:03


  1. Pingback: A € 6,- model train speed measurement device. Part 1: the hardware. | rudysmodelrailway - March 29, 2018

  2. Pingback: Speed profiling an engine in Traincontroller 9 using an external device. | rudysmodelrailway - March 29, 2018

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Archive of all posts


%d bloggers like this: