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

Hardware-Timed Single Point for Duty Cycle of PWM

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

Description:
This example configures a DAQmx Counter Task for Hardware-Timed Single Point PWM measurements on an X Series DAQ card. It imitates HWTSP Pulse Measurement, except it will not throw an error if there is no pulse (0% duty cycle or 100% duty cycle).

Steps to implement or execute code

  1. Rename your X Series DAQ Card to 'Dev1' in Measurement and Automation Explorer.
  2. Attach your PWM Signal to specified port on Front Panel (PFI9).
  3. Run the VI.View the Duty Cycle Measurement

I/O Connections Overview:
Connect a PWM signal to the terminal specified in PWM Signal Indicator on the Front Panel. This defaults to PFI9.

Requirements
Software
LabVIEW 2012 or compatible
NI-DAQmx 16.0 or compatible

Hardware
cDAQ with C series Counter Input Module and Analog Output
 
2017-10-30_15-40-23.jpgbd.png

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

- Regards,

Beutlich

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

Comments
cf57370
Member
Member
on

Hello,

I had like to know how integrate this VI in a 10 ms cadenced loop.

Cyril

Beutlich
Member
Member
on

What device and form factor are you trying to use?

- Regards,

Beutlich
cf57370
Member
Member
on

I am trying to use a X serie 6343 USB device. What do you mean about form factor?

Beutlich
Member
Member
on

The loop rate is determined by the Analog Output sample clock. The first example had a constant at 5 Hz, which corresponds to a 200 ms periods. I tested with a USB 6343, and I could run this easily at 10 ms by increasing the Analog Sample Clock rate to 100 Hz. I updated the example to call this out as a front panel control, so try looking at the updated version.

USB is the form factor. Example alternatives would be a 6343 for the PC (PCI) or for a test chassis (PXI). USB will be the worst for latency and throughput, but it seems this example runs at the rates you are looking for.

- Regards,

Beutlich
cf57370
Member
Member
on

Thank you, but I fear that I can't go under 15 ms with this solution. Moreover,when I integrate your VI in my whole programm,the execution time is increased to 30 ms. Is there another way to get the duty cycle? At the moment I use a lowpass filter and an analog input to manage the PWM signal but I think that the accuracy is low.

Beutlich
Member
Member
on

USB latency can be poor, which is why HWTSP features are not offered on USB X Series devices. However, when I tested this code with a USB 6343, I could run this loop rate down to 1 ms. I did not stress it past that, I can't comment on how much time your extra code adds.

This code runs expecting the frequency to be much faster than the acquisition, so that there are a plethora of samples that we ignore. You may instead have a slow PWM, so that the PWM has not pulsed often enough to fill the buffer for a new measurement. If this is the case, you may try changing the Read VI to pinch every value. Currently, the Read VI pulls an array (the latest two elements), and computes the PWM from that. Instead, you may just read one value, and store the latest value in a shift register to use as a comparison. It is very critical that you do not loose samples in this setup (that you read much faster than your PWM).

- Regards,

Beutlich
hfibonaich
Member
Member
on

can I use this program with DAQ6624 and 6723