# Looking for chip to compare digital pulse rates

#### Tom1962

Joined Sep 10, 2012
19
I have two signals, and each has a square wave, 0-5v, digital pulse that can vary from as slow as 10Hz to as fast as 3kHz. I need to compare the frequencies of these two signals and calculate their ratio. Think of it as the tachometers on two motors, where the goal is to say Motor A is spinning X% as fast as Motor B. Is there a chip out there designed to do this -- i.e., accept two digital pulses, compare their ratio, and output that ratio (via a serial output or otherwise)?

#### dannyf

Joined Sep 13, 2015
2,197
Frequency-to-voltage converters + comparator

#### Lestraveled

Joined May 19, 2014
1,946
As dannyf suggested, the LM331 is a precision frequency to voltage converter. Two of these will convert the speed of your two motors to voltages. You could then use a differential amplifier to subtract one from the other, thus creating a ratio or error voltage.

#### MikeML

Joined Oct 2, 2009
5,444
I'd be doing this with an Arduino mini. It is almost a single chip

It can send serial data to a PC via a USB port.

Last edited:

#### Lestraveled

Joined May 19, 2014
1,946
I'd be doing this with an Arduino mini. It is almost a single chip
I hear you...What can I say......I'm old school.

#### Alec_t

Joined Sep 17, 2013
12,150
I doubt you'd find a single-chip off-the-shelf solution. A micro is the obvious way to go but, as suggested, a couple of frequency-to-voltage converter ICs would get you part of the way. To find the ratio of their outputs you could use a variable gain amplifier and a comparator in a feedback loop.

#### dannyf

Joined Sep 13, 2015
2,197
If you are willing to make some assumptions, you probably can do this fairly simply: use one pulse train to charge up a capacitor, and another to either discharge it or reset the circuitry. The voltage across that capacitor tells you something about the two pulse trains,

#### crutschow

Joined Mar 14, 2008
27,408
Is this going to be part of a feedback control loop?
How fast (often) and how accurately do you do to determine this ratio?

#### Tom1962

Joined Sep 10, 2012
19
Is this going to be part of a feedback control loop?
How fast (often) and how accurately do you do to determine this ratio?

Not part of a feedback control loop per se, but system will be looking for specific ratios and trigger other events when those specific ratios exist. As for how accurate is needs to be, that will take a little trial and error, but I do not believe it needs to be highly precise. For example, if the pulse ratio is 1:2, and it gets measured as 1:2.02, I have to believe that's close enough. Also, if the error is consistent -- the same every time -- then anything in the ball park is good enough....

#### GopherT

Joined Nov 23, 2012
8,012
Not part of a feedback control loop per se, but system will be looking for specific ratios and trigger other events when those specific ratios exist. As for how accurate is needs to be, that will take a little trial and error, but I do not believe it needs to be highly precise. For example, if the pulse ratio is 1:2, and it gets measured as 1:2.02, I have to believe that's close enough. Also, if the error is consistent -- the same every time -- then anything in the ball park is good enough....
A 2:1 is an easy yes/no. What if you are running 100 pulses per second on one train and 101 pulses per second on another? It will take a whole second to determine that there is a difference. A difference between 100 and 100.1 Hz will be a real bear to detect (take 10 seconds to identify a 1 pulse difference). That assumes that the two trains are stable over a 10 second period. Is that OK?

If not, then we need a method to time the period of each pulse by adding a third clock (standard reference). Then you can count how many of the standard reference pulses occur between rising edges of each of your pulse trains. The pulse train that gets the most pulses, wins.

#### crutschow

Joined Mar 14, 2008
27,408
GopherT is saying that for low frequencies you will get much faster and more accurate results by measuring the signal pulse period rather than the pulse frequency, especially if you are going as low as 10Hz.

#### AnalogKid

Joined Aug 1, 2013
9,287
As dannyf suggested, the LM331 is a precision frequency to voltage converter. Two of these will convert the speed of your two motors to voltages. You could then use a differential amplifier to subtract one from the other, thus creating a ratio or error voltage.
Subtracting the voltages will not create a ratio. For that you will need an analog multiplier circuit.

ak

#### MikeML

Joined Oct 2, 2009
5,444
GopherT is saying that for low frequencies you will get much faster and more accurate results by measuring the signal pulse period rather than the pulse frequency, especially if you are going as low as 10Hz.
In a micro, I would measure the periods of the two pulse trains by using the respective pulses to gate a much higher frequency clock (possibly derived from the cpu clock source) into two counting-registers (timers). Since Period =1/Frequency, it is just as easy to calculute P2/P1 as it is F1/F2 to get the ratio. The ratio could be updated at a rate of the longer period.

#### Tom1962

