Pulse Width Modulation (PWM) Using NI-DAQmx and LabVIEW

Publish Date: Apr 25, 2016 | 89 Ratings | 3.24 out of 5 | Print | 13 Customer Reviews | Submit your review

Table of Contents

  1. Introduction to Pulse Width Modulation
  2. National Instruments PWM Hardware
  3. Programming PWM in LabVIEW Using NI-DAQmx
  4. Advanced PWM Applications in LabVIEW
  5.  Related Links

 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.

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

Back to Top

2. 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, M and the new X 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. 

Back to Top

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

Figure 2: Block Diagram of LabVIEW Pulse Train Program using NI-DAQmx
  1. 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.
  2. 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.
  3. Call the DAQmx Start VI. This VI begins the pulse train generation.
  4. 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.
  5. Call the DAQmx Clear Task VI to clear the task.
  6. 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.

Figure 3: Complete PWM application

Back to Top

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

Figure 4: LabVIEW PWM program with digital start trigger

Back to Top

5.  Related Links

For a more thorough look at PWM visit: Introduction to Pulse Width Modulation (PWM)

Back to Top

Customer Reviews
13 Reviews | Submit your review

PWM control through PID  - Apr 10, 2017

How we can utilize the PID controller for controlling the speed of the DC motor by taking the feedback of RPM, I am facing problem of controlling the PWM pulse through PID controller. How can we control PWM pulse through PID. Plz give suggestions. Regds

Good starting point  - Dec 10, 2016

Please elaborate more on PWM=0 and PWM =1. Currently it seems that you can not set PWM=0. This can be a problem if you control a heater using a PWM signal. Without special measures, you will not be able to shut it down.

examples add delays in signal  - Apr 4, 2016

I've found that this continual update adds about 10 millisecs between each duty cycle change. I think it's due to Windows (I'm running Windows 7 Pro 6-bit on a PXI-8840 core). I got it down to 1 millisecond by removing the Wait.vi in the While loop. I wish there was way to change duty cycle and how long the duty cycle will last while keeping the frequency constant.

  - Mar 14, 2012

which daq is suitable for PWM generation and the one that has atleast 5 counter outputs?

  - Feb 6, 2012

hello .. how can i generat a pwm with hardware frequency , that depend on the frequency of NI DAQ(for example: 6216)

can the old pulse config vi change the duty cycle of a running counter?  - May 26, 2010

Just for interest: with the old generate pulse drain vi one could not change the duty cycle of a running counter, could one? In the new example above, I think it is confusing that a write is used to change the properties of a running task. If a write to the property node, which seems more logical, the change does not take place. However, this is a good solution for me.

Works Well  - Oct 3, 2008

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.

painfully complicated?  - May 3, 2007

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?

ms updating of counterfrequency  - Nov 23, 2006

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.

  - May 8, 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?

  - Mar 27, 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..

does not print properly  - Mar 31, 2005

Unable to print this document properly, wither directly or through Acrobat writer. Right Hand side gets cut off.

width combersom in IE6.0  - Mar 31, 2005

I took the time to reformat in Word, in my browser I had to scroll left and right to read this article. MfG, Jo

View more reviews

Bookmark & Share





Rate this document

Answered Your Question?
Yes No