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
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.
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
- 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.
Figure 3: Complete PWM application
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