Pulse Width Modulation (PWM) Using NI-DAQmx and LabVIEW
Table of Contents
The National Instruments Getting Started with NI-DAQmx Series is aimed at helping you learn NI-DAQmx programming fundamentals. Through video and text tutorials, this series will take you from verifying your device's operation in Measurement & Automation Explorer (MAX) to programming data acquisition applications using LabVIEW. It is intended for both the beginner who wants to learn how to use the DAQ Assistant, as well as the experienced user who wishes to take advantage of advanced NI-DAQmx functionality.
Introduction to Pulse Width Modulation
Pulse width modulation (PWM) is a technique in which a series of digital pulses is used to control an analog circuit. The length and frequency of these pulses determines the total power delivered to the circuit. PWM signals are most commonly used to control DC motors, but have many other applications ranging from controlling valves or pumps to adjusting the brightness of an LED. The digital pulse train that makes up a PWM signal has a fixed frequency and varies the pulse width to alter the average power of the signal. The ratio of the pulse width to the period is referred to as the duty cycle of the signal. For example, if a PWM signal has a 10 ms period and its pulses are 2 ms long, that signal is said to have a 20 percent duty cycle. Figure 1 shows three PWM signals with different duty cycles. For a more thorough look into pulse width modulation, visit the link at the bottom of this page.

Figure 1: PWM Signals with Different Duty Cycles
National Instruments PWM Hardware
PWM signals can be generated as a digital signal, using counters or digital output line(s), or as an analog signal, using for instance, an arbitrary waveform generator or an RF signal generator.
Several National Instruments multifunction data acquisition (DAQ) devices are capable of producing PWM signals. This includes the E, S, B and the new M Series devices, which represent the next generation of multifunction DAQ from National Instruments. In addition, NI 660x counter/timer devices, as well as NI 653x digital devices can also be used.
Although the remainder of this article focuses on using counters to generate PWM signals, it's worth noting that National Instruments hardware and software also supports generation of PWM signals using digital line(s) or analog output options.
Programming PWM in LabVIEW Using NI-DAQmx
NI LabVIEW is the graphical development environment for creating flexible and scalable test, measurement, and control applications rapidly and at minimal cost. With LabVIEW, engineers and scientists interface with real-world signals, analyze data for meaningful information, and share results and applications. The LabVIEW graphical development environment, combined with NI-DAQmx, gives you the tools needed to easily construct applications using counters to generate PWM signals.
The first step to create a PWM signal is to produce a simple digital pulse train. Below is an example of using LabVIEW to program a counter on an M Series device to create a digital pulse train signal using NI-DAQmx. Each step of the program, indicated by a number at the bottom of the figure, is described below.
- Create and initialize a counter output channel to produce a pulse in terms of frequency using the DAQmx Create Channel VI. Select appropriate inputs for Duty Cycle, Frequency, Counter(s), Idle State and Initial Delay.
- Use the DAQmx Timing VI to configure the duration of the pulse generation. The Implicit instance should be used when no sample timing is needed, such as in counter tasks like pulse train generation. Additionally, choose Continuous as the sample mode.
- Call the DAQmx Start VI. This VI begins the pulse train generation.
- Loop continuously until the user presses the Stop button. Check for errors using the DAQmx Is Task Done VI. Add a Wait Until Next Millisecond Multiple VI to introduce a small time delay to the loop. This will prevent the loop from executing as fast as possible, consuming unnecessary processor resources.
- Call the DAQmx Clear Task VI to clear the task.
- Check for and display errors using the Simple Error Handler VI.
To create a PWM application from the simple pulse train output program, we need to have the ability to change the duty cycle dynamically. To do this, only one additional VI, a local variable, and a case structure with some simple logic is needed. With these additions, the duty cycle control on the front panel is checked every iteration of the loop. If it has been changed, the new duty cycle is set using the DAQmx Write VI. If the duty cycle has not been changed, the false case of the case structure executes, and nothing is updated. Figure 3 shows the complete PWM application.
Advanced PWM Applications in LabVIEW
One easily added feature is a start trigger to begin the pulse train. This application could be useful if you wanted to start a motor or turn on a light with the flip of a switch. A digital start trigger can be added using only one VI, as shown in Figure 4. To use a digital trigger, specify the trigger source and trigger edge.
Related Links
For a more thorough look at PWM visit: Introduction to Pulse Width Modulation (PWM)
Reader Comments | Submit a comment »
Works Well
I used this method to control a hobby servo -
in fact two of them and it works really well.
I had a 6024E DAQ card. Trouble is that you
only get 2 counters with this card. Is there
a card that gives you more counters and no
A/D D/A - I only need the counters! This is
an expensive solution but good.
- t.j.moir@massey.ac.nz - Oct 3, 2008
painfully complicated?
This seems painfully complicated compared to
the simple pwm solution available through the
NI-DAQ legacy sub-vi "Generate Pulse-Train,"
which at least to me seemed robust and
problem free. Am I missing something? What
is the benefit of this DAQmx approach that
justifies its complications? Now that I am
using the new version of LabVIEW is it worth
it to me to convert old VI's that have pwm in
them to this approach or should I just punt
and install DAQ legacy?
- mrb, cwu. braunst@cwu.edu - May 3, 2007
ms updating of counterfrequency
Whenever you want to update the
counter frequency fast (like 1 ms, with a
pulse width of 2us), you might need to
write the update of the frequency in the
initialization once. I think as to open the
dll. When you do this, you can avoid
the "The pulse generation with previous
property settings must complete a full
cycle before the property can be
updated." error.
For example, for figure 3, you would
copy the 'daqmx write.vi' block to just
after the 'daqmx start.vi' block, outside of
the while loop.
- Nov 23, 2006
It would be fine if the author could give an
example how to realize an PWM signal with the
Digital Outputs as well. Using a counter
seems quite simple because it inhibits a time
signal, but the Digital Outs don´t. Is there
a possibility to give an example for this
case, too?
- alex_bcn@fantasymail.de - May 8, 2006
Please create a PDF file and with a date.
Please provide an example of typical
pulsewidth hardware ranges. For instance my
2004 DAQPAD 6015 only does a duty cycle
down to 7% duty cycle, not 0.1%. From this
website it implies with any NI DAQ hardware
you can have a 0.1% to 99% duty cycle.
FYI, cameras requires external triggers
with < 1% duty cycle..
- Victor Villavicencio, Northrop Grumman. victorvilla93@hotmail.com - Mar 27, 2006
does not print properly
Unable to print this document properly,
wither directly or through Acrobat writer.
Right Hand side gets cut off.
- Mar 31, 2005
width combersom in IE6.0
I took the time to reformat in Word, in my
browser I had to scroll left and right to
read this article.
MfG,
Jo
- Mar 31, 2005
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).



