# TechTip:

## Getting Started with ULx for NI LabVIEW™ Part 3

### Using ULx for NI LabVIEW – Learn How to Scale Data and Add Alarming and Digital Control

In Part 2, we enhanced the VI we created in Part 1 by adding the ability to acquire data at a steady rate, display the data in a stripchart, save the data to a log file, and notify the operator if is there is an error. In this third and final part, we will enhance what we have done so far, by adding:

• Scaling
• Alarming
• Digital Control

Here is where we left of in Part 2; our Block Diagram and Front Panel looked like this:

Scaling
Most sensors measure real world data like pressure, vacuum, temperature, force, strain, velocity, angle, speed, etc. However what they send to your data acquisition device such as the USB-1608G can be very different. They measure in units such as PSI, Torr, °F/°C, ft/lbs or RPM, but they output a voltage, current or resistance for example. For this article, we will assume a sensor is outputting a voltage.

Let’s say you have a pressure sensor. It can measure air pressure anywhere from 0 PSI to 1000 PSI (Pounds per Square Inch). But the sensor outputs this pressure as a proportional 0 V to 5 V to the USB-1608G. To convert from volts to pressure, use the equation y = mX + B where X is the multiplier and B is the offset. That being the case, B = 0 because 0 V = 0 PSI, and X = 200 because 1000 PSI / 5 V = 200 PSI/V

Though there are several ways to implement a multiplier (or scale) and offset in NI LabVIEW, we are going to stay with our 'Keep It Simple' mode by just adding two constants and math operator VIs.

As you can see from where we left off, there is not much room to work in the While Loop, which is where we will be making all of our enhancements, so we’ll be needing to make it larger. Going forward, we’ll only be showing you the contents of the While Loop in this article unless more is needed.

Let’s start by making our While Loop wider. Hover your cursor over the While Loop’s right drag handle (small blue square), and stretch the loop to the right, doubling the width of the loop. Next we need to move the Write to Measurement file.VI, Waveform Chart.VI and Data.VI over to the right as well. Sure, you can drag them one by one but then you have to clean up your wiring. Instead, place the cursor to the right and just above the Write to Measurement File.VI, hold down the left mouse button, drag a rectangle around the 3 VIs and their wiring and release the left mouse button. You’ll see you have now grabbed all 3 VIs and their wiring. Now hover your mouse over the Write to Measurement file.VI, hold down the left mouse button and drag the group of VIs and wires to the right. It should look like this:

Since everything we’re to do is in this loop, then that is the portion shown.

Next, we need to remove the orange data wire coming out of the Read.VI. All you need do is click on the wire to give it focus, then press on your keyboard’s ‘Delete’ key to remove it. You’ll see the 3 VIs now have wiring errors. Also notice the run arrow is broken. You can click on it to find out why the VI doesn’t work. It states, “The wire connects 1 or more data sinks but has no source,” which simply means, you are missing a wire. But that’s OK! We are going to fix that, but we need to add a scaling and offset by way of Multiply and Add VIs.

By now you should be able to add VIs to a block diagram, but in case you’ve forgotten; right click on the white area of the Block Diagram, select Programming >> Numeric. Left mouse click on a Multiply VI and drag it to your Block Diagram. Place it to the right of the data pin of the Read.VI. Repeat this same process for the Add.VI and place it to the right and a little higher than the Multiply.VI.

Perform the following:
Wire :
Read.VI data pin to Multiply.VI y pin;
Multiply.VI x*y pin to Add.VI y pin;
Add.VI x+y pin to Data.VI input pin.

Right click on Multiply.VI x pin, select Create >> Constant. Set it to 200.
Right click on Add.VI x pin, Select Create >> Constant. Set it to 0.

Your diagram should look like this:

Here is our Front Panel, running with the new changes:

Note that it looks about the same as it did before, but the data is 200 times what it was originally and so are the Y axis labels on the chart.

