Digital I/O

Showing results for 
Search instead for 
Did you mean: 

calculate duty cycle using a USB-6009 Data acqusistion unit

Hello everyone,
I am trying to calculate the duty cycle of a digital signal using a USB-6009 Data acqusistion unit. While I am aware of the shortcommings of this unit for this type of application, if I calculate the duty cycle 100 times per second, that should be enough. The period of my square wave is about 10 milliseconds.
My first attempt (accelerometer_vi_2) I tried to use the "Pulse" block, however, I think I am using the wrong data type for this block. Is there a way to convert the data type so I can use this block?
My second attempt was to replicate some of the ideas discussed here:
Basically, for each period, you create an array where the duty cycle can be calculated by summing every 1 in a boolean array and dividing it by the length of the array. Instead of a boolean array, I used a binary array because I could not use the sum tool for a boolean array. Even with the binary array, however, the VI does not work!
I am sure the answers to some of this stuff is fairly simple, however, this is my first Labview VI, so I know I have a lot to learn! If there is a better way then the options I have discussed, to calculate the duty cycle, I am all ears!
Download All
0 Kudos
Message 1 of 6
(4,054 Views) is the VI i used to try and calculate the duty cycle using a binary array as described in my first post! Sorry to add more confusion!
0 Kudos
Message 2 of 6
Ok lets try this one more is the vi that uses a binary array for the duty cycle calculation...
0 Kudos
Message 3 of 6


I attached some code that will run a post process analysis on the data to calculate the duty cycle.  Please note that the constant for Wait Until Next ms Multiple is set to 1.  Thus, the loop will run every 1 msec. (On Demand timing), which equates to a sample rate of 1,000 Hz.  Since your period is 10 ms in duration, this should hopefully allow for enough samples during one period to compute an accurate duty cycle. 

This code is used for monitoring one line/channel.  I saw that the original code was setup to acquire across several lines in a port.  Do you need to monitor more than one channel in order to measure several channels duty cycle?  Please also note that I used DAQmx function calls instead of the DAQ Assistant.  Let me know if and where you need flexibility and I can make some suggestions.


National Instruments
Applications Engineer
0 Kudos
Message 4 of 6
Thanks for your help Samantha,
I got your code to work, however, I am not sure that 10 samples will provide the kind of precision I need to work with my particular application. Is there any way to set that wait function to less then one ms???
0 Kudos
Message 5 of 6


If you are looking for more accuracy, I would highly recommend getting a Multifuctional DAQ 62XX or Counter 66xx device with a counter so you can run a Counter Input Task for semi period.  There is actually a shipping example in the Example Finder titled Measured Duty Cycle - Buffered - that used this type of task with a counter to measure duty cycle.  The USB 6009 that you currently have only does event counting.  It is recommended to use a counter input task for duty cycle measurements.

You can set the constant input of Wait Until Next ms to a number lower that 1.  For example .5 or .2, however this type of software timing (vi posted above) is system dependent.  The loop it set to run every 1 ms if the processor allows it.  In addition if the DAQmx or code for building the array makes the loop take longer than 1 ms, than you are not actually achieving this rate.  Thus, the constant can be set to a value less than 1, however you may not get that timing.  The same is true for the 1 ms, since it is system dependent.


National Instruments
Applications Engineer
0 Kudos
Message 6 of 6