The implementation of the PWM generation and the pulse counting depends on the hardware that you are using. Counters are preferable for generating PWM output. Please refer to your hardware's datasheet to see how many counters are available on that device and whether it can support PWM.
- E, S, M and X series devices – Look at user manual
- Other Digital Modules – check if they are Static (i.e. Software-Timed) or Hardware-Timed in the user manual
- C Series - Check if your C Series modules can access the counters or timers on your CompactDAQ (cDAQ) chassis or controller's backplane.
- cDAQ – look at number of counters on chassis in the user manual. This feature is only available in the modules that provide access to the chassis onboard counters.
PWM with NI-DAQmx in LabVIEW
To implement PWM in NI-DAQmx, make sure you have investigated your hardware and the capabilities it has. After that, you can determine the best solution to your application: Software Times or Hardware Timed PWM.Software Timed Pulse Width Modulation
If your application does not have tight requirements, with respect to speed and precision, a digital output can be used with software timing control for pulse train or PWM generation.
Here is an example in the Community for implementing PWM with software timed digital I/O. Hardware Timed Pulse Width Modulation
- Starting with an Example (recommended)
- Launch LabVIEW
- Navigate to Help >> Find Examples... which will launch the NI Example Finder
- Confirm you are in the Browse tab and navigate through the folders Hardware Input and Output >> DAQmx >> Counter Output
- Double click the example you want to use
- DO NOT edit this example. Any changes will overwrite the original example.
- Click File >> Save As
- Choose the option that suits your needs. Recommendation is to choose Duplicate Hierarchy to new location
- Choose the location and click Save
- Close out for the example and open Example in the new location
- Once the local copy of the example is opened, you can change the configuration and the channel used in the example.
- Starting from a Blank Block Diagram
- Create and initialize a counter output channel using the DAQmx Create Virtual Channel VI. There are three ways to define your counter output task in NI-DAQmx based on your application needs:
- CO Pulse Freq
- CO Pulse Time
- CO Pulse Ticks - Note - Different hardware have different time-bases, running the same code with different hardware may change pulse widths.
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.
Note: 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.
- Place a While Loop to run continuously until the user presses the Stop button.
- Inside the While Loop:
- Create a Local Variable for the Duty Cycle. This will be compared to the original Duty Cycle variable and the comparison will act at the selector for the Case Structure.
- Place a Case Structure:
- True Case: Call the DAQmx Write VI. If the Duty Cycle has been changed, the new duty cycle is set using the DAQmx Write VI.
- False Case: Wire all the inputs straight to the outputs. If the duty cycle has not been changed, the false case of the case structure executes, and nothing is updated.
- Check for errors using the DAQmx Is Task Done VI.
- Add a Wait Until Next Millisecond Multiple VI to introduce a short 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.
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.