Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measuring Vehicle Speed using C167 microcontroller

Not open for further replies.


New Member
Hi all,

I'm new to this forum :), I'm currently working on one of my projects in which I'm using a micrcontroller (Infineon C167) to capture the output signal from a speed sensor, this sensor is producing a digital pulse (not PWM) depending on the changes of frequency, e.g. 10Hz = 4mph. I managed to simulate the speed sensor output and the response frequency is 0 to 135 Hz representing from 0mph to 60mph. But I'm struggling to derive an algorithm using C language for the microcontroller to calculate the speed, can anyone help please? plus can I also calculate the RPM with a known speed?
Sometimes in development, it's easier to solve a problem in hardware instead of in software, especially when an end product becomes urgent. Later on in production, the software can be done, the hardware can be linked out on the PCB, and the parts ommitted.

Here's an example hardware solution, if you get the pulse, differentiate it, trigger a monostable, then average the output with an R & C. The resulting DC voltage is proportional to roadspeed, can be fed into the infineon's adc input. I'll let someone else do a schematic if this is an option you'd like to try.

Otherwise in software, you'll have to get your head round the infineon's counter/timer, interrupt service routine triggered by the input frequency, and read the period from the counter/timer. Me, I'd do a prototype in hardware because I'm lazy, then let a softy come up with the equivalent, so I can re-route the PCB minus the hardware I did, or leave it in if the softy is too busy that week.
Thanks for the reply Marc,

The idea is good but I 'm only allow to work on the software path, I played around with the timer counter of the c167 and try to get the results on some LEDs just to test to see if it works or not but the result was not as expected....:( so i guess the algorithm i m working on isnt correct, and wud like know if theres any better way to work it out in terms of software :)
The most important question to ask is how long are you willing to wait for the Speed Reading? Another way of asking this is how long an interval are you willing to count pulses for? If you want a new answer once a minute, that is one thing. If you need a new speed reading once a second, that is another.

You need to look up "frequency measurement" vs "period measurement"
OK, a S/W solution :)

Take in the SS pulse into a suitable c167 IO.

Config a counter/timer to measure the period with.

Config this IO to trigger an ISR, by a ChangeOfState the input.

At the front end of the (triggered) ISR, if it's been a H>L, then return from I. If it's a L>H, then read off the period counter into a suitable RAM register and reset timer, before RTFI.

In your exec loop (or whatever you do) you'll find the period in the RAM register. If you want a frequency, obviously you'll have to do math with it.

Hope this helps.
thanks Mike and Marc for the reply, I had already set up the microcontroller to my requirement in order to capture the incoming signal from SS but my problem is the maths bit, I'm circulating around try to convert those captured signals into relative speed.....
Speed is just "Distance traveled" per "unit time"

Suppose the circumference of the (rear?) driven wheel is 4 ft. Then one revolution of the driven wheel means 4 ft of travel.

Suppose the rear-end gear ratio is 3:1, meaning three revolutions of the drive shaft for one revolution of the rear wheel.

Suppose your speed pick-up is two pulses per drive-shaft revolution.

Suppose the integration time you use in your uc is one second.

Now suppose you see 27 pulses in one second.

27 pulses/sec × 1DSrev/2pulses × 1Wrev/3DSrev × 4ft/1Wrev

=(27 × 4)/(2 × 3) ft/sec = 18 ft/sec

To convert to miles per hour:

18 ft/sec × 3600sec/1hr × 1 mile/5280ft = (18 ×3600)/5280 mi/hr
= 12.27mi/hr.

Note that with an integration time of only 1 sec, your resolution is such that you can only capture 26, 27, or 28 pulse in one second near 12mph. That is kinda coarse. That is what elicited my look up "period measurement" comment in the earlier post.
thanks v much Mike, I aint really that good at C programming and by the way can i get the rpm as well jst from the signal that i have with the SS? cheers^^
Last edited:
"Period measurement" seems the natural method for this one. Measuring it as a frequency by comparison, Mike has done an analysis on it.

We already know speed sensor = 0 to 135 Hz representing from 0mph to 60mph.

I believe OP has already managed to capture (in a register) the time period, e.g. between each rising edge of the SS. From there on it's math. I don't do C myself, never got into it, I'm assembler code.

The way I plan out math functions in microcontroller software, is to do it on MS Excel, trying out the math on a spreadsheet (example attached). Once it's working nicely, I code it on the microcontroller and it always works first time.


  • TO-BEOutput
    14.4 KB · Views: 210
thanks again Marc, really appreciate it, not a bad idea to test it on excel first, however i still have another question with the uc C167, to set the timer counter, i tried adjusting the prescaler but seems its not matching the SS frequency?? I tried resolution of /8, /16....but still the captured results seem change too quickly? I'm detecting the rising edge of the incoming pulses from SS.....
I notice you use the word 'frequency' a bit more than I'd expect. You are measuring the period between SS rising edges aren't you?
what I'm trying to do is to match the frequency of uc with the incoming SS frequency, I'm detecting the rising edge and just measuring the freq of the SS, so if uc detected lets say 5Hz, that will corresponds to 3mph, not sure if this is a gd idea cos at da mo, i m jst using a look-up table rather than writing a algorithm which i guess is less accurate........
Oh, are you making an local oscillator inside the uc track the SS signal? That's a bit ambitious of you...

... Nah, just count the timer ticks that occur, between incoming SS pulses. Then you have the period (1 / Hz)
yer i tried few methods but seems not working so i try this simpler method and also i m not that familiar with uc
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips