From 8:00 PM CST Friday, Feb 24 - 6:00 PM CST Saturday, Feb 25, will be undergoing system upgrades that may result in temporary service interruption. We appreciate your patience as we improve our online experience.

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

Publish Date: Apr 25, 2016 | 82 Ratings | 3.26 out of 5 | Print | 11 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
11 Reviews | Submit your review

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

View more reviews

Bookmark & Share




Rate this document

Answered Your Question?
Yes No