myDAQ Student Projects

Showing results for 
Search instead for 
Did you mean: 

PWM (square-wave) Frequency and Duty Cycle Detection on Non-Counter DIO


PWM (pulse-width modulation) is a common technique for exciting sensors (such as ultrasonic sensors) and powering actuators (including motors, speakers, and lights). Sensors that generate PWM signals include digital Hall-effect sensors (such as those found in dynamometers), simple microphones (known as delta-sigma microphones), digital counters, and motor encoders.

For low-cost data acquisition devices, counter inputs are limited, in some cases making it difficult to both generate and detect PWM signals on the same device. Since digital counter inputs are hardware triggered, they are preferable when measuring PWM signals; however, if not enough counter inputs are available, digital inputs can be combined with signal processing to measure frequency and duty cycle of a PWM signal.

The VI below measures the fundamental frequency and duty cycle of a periodic square-wave or PWM signal using a standard digital (non-counter) input on a data acquisition device.


  • NI myDAQ (or other data acquisition device such as a USB-6008 or USB-6009)
  • LabVIEW 2009 or later
  • NI DAQmx


Frequency detection is an atypical use of a digital line and elicits some sampling and processing overhead, and will only be accurate with sufficient sample lengths and input signals that are periodic or pseudoperiodic. In testing with NI myDAQ, square waves up to 400 Hz were reliably measured. Aliasing will occur for higher-frequency signals, and digital counter inputs should be used instead.

The DAQmx Pulse Train Frequency VI is written similar to a DAQ Assistant VI; references to the data acquisition device are automatically opened, but should be closed at program termination. See the example, PWM Input Test to see its use.


1.0 (2010-08-10): Initial release.

1.1 (2010-08-24): Moved files into single .zip archive.


Great idea, I got a lot out of this example. I was wondering if there was an alternative to "NI_MAPro.lvlib: Extract Single Tone Information" and ."NI_MAPro.lvlib: Extract Single Tone Information Polymorphic subVI." I do not have these plugins.

I have only one counter port on my daq board and I want to get RPM and GPM data, so I was looking for something like this to count using a digital I/O.

NI Employee (retired)

Thanks, MechTom. Extract Single Tone basically performs a fourier transform (FFT) on the signal, and returns the frequency with the highest amplitude. You should be able to recreate the functionality.

Europa VII

Do you know if USB-6009 supports counters to generate PWM? If not, how could I generate one?

NI Employee (retired)

I believe the USB-6008 / 6009 only have one counter, and two are required to generate PWM. You can use software timing, but this will have some jitter and will be limited to low frequencies.