# Looking for chip to compare digital pulse rates

Discussion in 'General Electronics Chat' started by Tom1962, Oct 5, 2015.

1. ### Tom1962 Thread Starter New Member

Sep 10, 2012
19
0
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)?

2. ### dannyf Well-Known Member

Sep 13, 2015
2,196
417
Frequency-to-voltage converters + comparator

3. ### Lestraveled Well-Known Member

May 19, 2014
1,957
1,217
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.

4. ### MikeML AAC Fanatic!

Oct 2, 2009
5,450
1,066
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: Oct 6, 2015
5. ### Lestraveled Well-Known Member

May 19, 2014
1,957
1,217
I hear you...What can I say......I'm old school.

6. ### Alec_t AAC Fanatic!

Sep 17, 2013
5,956
1,131
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.

7. ### dannyf Well-Known Member

Sep 13, 2015
2,196
417
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,

8. ### crutschow Expert

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

9. ### Tom1962 Thread Starter New Member

Sep 10, 2012
19
0

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....

10. ### GopherT AAC Fanatic!

Nov 23, 2012
6,297
4,019
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.

11. ### crutschow Expert

Mar 14, 2008
13,469
3,356
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.

GopherT likes this.
12. ### AnalogKid Distinguished Member

Aug 1, 2013
4,678
1,295
Subtracting the voltages will not create a ratio. For that you will need an analog multiplier circuit.

ak

13. ### MikeML AAC Fanatic!

Oct 2, 2009
5,450
1,066
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.

14. ### Tom1962 Thread Starter New Member

Sep 10, 2012
19
0

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!

15. ### MikeML AAC Fanatic!

Oct 2, 2009
5,450
1,066
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.

16. ### kubeek AAC Fanatic!

Sep 20, 2005
4,686
805
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.

17. ### GopherT AAC Fanatic!

Nov 23, 2012
6,297
4,019
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.

18. ### Tom1962 Thread Starter New Member

Sep 10, 2012
19
0
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...

19. ### Tom1962 Thread Starter New Member

Sep 10, 2012
19
0
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?

20. ### GopherT AAC Fanatic!

Nov 23, 2012
6,297
4,019
This should be possible with a Phase Locked Loop. The
http://www.ti.com/lit/ds/symlink/cd74hc4046a.pdf
Could be used. Put one signal into COMP(in) and one signal into SIG(in), the PC2 pin can be biased with two 100k ohm resistors to set a mid-point (Vcc/2) when it is in the high-impedance tri-state. A voltage deviation at PC2 pin to +5 will mean one signal is faster and a deviation of PC2 pin to 0V will mean the other signal is faster.

Good luck.