From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pulse Frequency Measurement Using Analog Input

Solved!
Go to solution

Hi All,

 

I've searched the forums and found some useful threads but I can't figure out how to apply them to my problem. I'm trying to read pulse outputs from several flowmeters, but my DAQ only has two counters and I'm using them to control pumps. I'm unable to obtain more hardware, so I am trying to measure the pulse frequency using the analog inputs on the DAQ. The code works, but I can only obtain a resolution of 10Hz compared to the digital counter input which measures the frequency to several digits of precision.

 

I've attached my code. Analog Freq 1.png

Analog Freq 2.pngThe first while loop is just a digital counter task recording the pulse frequency so I can compare it. The second while loop reads 100 analog voltage samples and an internal for loop checks each sample to see the voltage has changed from the previous iteration. If the voltage is now greater than 0.5V there is a positive edge and the 'Pulse Count' increases, otherwise it stays the same. The - || > 0.5 bit just stops small fluctuations from being counted. The count is then divided by 0.05s (the sample collection time) to calculate the frequency. The digital and analog readings are similar, but the digital one is always higher resolution, i.e. 138.57Hz digital and 140Hz analog. Can anyone tell me how I can improve the resolution?

 

Cheers in advance for your help, any advice will be greatly appreciated.

 

Dan

 

0 Kudos
Message 1 of 9
(7,136 Views)

The only way to increase your frequency resolution is to sample for a longer period of time. Your pulse count is an integer which you divide by the sampling interval of 0.05 seconds, which is the same as multiplying the pulse count by 20 Hz. That is, each pulse you count adds 20 Hz to the frequency count. With a longer sampling interval you'd get finer resolution: 0.1 s would give 10 Hz resolution, 0.5 s would give 2 Hz, etc.

 

One thing that I noticed is that you could get rid of the "not equal" comparison between the current and previous values in your For loop, since that will always be true if the other comparison is true (that is, if the difference between them is greater than 0.5). If the current and previous readings differ by more than 0.5 the signal has either risen or fallen between the two samples being taken, then if the current reading is greater than 0.5 you increment the pulse count. I don't know what your incoming signal looks like, but if it rises or falls slowly enough and remains above 0.5 that whole time, you'll continuously increment the pulse counter as long as it continues to rise or fall.

 

 

0 Kudos
Message 2 of 9
(7,109 Views)
Solution
Accepted by topic author GrassNinja

For measuring the pulse count and frequency, look on the Signal Processing->Wfm Measurement palette.  There are several VIs here that will simplify your code. 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 9
(7,104 Views)

Thank you both for your help! The 'Pulse Measurements.vi' seemed to do the trick once I played around with it. It gives me an error when the flowmeter isn't running but I've just added an error handler to ignore it.

0 Kudos
Message 4 of 9
(7,093 Views)

I am encoutering a similar problem.

 

Would you mind sharing your vi?

0 Kudos
Message 5 of 9
(7,036 Views)

Hi Koenve, 

 

I am away from work at the moment but I will find the VI when I am back tomorrow. 

 

I ended up creating two VIs, one for high frequency measurements and one for low frequency measurements. What range of frequencies are you trying to measure? 

0 Kudos
Message 6 of 9
(7,024 Views)

That would be really helpful, thanks for you reply!

 

I am trying to measure frequencies below 100 Hz.

 

0 Kudos
Message 7 of 9
(7,015 Views)

Hi Koenve,

 

For low frequencies I did something slightly different. I've attached two VIs, one is the subVI for calculating the frequency and one is the main VI for acquiring data.

Main VI.pngIn the main VI (above) I have disabled the DAQ task and I'm just using the Simulate Signal.vi to test it. If you'd like to use the DAQ task just remove the diagram disable structures and wire the waveform output of the DAQ Read.vi to the FT01 Signal Graph instead. The important thing here is that the task has a sample rate of 250/s and reads 500 samples. This means that it reads 2 seconds worth of data so that it can calculate low frequencies.subvi.png

In the subVI (above), the waveform is unbundled into an array of data points. The Threshold Detector.vi from the Signal Operation Palette returns array containing the sample numbers at which point the signal passed the threshold (for my 0-5V signal I have set the threshold as 2). For example, a 5Hz signal across two seconds with 500 samples will cross the threshold on the 50th sample, 100th sample, 150th sample etc.

 

The for loop then calculates the difference between each of these points and creates an array. A subset of this array is taken to avoid using the first couple of elements because they vary too much. The Mean.vi then calculates the average number of samples between threshold crossings. This number is multiplied by the dt of the waveform (time between samples) to obtain the average period. The reciprocal is then calculated to find the average frequency.

 

For very low frequencies (<2Hz) the array produced by the for loop is very small. In this case, the program just uses the second element of the array to calculate the frequency. For even lower frequencies (<0.5Hz) the code can't accurately calculate the frequency so it is just set to 0.

 

 

I'm not sure if that explanation is very clear but the code should work well for signals between 100Hz and around 1Hz. 

 

 

 

Download All
0 Kudos
Message 8 of 9
(6,997 Views)

Can you show me your hardware connection? I always get 50 HZ no matter how i change the speed of water flow

0 Kudos
Message 9 of 9
(3,114 Views)