Example Code

Combining thermocouple input with DAQmx output of a variable PWM with different I/O rates

Code and Documents

Attachment

Overview

Often setting up a DAQmx task can be troublesome, especially when you're looking to use an input to modify an output at the same time but at different rates. This example looks at acquiring a temperature reading once a second and modifying the duty cycle of a Pulse Width Modulated digital output. The use case was to monitor temperature and control the speed of a fan based on that.

Description

This VI uses a combination of DAQ assistant and Daqmx tasks. There are two loops which perform the temperature reading and Pulse width modulation separately as they require different rates.

The top loop acquires a temperature reading through the DAQ assistant and this was done for speed to allow it to be modified easily to any other type of reading. This passes data to a case structure, which at the moment only selects two different duty cycles based on a push button on the front panel. This whole section of code can be replaced with a custom algorithm depending on the application, so it's been left open to modification. The duty cycle is then updated on the front panel and the wait (ms) function ensures the loop tasks at least a second. To stop this loop, there's a local variable that reads the stop button's value on the front panel. The Stop is in the bottom loop so it's more responsive.

The bottom loop has the DAQmx API functions laid out to set up the output of one periods worth of PWM (for example it would output for 1second if the frequency was 1Hz and it would split that second up into high time and low time dependant on the duty cycle). It will allocate the high and low times based on the duty cycle ratio that's input to the DAQmx write function. Outside the loop, from left to right, the counter task is set up for pulse output, the timing is set to continuously sample but with implicit timing based on the frequency value used for the task.

On entering the loop the task is started and a very small value for the duty cycle is fed into a shift register. This while loop will cycle round at the rate set by the task. In the top left, a local variable reads the duty cycle value from the top loop and compares this to the value in the shift register. If they are the same, it then runs the DAQmx write VI to provide one pulse of PWM. If they are different then it passes that data into the shift register to be used next cycle of the loop.

The DAQmx is Task Done VI controls the speed of this loop by waiting until the PWM signal has been implemented on the output channel.

One issue was how to pass data from one loop to the other and update the duty cycle while both loops were running. A local variable has been used to read the duty cycle value in the top loop.

Requirements

Software

LabVIEW 2013 or higher

NI-Daqmx 9.7.5 or above

Hardware

cDAQ 9174 with 9211 and 9474

With slight modification, any DAQmx device can be used that has counter functionality and digital outputs

Steps to implement Code

Open in LabVIEW 2013

Configure DAQ assistant task to your hardware

Configure the counter input at the bottom left to your counter of choice

Run VI

PWMcode.jpg

James P

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

Contributors