Saturday, November 26, 2016

Arduino Model Railroad Scale Speedometer - Finished

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 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;
    }
  }


10 comments:

  1. Thanks for sharing! I just ordered my first Arduino and plan to try this project.

    ReplyDelete
  2. I'm fairly new to the Arduino. Is there any reason an Uno couldn't be used for this instead of a Nano?

    ReplyDelete
    Replies
    1. You can use an UNO. They are functionally equivalent.

      Delete
  3. 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

    ReplyDelete
    Replies
    1. 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.

      Delete
  4. If you get the sensor board, it comes pre wired with 6 sensors. No electronics background needed.

    ReplyDelete
  5. 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 ?

    ReplyDelete
  6. Thanks again. What wattage are the 100 ohm and 1k resistors for the ir sensors ? Getting my parts list together.

    ReplyDelete
    Replies
    1. That's 1/4 watt 10k and 100k. The 68 ohm is 1/2 watt.

      Delete

Related Posts Plugin for WordPress, Blogger...