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