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!
Become the Maker you were born to be. Try Arduino Academy for FREE!
Get the IR Sensor Board at http://arduinotronics.blogspot.com/2017/06/6-channel-infra-red-transceiver-sensor.html
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:
Serial.begin(9600);
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
matrix.begin(0x70);
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(“Ready ….”);
matrix.print(0.0);
matrix.writeDisplay();
//finished=0;
s1Covered=digitalRead(sensor1);
if (s1Covered==0 && started==0){
start=millis();
started=1;
Serial.println(“Started”);
}
s2Covered=digitalRead(sensor2);
if (s2Covered==0 && started==1){
finish=millis();
finished=1;
Serial.println(“Finished”);
elapsed = finish-start; // millis
elapsed = elapsed /1000; // seconds
Serial.print(“Seconds: “);
Serial.println(elapsed);
miles = distance / 63360; // miles
hours = elapsed / 3600; // hours
mph = miles / hours;
scaleMPH = mph * scale;
Serial.print(“Scale MPH: “);
Serial.println(scaleMPH);
// print a floating point
matrix.print(scaleMPH);
matrix.writeDisplay();
delay(25000);
started=0;
finished=0;
}
}
Thanks for sharing! I just ordered my first Arduino and plan to try this project.
I'm fairly new to the Arduino. Is there any reason an Uno couldn't be used for this instead of a Nano?
You can use an UNO. They are functionally equivalent.
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
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.
If you get the sensor board, it comes pre wired with 6 sensors. No electronics background needed.
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 ?
That would be correct.
Thanks again. What wattage are the 100 ohm and 1k resistors for the ir sensors ? Getting my parts list together.
That's 1/4 watt 10k and 100k. The 68 ohm is 1/2 watt.
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
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
the I2C pins are A4 & A5, the sensors are connected to D4 & D5 (arbitrary).
The code appears to be reading speeds in one direction only, have you written a bi-directional sketch?
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.
the size of the 7 segment LED doesn't matter. only the wiring of the segments. You could make your own using strips of leds behind plastic.
Bobot has rewritten the sketch, which can be viewed at http://bobotstrains.blogspot.com/2017/03/supplemental-info-for-model-railroad.html
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.
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
did you connect and install the library per https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack
run their example sketch to make sure your display is working.
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
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?
float distance = 24; //inches between sensors
change to 10
float distance = 24; //inches between sensors
change to 10