Joined Sep 10, 2012
19
In a micro, I would measure the periods of the two pulse trains by using the respective pulses to gate a much higher frequency clock (possibly derived from the cpu clock source) into two counting-registers (timers). Since Period =1/Frequency, it is just as easy to calculute P2/P1 as it is F1/F2 to get the ratio. The ratio could be updated at a rate of the longer period.

This is where I started with an atmega328, using hardware interrupts to time the duration between pulses. My concern is that I might miss some pulses on the lower priority interrupt pin if it triggers when the higher priority interrupt is processing. The interrupt service routine is very tight -- less than 10 clock cycles -- but that still leaves times when the other interrupt is disabled. The F to V approach seems clunky, so I may keep working on this code instead -- perhaps by testing the pulse durations one at a time via an interrupt, disabling the non-active interrupt at the time. That would mean the frequencies are not tested at the same time, but it should be fine for my purposes if the two tests are done within a few ms of each other. Appreciate all the feedback -- keep it coming!

#### MikeML

Joined Oct 2, 2009
5,444
Why would you lose any pulses, even if you turn off the second interrupt while processing the first?

The second pulse event is latched into a hardware flip-flop and is waiting to cause an immediate second interrupt as you re-enable interrupts after processing the first one.

The only thing you would ever lose is if two pulses on the same interrupt came so close together that the second occurred before you cleared the flop while servicing the first one.

#### kubeek

Joined Sep 20, 2005
5,757
All yo need is a microcontroller with three timer/counters. One is set as a timer and makes an interrupt say every 1s, the other two increment on each pulse. When the interrupt is up you read the values and calculate the ration. Accuracy is going to be +/- 1 count, which might be a lot with signals close to the 10Hz.

#### GopherT

Joined Nov 23, 2012
8,012
His signals are 10 RPM to 3000 RPM. That is 0.166 Hz to 50 Hz.

The OP should use a 250kHz clock to measure the pulse period. That way, he can differentiate between 2999 and 3000 RPM and anything slower. Also, should be able to differentiate between 0.166 and 0.165 Hz when near 10 RPM.

The OP should simply gate his Square Wave 1 and count clock cycles only when his input is LOW using TIMER 1, then, sometime in the 0.01 seconds or longer when the pulse is high, he can store it into a register (that corresponds to many tens of thousands of clock cycles so no need to worry about overlapping interrupts). Do the same for the SQUARE WAVE2. Each time you load a value into the register, compare it to the value of the other clock. Back and forth, back and forth - updating a single output as high or low depending on whether or not SQUAREWAVE 1 or SQUAREWAVE 2 has a higher count.

#### Tom1962

Joined Sep 10, 2012
19
Are you sure this is true on the Atmega328? If so, I'm worrying about nothing. I got the impression based on my crack google research that lower priority interrupts were ignored if they came in while processing the ISR of a higher priority interrupt. If that's not the case, then I can just attach each signal to its own interrupt pin and time the durations. An occasional 5uS delay if an interrupt has to wait isn't going to affect anything. The sensors creating the pulse probably have more inconsistencies than that...

Why would you lose any pulses, even if you turn off the second interrupt while processing the first?

The second pulse event is latched into a hardware flip-flop and is waiting to cause an immediate second interrupt as you re-enable interrupts after processing the first one.

The only thing you would ever lose is if two pulses on the same interrupt came so close together that the second occurred before you cleared the flop while servicing the first one.

#### Tom1962

Joined Sep 10, 2012
19
His signals are 10 RPM to 3000 RPM. That is 0.166 Hz to 50 Hz.

The OP should use a 250kHz clock to measure the pulse period. That way, he can differentiate between 2999 and 3000 RPM and anything slower. Also, should be able to differentiate between 0.166 and 0.165 Hz when near 10 RPM.

The OP should simply gate his Square Wave 1 and count clock cycles only when his input is LOW using TIMER 1, then, sometime in the 0.01 seconds or longer when the pulse is high, he can store it into a register (that corresponds to many tens of thousands of clock cycles so no need to worry about overlapping interrupts). Do the same for the SQUARE WAVE2. Each time you load a value into the register, compare it to the value of the other clock. Back and forth, back and forth - updating a single output as high or low depending on whether or not SQUAREWAVE 1 or SQUAREWAVE 2 has a higher count.
For what it's worth, my examples were in Hz, not RPM, so anything under 10Hz or so would be irrelevant (anything less than 20Hz probably means something is wrong anyway). I'm not familiar enough with Timer interrupts to understand this suggestion, but will study up. Can you explain what you mean when you say to "gate" the square wave and count clock cycles with TIMER1?

#### GopherT

Joined Nov 23, 2012
8,012
This should be possible with a Phase Locked Loop. The