Author Topic: Arduino Speedometer for <$15  (Read 2077 times)

0 Members and 1 Guest are viewing this topic.

babbo_enzo

  • Crew
  • *
  • Posts: 200
  • Gender: Male
  • Respect: +12
    • Experience IT!
Arduino Speedometer for <$15
« on: October 16, 2014, 12:24:58 PM »
0
Hi gents, I've open this topic and deleted my long message from here to don't Hijack the original.
https://www.therailwire.net/forum/index.php?topic=34250
--------------------------------------------------------------------------------------------------------------------------------------
The Micro used is this:
http://www.ebay.com/itm/5PCS-USB-Nano-V3-0-ATmega328-16M-5V-Micro-controller-CH340G-board-For-Arduino-A-/201152975734?ssPageName=ADME:L:OU:IT:3160

Speedometer:
The idea was to be familiar with this game and start KISS. So, before decide where in the Layout, I plan for a "portable" one ( that can be used also on our modular Fremo meetings?).

Mechanically will be based on a plastic bar that holds two of this IR sensor at the ends :
http://www.ebay.com/itm/Infrared-reflective-Photoelectric-Switch-IR-Barrier-Line-Track-sensor-TCRT5000L-/310536079942?ssPageName=ADME:L:OU:IT:3160
I've order also some spare TCRT5000 sensors (in case it works) to install somewhere along the tracks in future.
If these sensors demonstrate not enough sensible at reasonable distance ... I've order also a couple of :
http://www.ebay.com/itm/Smart-car-Obstacle-avoidance-Infrared-Sensor-module-Reflective-photoelectric-/400494756580?ssPageName=ADME:L:OU:IT:3160

The "brain" can be positioned on fascia (cab or cards holders?).
Project parts are :
- 1 Arduino Nano
- 2 IR sensors boards
- 1 2x20 char LCD
http://www.ebay.com/itm/IIC-I2C-TWI-SP-I-Serial-Interface1602-16X2-Character-LCD-Module-Display-Blue-/310565065847?ssPageName=ADME:L:OU:IT:3160

+ 1 .... sound voice generator card as option .... to warn the operators !
http://www.ebay.com/itm/ISD1820-Sound-Voice-Recording-Playback-Module-With-Mic-Sound-Audio-Loudspeaker-/200958077647?ssPageName=ADME:L:OU:IT:3160

Parts cost (more or less! 8) ) :
Arduino =                         $4.
2x IR sensor interface =    $3.
LCD =                              $5,
Audio board ( option)         $3.
------------------------------------
TOTAL =                           $15

To clock the interrupt, as  the Arduino's internal timer is not totally reliable, I try the PWM analog output wired directly to the interrupt pin  2.
The analog PWM outputs did have a very steady square wave of 490Hz.  The duty cycle is determined by the value written to the analog pin.  (i.e. - 0 is zero volts, 127 is a 50% duty cycle, 255 is a logic high/5V).

Also I will limit max time measurement between 2 sensors to 60 seconds and unit is "hardware configured" to measure the train’s speed over 1/50 or 1/100 ( jumper selectable) of a scale mile (or Km) both displayed on LCD.
Means you need to install the 2 IR sensor at a specific distance (depending on Scale and resolution wanted)
The sensor spacing for N and HO is listed here:

Scale - Scale mile (inch) - 1/50 mile - 1/100 mile
 N         396.000               7.920         3.960
 HO      728.276              14.566         7.283

scale - Scale Km(cm) - 1/50 km (cm) - 1/100 Km (cm)
  N         625.00               12.50           6.25
 HO      1149.43                22.99          11.49 

For example, in N scale if you use the 1/100 scale mile interval, you need to place the sensors 3.96 inches apart.
If your particular scale is not listed you need to calculate the proper interval by dividing 63360 (the number of inches in a mile) by your scale’s ratio, and then dividing that result by the interval chosen (50, 100).

The accuracy of the unit depends upon how precisely the sensors are spaced.
Sensor spacing errors can be minimized by choosing the longest spacing that is practical for your layout and scale
Also, IR sensors use available ambient light. Mounting them in tunnels or dark corners of the layout is not working. It is best to pick a well lit section of the layout where the light source comes from above and ‘inside’ the layout. If the light source is ‘outside’ the layout a person walking by may trigger the unit.
(these info are from TCS TrainSpeed device)

Speed calculation is simple:
for example if sensors are installed at 1/100 scale miles:
10 / (time in secs ) ) x 3,6 = Km/h
Km/h x 1,6093 = Miles / h
PS- to have a better speed resolution display my interrupt routine counts 1/10 of second in place of seconds.
When Arduino Nano will land here... I will follow with pictures of cards and listing of software.
« Last Edit: November 11, 2014, 05:32:29 PM by babbo_enzo »

babbo_enzo

  • Crew
  • *
  • Posts: 200
  • Gender: Male
  • Respect: +12
    • Experience IT!
