Train Speedometer pages added

Following a presentation I made on the September 2023 NRail.org ZoomTRAK meeting, I’ve written up a pair of new pages for this blog about my example design of an Arduino-based train speedometer, which is designed to measure and display the scale speed of a passing model train, with configurable scale (1:87, 1:160, …) and measured units (km/hr, mph, …). The pages can be found at these links.

38 thoughts on “Train Speedometer pages added

  1. exuberant7318249bc9's avatar

    Have had this project on my radar for over a year now. Finally with your December 2024 I managed to compile the code. But when executed it seems to halt at “DISP OK”. Pins, 2,3,4,5,8,9,10 hook up diagram would be helpful. Thanks for sharing.

    Liked by 1 person

  2. twrackers's avatar

    Cool, you’re the first person in the 9 years since I started this blog to actually comment on one of my posts!

    I will get the info you want back to you, hopefully today while we here in the Richmond VA area deal with a days-long-so-far boil-water alert AND a big honking snow storm supposed to move in this evening. Right when our RANTRAK club has our monthly meeting tomorrow AND need to load up the NTRAK layout into a trailer for the Greenberg train show here next weekend. o_O

    For now, I don’t know why the code would stop at the DISP OK point. Will dig into that first.

    TWR

    Like

    • exuberant7318249bc9's avatar

      Thanks for your quick reply. I’m having difficulty replying due to sign in to world press. I am using the same boards you listed. Will reply in depth later.

      Like

      • exuberant7318249bc9's avatar

        Just got through reloading a new copy of your program and have experienced the same result. I then tried changing “Trace 0 to Trace 1” In Sensor.cpp the program compiles and when executed the code halts at “spd err”. I then tried changing “Trace 0 to Trace 1” in Speedometer.cpp and got a compiling error “Streaming.h” no such file or directory. I tried the above because I wasn’t getting any output in the serial monitor.

        Like

      • twrackers's avatar

        My mistake, I use the Streaming library out of habit. Do a fresh pull of the TrainSpeedometer repo on GitHub, I’ve changed a few codes so that by default the old-style Serial.print/println functions are used for trace outputs. Probably easier for you than getting the Streaming library from the arduino.cc website.

        Well. it used to be on Arduino’s site. Now you would install the Streaming library from here: https://github.com/janelia-arduino/Streaming

        Like

  3. twrackers's avatar

    First thing you can try:

    In Sensor.cpp, change line 3 to say #define TRACE 1, then rebuild and run the sketch. Odds are an error is occurring when the code is trying to init the pair of sensors. Meanwhile the hardware that appears in the YouTubes is still assembled in a box behind me, so I’ll use that and my code to whip up a diagram of how it all is connected.

    Like

  4. twrackers's avatar

    By the way… which Arduino-type board are you setting this up with? If you’re using the SparkFun QWIIC Shield like I did, I’ve discovered in the last 48 hours that it isn’t supported properly by all newer CPU boards. It was apparently designed only with the original Arduino UNO in mind. I have a few Adafruit boards which are driving the I2C on pins other than what the QWIIC Shield expects.

    But the fact the display responded properly implies that’s probably not the root problem.

    Like

  5. twrackers's avatar

    Okay, I have two Adafruit VL6180X sensors, both connected to QWIIC Shield. Each sensor also has additional pin marked GPIO and XSHUT. Call sensors SensorA and SensorB. These are the connections to the Arduino’s pins 2-5:

    • Pin 5 -> SensorA XSHUT
    • Pin 4 -> SensorB XSHUT
    • Pin 3 -> SensorA GPIO
    • Pin 2 -> SensorB GPIO

    Pins 8-10 are used to select runtime options. All are in INPUT_PULLUP mode, so leave them floating or ground them as follows:

    • Pin 10: open for “near track”, ground for “far track”
    • Pin 9: open for metric (“KPH”), ground for imperial (“MPH”)
    • Pin 8: open for Japanese N scale (1:150), ground for American (1:160)

    Notes:

    • See TrainSpeedometer.ino lines 29-37 for range definitions which define where near and far tracks are w/r/t sensors.
    • Japanese N-scale trains are 1:150 scale except for Shinkansen models which are 1:160. That’s because real Shinkansen run on 4′ 8.5″ tracks like in US, but the rest of the trains run on 3′ 6″ gauge. So those trains are modeled a wee bit larger.

    Give me a few minutes and I’ll post a picture of the sensor board.

    Like

      • twrackers's avatar

        That should work. So you’ll get the “near track” distance, metric display, and 1:150 scaling of speed. You can always edit your copy of the code to change the defaults, esp. the distance from your sensors to the track being monitored.

        Like

  6. twrackers's avatar

    On the sensor boards, the XSHUT pins are used to disable each sensor long enough for the I2C address of the other to be reprogrammed, so that when enabled they have distinct addresses. These are outputs from the Arduino.

    The GPIO pins are used as input interrupts from each sensor to the Arduino, so that an interrupt handler can read the detected range from a sensor when conversion is complete.

    Like

  7. twrackers's avatar

    By the way, about the VL6180X sensor…

    Adafrut claims on https://learn.adafruit.com/adafruit-vl6180x-time-of-flight-micro-lidar-distance-sensor-breakout/pinouts that

    The VL6180X is a I2C sensor. That means it uses the two I2C data/clock wires available on most microcontrollers, and can share those pins with other sensors as long as they don’t have an address collision. For future reference, the I2C address is 0x29 and you can’t change it!

    Not true. My speedometer code relies on being able to do exactly that: set each sensor to its own new address. The procedure is described in an application note from ST, the sensor chip maker. It’s application note AN4478, downloadable from page https://www.st.com/en/imaging-and-photonics-solutions/vl6180x.html#.

    You can see where that’s done in my code in Sensor.cpp, line 38. I used driver code that came from ST.

    Like

    • exuberant7318249bc9's avatar

      Have been checking various items and have tested a few. Sensor and addresses are as should be.
      In Sensor.cpp lines 35 & 36 mentions a virtual destructor to make sure to delete the object

      Like

  8. exuberant7318249bc9's avatar

    I tried a different board and had the same outcome. When I reverse the interrupt and enable pins I get the halt at “DISP OK”. When hooked correctly the “SPD ERR” is displayed. Tried the adafruit triple example modified for only two sensors a got readings from both. Did not block both sensors simultaneously but will try that later. Not sure if readings were correct.

    Like

  9. twrackers's avatar

    Are you sure you’ve pulled the most recent version of the TrainSpeedometer repository? In the entire repo, the word ‘virtual’ only appears once, in Speedometer.h line 50, and the destructor symbol ‘~’ appears in none of the .ino, .cpp, or .h files.

    Like

  10. twrackers's avatar

    My error, only two days ago discovered API errors in Sensor and Speedometer classes, got the same SPD ERR result. That’s been fixed and the repo has been updated. Try again.

    Don’t know how this slipped past me for so long, but I only the other day tried running the code since months ago.

    Like

  11. exuberant7318249bc9's avatar

    Downloaded and tested near track only. Tested metric and imperial Japanese and US. It reported speeds 15 out 15 trys. My layout of device mimics yours closely will provide a picture when completed. I wish to thank you wholeheartedly for this project. Jim.

    Like

  12. twrackers's avatar

    After some digging and a couple of doses of headache meds, I’ve found a way that may allow me to reconfigure the hardware to remove the need for the four additional GPIO pins on the Arduino board. It will require the addition of an Adafruit LTC4316 I2C Address Translator – Stemma QT / Qwiic (https://www.adafruit.com/product/5914) in addition to the two VL6180X sensors. The three parts would be daisy-chained from the Arduino as sensor-translator-sensor, so only one Qwiic port will be needed. Looks it might also work with the way-more-compact Adafruit_VL6180X library rather than the STMduino (sp?) library I was using.

    Now I just need to finish making v2.0 of my sketch to verify I can get the performance I had before. Will keep you updated on this. If it works, I’ll update my blog pages as well.

    🙂

    Like

Leave a reply to twrackers Cancel reply