Measuring vehicle speed with microcontroller C167

Discussion in 'The Projects Forum' started by hc8th, Jul 14, 2009.

  1. hc8th

    Thread Starter New Member

    Jul 14, 2009
    Hi all, I'm new in here:). I'm currently working on a project of measuring vehicle speed, rpm and temp of a test vehicle. It has a speed sensor attached to the rear wheel, and it is giving me a digial pulse (not PWM) depending on freq, for e.g 10Hz = 5mph. I tested the sensor and its operating frequency is 0 to 135 Hz representing 0mph to 60mph, my problem is to set the timer counter of uc C167 to capture these pulses from the sensor and also to derive an algorithm to determine the speed.
    Can any1 help with C programming of the algorithm which it can determine the speed in response to the frequency measured?
    Many thanks to all :)
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    If 10 pulses/sec is 5 MPH, then it looks like pulse frequency/2 gives the result. For this you need an algorithm?
  3. SgtWookie


    Jul 17, 2007
    Actually, if you have a reasonably accurate timebase on the UC, just update the speedo every 1/2 second with the current count, and reset the count. No reason to bloat the code by including math.h.
  4. hc8th

    Thread Starter New Member

    Jul 14, 2009
    thanks for the reply, cos i m new to uc and very little experience with electronics, my puzzle is wht value is stored in the timer while capturing the input, and which one would i use if i want accuracy, the timer or counter???
  5. eblc1388

    AAC Fanatic!

    Nov 28, 2008
    One should note that counting input frequency cycles, especially at this low frequency would give mile per hour result very high error, especially at low mph.

    e.g. for physical 10Hz coming in, the one second counter may count 10 or 9, depending on the moment in time the timer starts/finishes counting. The maths would then give either 4.4 or 3.9mph. That's 11% error. Counting for 0.5 second is even worst.

    A better method is to clock the counter with a fast clock and use the input speed related frequency to gate(start/stop) the counter. The mph result is then the inverse of this count result after proper scaling.

    If the C167 MCU has timer capture feature, then it can be easily implemented. Else you have to monitor the speed signal input, wait for the correct moment to start the timer and stop it after detecting one complete input cycle. Then process the data.
  6. hc8th

    Thread Starter New Member

    Jul 14, 2009
    Thanks to all again :), can i also get the rpm readings with a known speed or i need to measure from the engine crankshaft??:confused: Also i aint sure how to set the prescaler of the uc to 0 to 135 Hz in order to match the speed sensor freq :confused:......