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 https://www.arduino.cc/en/Main/Software, 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.
If 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.
The 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 …