1
\$\begingroup\$

I am trying to make a speedometer cum odometer. I'm using a PIC based MCU. The speed I'm getting is as an analog input. (at 0 V, I get 0 km/h and at 18 V, I get 25 km/h). I can easily measure the speed using the ADC conversion. The issue I'm facing is how do I measure the distance here. Does anyone have any ideas for the same?

One thing I can think of is having a delay of 100 ms in the code and multiplying the instantaneous speed by 100 ms. Since all other function execution should take a maximum of 1-5 ms (including ADC conversion and sending bits to LCD displays etc), I can neglect that time and consider the distance here. But this method is not very exact. Does anyone have a better solution?

PFA the basic code

currentSpeedVoltage = ADC_Read(9);  
currentSpeedVoltage = (currentSpeedVoltage * 0.001220703125);   
//(0.001220703125 = 5/4096) (12 bit resolution for ADC).   
currentSpeed = (currentSpeedVoltage*25)/18;   
// assumed here that at 18 Volts, we show the speed as 25 km/hr.  
distance += (currentSpeedx100x5*(pow(10,-5))/18)/1000;
\$\endgroup\$
6
  • 3
    \$\begingroup\$ mS = millisiemens, ms = millisecond. I'm pretty sure you mean the latter, but you're typing the former. Likewise, Km = kelvin-meter, km = kilometer. \$\endgroup\$
    – Hearth
    Commented Oct 9, 2023 at 13:15
  • \$\begingroup\$ @Hearth yes I meant the latter. Corrected in the question as well. Thanks for pointing it out. \$\endgroup\$ Commented Oct 9, 2023 at 13:18
  • 3
    \$\begingroup\$ Hello and welcome ... the usual method for distance travelled is to count the turns of the wheel (with a magnetic / mechanical / optical sensor). It still has some inaccuracies due to the variation in circumference because of tyre pressure etc, which means it works best if you calibrate for your bike. You might consider calibrating the method you propose, and seeing how inaccurate it is, perhaps it's good enough? \$\endgroup\$
    – jonathanjo
    Commented Oct 9, 2023 at 13:20
  • 4
    \$\begingroup\$ I’m curious to know what speed sensor you’re using to get an analog output. \$\endgroup\$
    – Bryan
    Commented Oct 9, 2023 at 17:24
  • 4
    \$\begingroup\$ You need to move to a rotation based sensor, you are not going to get distance from this voltage thing, definitely not with a pic, but even with a full on processor, still would have a bad estimate. did a pic based speedo, trivial to do speed and distance with the right sensor for the job. \$\endgroup\$
    – old_timer
    Commented Oct 9, 2023 at 19:38

2 Answers 2

3
\$\begingroup\$

To get distance from speed you need to multiply speed by time. 1 hour at 25 km/h means you've gone 25 km. That only works for a constant speed though, if in that hour the speed varies, the distance will vary as well. To get more accuracy you need to use smaller units of time, the smaller the increment, the more accurate. Ultimately you would need infinitely small steps.

If you plot the varying speed over time you will end up with a curve, the area under the curve will represent the distance. So you have the area under a curve that you want to measure with infinitely small steps, what you use for that is calculus, more specifically integration.

What you might be able to do is use an op-amp integrator circuit to integrate the speed sensor voltage. Basically the sensor voltage charges a capacitor for a given time, say 100 ms, then you digitize the output and add it to your distance value, reset the integrator (discharge the cap) and repeat the process. If you keep the time it takes to read the output and reset the integrator to a small percentage of the sample time it should be relatively accurate.

\$\endgroup\$
2
\$\begingroup\$

Integrating speed to get distance has a rather serious problem with DC offset. That is, while not moving, ADC readings other than zero causes odometer creep (up or down).

With a crystal-controlled PIC, timing is much more precise than ADC. One might have an internal timer trigger an ADC reading at regular intervals only while motive power is engaged, to eliminate odometer creep while not moving. This would require another PIC input GPIO pin to sense motive power. One might also throw out odometer updates for speeds below a very low threshold (no extra I/O required).

Interrupt routine triggered by timer (or triggered by newADC_ready):

  • Subtract newest ADC result from previous: scale & add/subtract to odometer
  • transfer newest ADC result -> previous
  • return from interrupt

This method of taking difference helps reduce creep caused by ADC offset somewhat. However, ADC offset still affects the calibration of 18V:25kmph

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.