From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Counter Output PWM with Dynamic Duty Cycle Updates in DAQmx

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • Data Acquisition (DAQ)

    Software

  • LabVIEW

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview

This VI allows the user to dynamically update the duty cycle in order to change the position of the servo on the fly.

Description

This VI was created and tested on an NI M Series DAQ card for the purpose of controlling a servo motor that operates at a frequency of 50 Hz and varies its position relative to the duty cycle (or pulse width) of the signal.

Requirements

Software

  • NI LabVIEW 8.5 or compatible
  • NI DAQmx 8.6.1 or compatible

Hardware

  • Most DAQ cards with Counter I/O

Steps to Implement or Execute Code

Front Panel.jpg

1. Select the desired counter output physical channel.

2. Set your initial frequency and duty cycle.

3. Run the VI.

4. Change the duty cycle and press the 'Update Duty Cycle' button to trigger update.

Block Diagram Steps

Block Diagram.jpg

1. The DAQmx Create Channel.vi to creates and configures the counter output frequency task to generate a pulse train of a user specified frequency and duty cycle. 
2. The DAQmx Timing.vi to sets the mode to continuous and the timing to be implicit (i.e. does not require sample timing since it is a counter task for pulse train generation).
3. Start the task.
4. Enter the True case to update the duty cycle upon user selection.  The False case simply passes through the task and error wires.
5. Stop the task.
6. Dynamically reconfigure the duty cycle of the pulse train.
7. Restart the task.
8. Exit the while loop if an error occurs or upon user selection, and then clear the task and handle errors.

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
sahpe
Active Participant
Active Participant
on

Thank you for this example VTHokie, however I believe there is a slightly better solution, not sure if it works with all DAQmx devices though:

http://zone.ni.com/devzone/cda/tut/p/id/2991

univer_solar
Member
Member
on

I used USB 6251 NI and I can control the PWM according to the example. I want to change the peak to peak voltage of the input pulse. Would you please tell me how to do that?

Thanks

A.E.M
Active Participant
Active Participant
on

I agree with sahpe. I don't see the reason why we should stop the task in order to update the duty cycle, as this will add an 'empty space' in the signal, where no PWM signal is getting outputted.

Best Regards

Alex E. Munkhaus
Certified LabVIEW Developer (CLD)
System Engineer
mysticfree
Member
Member
on

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.

Contributors