LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best approach for measuring PWM duty cycle in LabVIEW

Solved!
Go to solution

I'm looking into the best way to measure the duty cycle and frequency of an externally generated PWM signal in LabVIEW.

This is currently a design question rather than a troubleshooting issue, so I don't have an error message or VI to attach yet. I'm trying to understand the recommended NI hardware and LabVIEW approach before building the test setup.

 

Would a counter/timer input normally be the preferred method for measuring PWM frequency and duty cycle, rather than acquiring the waveform continuously with an analogue input?

 

I'm also interested in how the measurement would normally be passed into a LabVIEW VI for displaying the duty cycle and frequency in real time.

 

If anyone has implemented something similar, I'd appreciate suggestions on the appropriate NI hardware and LabVIEW functions to start with.

0 Kudos
Message 1 of 9
(1,471 Views)

Hi cedricuk,

 


@cedricuk wrote:

I'm looking into the best way to measure the duty cycle and frequency of an externally generated PWM signal in LabVIEW.

 

If anyone has implemented something similar, I'd appreciate suggestions on the appropriate NI hardware and LabVIEW functions to start with.


How familiar are you with DAQmx?

Did you open any of those DAQmx example VIs? There are several VIs about handling frequency/dutycycle with DAQmx…

 

Asking for "best" without mentioning any system parameters is quite nonsense! What exactly do you want to work with? Which kind of signal? Frequencies, voltage levels, …

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 9
(1,461 Views)

I totally agree with GerdW that you have not supplied enough information to us.

 

However, as a few general notes....

 

The main two types of input are digital and analog, and you haven't even committed to choosing one of those yet.

 

Digital is far less ambiguous to work with but it presupposes that the signal you're getting from whatever you are measuring is well-regulated.  By "well-regulated" I mean that it has rise/fall times that are consistent and quick compared to the pulse length, and the pulse magnitude is both consistent and its high and low levels are levels that the digital input hardware is compatible with.  Going from 0V to 5V is pretty common, but not the only option.

 

A "counter" is a much more specific version of a digital signal where it's just looking for the number of (usually) low-to-high transitions and nothing else.  You might be able to extract a rough frequency from that if you measure for long enough at a high enough sample rate compared to the expected frequency, but not duty cycle.

 

If you're trying to use digital to measure duty cycle, you'll need a measurement rate that is your max expected frequency divided by the resolution of the duty cycle you're looking for.  For instance, if you're measuring a 2 kHz signal and you want to measure duty cycle to the tenth of a percent (0.001), then you'd need a (2000Hz÷0.001)= 2 MHz sample rate on the digital input to measure that.

 

Duty cycle and frequency are much more commonly measured with analog input, though.  For that, NI hardware usually accepts up to 10 V and does not need a specific level for its low and high magnitude, so if you have a signal that goes from 1V to 1.5V and back, that can be processed just fine.  For analog input, you generally want to have a sample rate that is a bare minimum of 10 samples (and preferably more) for the shortest pulse you expect to see.  Not the pulse width of the "high" level, but the complete pulse, from low to high and back again, so [(expected rise time) + (expected fall time) + (shortest pulse length you expect)] should be 10 samples minimum.

0 Kudos
Message 3 of 9
(1,419 Views)
Solution
Accepted by topic author cedricuk

@Kyle97330 wrote:

A "counter" is a much more specific version of a digital signal where it's just looking for the number of (usually) low-to-high transitions and nothing else.  You might be able to extract a rough frequency from that if you measure for long enough at a high enough sample rate compared to the expected frequency, but not duty cycle.

I can't recall the last time I've disagreed with @Kyle97330, so maybe this is the very first.  Counter hardware is actually *much* more capable than the remarks above suggest.  Caveat: as stated by Kyle & others though, a counter would only be appropriate if your PWM signal is (or can be made to be) compatible in terms of voltage levels, transition speed, etc.

 

Supposing you have a counter-compatible signal, you can measure both frequency and duty cycle with very high precision and rather high accuracy, both almost certain to be better than results from an analog approach.   While pretty much true that a counter "merely" concerns itself with digital transitions, it can respond to both your PWM transitions AND the transitions from a digital hardware clock pulsetrain.  There are many available measurement modes for counter tasks, including ones where you measure both high and low times which can be used to calculate freq and duty cycle very precisely.  Much of the precision comes from the very high hardware clock frequency, typically 100 MHz in common NI data acq devices.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 4 of 9
(1,333 Views)

Thanks, GerdW. You're right that I should have included more specific signal parameters. I'll look through the DAQmx frequency and duty-cycle example VIs and narrow down the hardware requirements first. I appreciate the guidance.

0 Kudos
Message 5 of 9
(1,237 Views)

Thanks for explaining the differences between digital and analogue acquisition, especially the sampling-rate requirements and signal-level considerations. That gives me a much clearer starting point for evaluating the measurement approach.

0 Kudos
Message 6 of 9
(1,236 Views)

I’m trying to understand how to choose an appropriate sampling rate when capturing a digital pulse signal using NI DAQ hardware.

 

If the signal has a relatively short pulse width, what factors should determine the minimum sampling rate needed to capture it reliably? Is it sufficient to base the rate on the pulse width, or should signal rise time and the required timing resolution also be considered?

 

I’d appreciate any practical guidance on choosing the sampling rate for this type of measurement.

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

Hi cedricuk,

 

you already had a lot of suggestions on that topic…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 9
(1,207 Views)

@cedricuk wrote:

I’m trying to understand how to choose an appropriate sampling rate when capturing a digital pulse signal using NI DAQ hardware.

And this is one of the pleasant surprises about counter tasks -- the typical rules about sample rate frequently don't apply!  This is a consequence of the fact that they react and "measure" based on digital transitions and logic circuitry.

 

Counter tasks are pretty unique in that sense.  In fact, in many cases they produce better measurements with *slower* sample rates rather than faster ones due to quantization effects.  Here's a couple of my previous posts that touch on this subject:

- Nyquist debunking

- Quantization error considerations

 

I'd suggest you check out pulse measurement mode that can report freq and duty cycle data directly.  Most currently available NI devices with counters will support this mode (older devices required some simple-enough post-processing).

 

 

-Kevin P

 

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 9 of 9
(1,031 Views)