Re: Arduino Speedometer for <$15
« Reply #1 on: November 11, 2014, 12:22:16 PM »
0
At the end I've finally find some time to write some software ( well, not really nice, I admit, but at least seems working well)
As a first test, I've connected two buttons in place of the two IR sensors.
And software take care for now just of a left_right direction (from sensor 1 to sensor 2)
Next step will be complete the sketch with a possibility to detect a run in reverse direction on same track.
And ... make a better Sw "status machine" !
Also I've connected a LED to display 1/10 sec clock during mesurement, and for the moment use the Serial Monitor in place of the final LCD Library ...
Well, lot of more free time to find to complete, but... I've fun for now, and this was the primary scope!
See attached the sketch listing at present stage of evolution ...  ( I will replace later with a PDF file ? )
---------------------------------------------------------------------------------------------------------------------
/* Wired for an Arduino NANO board
Uses analog PWM output of 490Hz with a 50% duty cycle.
Connect a jumper from D3 to Digital Pin 2 (interrupt 0)
Connect IR sensor 1 to pin D4 (test done with a button to GND)
Connect IR sensor 2 to pin D5(test done with a button to GND)
- optional LED to pin 13 and resistor 1K to +5 to watch 1/10 seconds blink.
- All Serial.print to test on IDE Monitor
Final will replace with LCD library
*/

int clockInt = 0; // digital pin 2 is now interrupt 0
int masterClock = 0; // counts rising edge clock signals
float th_sec = 0;
float temp = 0;
float KmHour = 0;
float MilesHour =0;
int ledPin = 13; // initialize the LED as an output
int ledpinstate = HIGH;
int oldstate = HIGH;
int IRsensor1 = 4; // input for first IR sensor
int Sensor1_Stat = 1;
int lastSensor1_Stat = 1; // memory status of the sensor-1
int IRsensor2 = 5; // input for second IR sensor
int Sensor2_Stat = 1;
int lastSensor2_Stat = 1; // memory status of the sensor-2
boolean RunningLeft = false;
boolean RunningRigth = false;
boolean Waiting = true;
void setup()
{
pinMode(IRsensor1, INPUT_PULLUP); // IR 1 sensors pin
pinMode(IRsensor2, INPUT_PULLUP); // IR 2 sensors pin
pinMode(ledPin, OUTPUT); // test LED pin
pinMode(3, OUTPUT); // PWM generator pin 490Hz
attachInterrupt(clockInt, clockCounter, CHANGE); // clockInt = pin 2 (interrupt 0) is our interrupt
// clockCounter function is called on a RISING clock edge
analogReference(DEFAULT);
Serial.begin(9600);
analogWrite(3, 127); // this starts our PWM 'clock' with a 50% duty cycle
Serial.println("Running");
}
// -------------- Interrupt 0 --------------------------------
/* clockCounter() is executed when the interrupt 0 is triggered
masterClock increments by one until one second (49 cycles) is reached,
then masterClock resets to zero
*/
void clockCounter() // called by interrupt
{
if ( Waiting == false ) // time counts only if train detected
{
masterClock ++; // each clock add 1 to masterclock count
if (masterClock == 98) // (490Hzx2 ) / 10
{
ledpinstate = ! ledpinstate; // Toggle LED status in memory every 1/10sec
th_sec = th_sec + 0.1; // count 1/10sec
masterClock = 0; // Reset count every 1/10 sec
}
}
return;
} // --------- Interrupt End -------
void loop()
{
if (RunningLeft == false)
{
Sensor1_Stat = digitalRead(IRsensor1); // read the IR sensor 1 input
delay (10); // optional debounce for test button
if (Sensor1_Stat != lastSensor1_Stat) // if the status has changed
{
switch (Sensor1_Stat)
{
case 0:
Serial.print("->RUN LEFT->");
RunningLeft = true;
Waiting = false;
lastSensor1_Stat = Sensor1_Stat; // save the current state as the last state
break;
case 1:
lastSensor1_Stat = Sensor1_Stat; // save the current state as the last state
break;
}
}
}
// ------------------------------------
if (RunningLeft == true)
{
Sensor2_Stat = digitalRead(IRsensor2); // read the IR sensor 1 input
delay (10); // optional debounce for test button
if (Sensor2_Stat != lastSensor2_Stat) // if the status has changed
{
switch (Sensor2_Stat)
{
case 0:
Serial.println("-STOP-");
RunningLeft = false;
Waiting = true;
temp = 10.0 / th_sec; // Sensors at 1/100 smile:1/100 of Km divided by time
// temp = 5.0 / th_sec; // Sensors at 1/50 smile:1/50 of Km divided by time
KmHour = temp * 3.6; // multiply by 3600 seconds = Km/hrs
Serial.print("Km/Hour= ");
Serial.println (KmHour);
MilesHour = KmHour / 1.6093 ; // conversion in Miles/hrs
Serial.print("Miles/Hour= ");
Serial.println (MilesHour);
lastSensor2_Stat = Sensor2_Stat; // save the current state as the last state
th_sec = 0;
break;
case 1:
lastSensor2_Stat = Sensor2_Stat; // save the current state as the last state
break;
}
}
}
if(th_sec == 600) // MAX=60 seconds
{
Serial.println("to much: max.60 sec.!");
th_sec = 0;
}
if (ledpinstate != oldstate) // LED memory changed = 1/10 second
{
digitalWrite(13, ledpinstate); // Switch LED every 1/10sec
oldstate = ledpinstate;
Serial.print(th_sec);
Serial.print("-");
}
}
« Last Edit: November 11, 2014, 05:31:02 PM by babbo_enzo »

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Speedometer for <$15
« Reply #2 on: November 13, 2014, 11:48:13 PM »
0
Thanks for posting this!  I plan to go through your code and compare it to my own (which I still need to clean up and post in the other thread).
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.