Alarming
Our pressure sensor has the ability to measure 0 to 1000 PSI. But what if the pressure vessel could only hold an air pressure of 900 PSI? What if once the 900 PSI was exceeded the vessel could rupture and cause damage. Let’s assume someone is monitoring this front panel and some others. We wanted to make their life easier by letting them know when the pressure gets to some value, not quite 900 PSI, more like 750 PSI so there is time to react to the situation. With little effort we can add some additional VIs to let the operator know when the pressure is safe as well as unsafe.

We’ll compare the current value to 750 PSI, if the pressure is less than 750 we’ll display an unlit indicator, if the pressure exceeds 750 the VI will light the indicator. To do this, we need a Greater Than.VI which is located on the Comparison Palette. Place it to the right of the Add.VI. It is important to wire the output of the Add.VI to the x pin of the Greater Than.VI else it will operate in reverse. Create a constant for the Y pin of the Greater Than.VI, and set it to 750. Create an Indicator (right click, select Create>> Indicator) and wire it to the output of the GreaterThan.VI. Change the caption of the Indicator by going to the Front Panel, double click on the indicator’s caption and enter something appropriate such as “Over 750 PSI.” I also made it a bit larger.

Your block diagram and front panel should like this:

To simulate the pressure sensor, use a variable power supply, one that can output from 0 to 5VDC. If you have not already done so, attach the output of the power supply to Channel 0 (CH0H) of the USB-1608G. If you the USB-1608G is configured for single ended inputs, connect the power supply’s ground to an AGND terminal of the USB-1608G. If it is set up for differential ended inputs, then wire it as above and install a jumper wire from CH0L to AGND. Set the output of the power supply to 0VDC, and run your VI. The Indicator will turn on when the output of the power supply reaches and exceeds 3.75 V (750 PSI ÷ 200 PSI/V = 3.75 V), and off when below 3.75 V.

Digital Control
Our last task is to include the use of the USB-1608G’s digital outputs. Assuming our ‘pressure system’ is basically an air compressor and storage tank, maintaining a maximum pressure in the tank is something we can easily automate.

Each Digital Bit of the USB-1608 can be individually configured for input or output. When set for output, the digital bit will be set to 0V when off, and nominally 5V when on. We can use the comparison VI used previously as the decision maker for when to turn on or off the bit.

Just like when we start this VI in Part 1, we add to add a Create Channel.VI, this time, we will want to change it from AI Voltage to Digital Output before we right click on the Physical Channels pin to select the digital bit of the USB-1608G. Additionally, when we add the VI for physical channels, create it as a constant not a control so it will not be visible on the Front Panel. When added, drop down on the arrow, and select “Dev0/AuxPort/Do0”. Next add a ULx Write.VI, place it above the indicator, adjusting the loop size as necessary, and wire it as shown below.

When it comes to error handling, we want to leverage our existing wiring as best we can. First, move the Unbundle.VI, Stop button, Or Gate and Conditional Terminal to the right. Replace the Or.VI with a Compound Arithmetic.VI. This VI is located on the Boolean Palette. It is a polymorphic VI. When you hover your mouse over it, you will see 2 drag handles (bottom and top). Drag the lower one down to add one additional input. Next add an Unbundle.VI to the ULx Write.VI’s error out pin, and wire it to the remaining input of the Compound Arithmetic.VI as shown below.

Lastly, to complete the error handling, while we could add an additional Simple Error.VI, we can make our block diagram more efficient if we keep only the original one with its settings. Add a Merge Errors.VI to the block diagram. Depending on the version of NI LabVIEW, it will either appear as shown below if using Version 2009, or in the newer versions it looks like this . Wire it as shown:

To detect the operation of the digital bit (DIO0), connect it to a digital volt meter, and use the terminal GND for your meter’s ground. Set the output of the power supply to 0 VDC, and run your VI. As before, the Indicator will turn on when the output of the power supply reaches and exceeds 3.75 V and DIO0 will output about 5 VDC, below 3.75 V, the Indicator will turn off and DIO0 will drop to 0VDC.

A view of the completed and running Front Panel:

As we have seen in all three parts of this article, it is easy to use NI LabVIEW with Measurement Computing hardware to measure analog signals, manipulate the data and control digital IO. This is just a simple example of what you can do with tools such as these. The list is endless.