# Using a microcontroller as period counter and speed detector

Discussion in 'Embedded Systems and Microcontrollers' started by Manjugolu, Aug 4, 2014.

1. ### Manjugolu Thread Starter New Member

Jul 31, 2014
16
0
Hi, I am using a microcontroller to analyse the data coming from a sensor circuit. I need to determine the speed of a moving vehicle using the complete module. The maximum speed of the target is 100km/hr which means a frequency of 4320 Hz(this is doppler shifted frequency calculated) of the signal coming from sensor circuit has to be fed to the microcontroller which will finally provide speed of vehicle.
How shall we use a microcontroller as period counter and then provide speed of object. Also what kind of microcontroller shall be used.

2. ### mitko89 Member

Sep 20, 2012
123
19
If the speed is determined by the frequency, you need a uC with a timer, which will interrupt on any rising edge of the signal. You will then measure the time period between the interrupts so you can calculate frequency. As you know that 4320 Hz corresponds to 100 km/h, you can calculate the speed by calculating the ratio.

3. ### sirch2 Well-Known Member

Jan 21, 2013
1,008
351
What else does the MCU have to do; maths? drive a display? store the data? Basically any MCU can count clock ticks between high/low transitions and at 4kHz you may not even need to bother with interrupts, a modern MCU can do a lot in a millisecond.

4. ### mitko89 Member

Sep 20, 2012
123
19
Good question I though of asking, but Manjugolu didn't bother asking it so I didn't bother asking too.
Generally it's always better to use interrupt except for when they are needed for something else, which has higer priority for the design. In the case, it all depends on the question above.

5. ### sirch2 Well-Known Member

Jan 21, 2013
1,008
351
It's getting a bit off topic but I would disagree because it causes a jump in the code which adds complexity but each to his own.

6. ### mitko89 Member

Sep 20, 2012
123
19
But guarantees you will not miss an event, which is the more important in most of the situations. Adding a jump, in most of the cases, means you will need one instruction cycle more for entering the ISR and one more for returning from it. Two more cycles that grant your code stability, I will always accept this trade. I don't thing few more lines of code is that much of an issue if the matter is complexity.

7. ### Alec_t AAC Fanatic!

Sep 17, 2013
5,797
1,103
Some maths will be involved if you take several speed readings and average them to improve accuracy.