# Connexions

You are here: Home » Content » Lab 1: Introduction to LabVIEW

### Recently Viewed

This feature requires Javascript to be enabled.

# Lab 1: Introduction to LabVIEW

The objective of this lab is to offer an initial hands-on experience in building a VI. More detailed explanations of the LabVIEW features mentioned here can be found in the (Reference), (Reference), and (Reference). One can launch LabVIEW 2011 (the latest version at the time of this publication) by double-clicking on the LabVIEW 2011 icon, which opens the dialog window shown in Figure 1.

## Building a Simple VI

To become familiar with the LabVIEW programming environment, let us calculate the sum and average of two input values in the following step-by-step example.

### Sum and Average VI Example Using Graphical Programming

To create a new VI, click on the Blank VI under New, as shown in Figure 2. This can also be done by choosing File New VI from the menu. As a result, a blank front panel and a blank block diagram window appear, see Figure 2. Remember that a front panel and block diagram coexist when one builds a VI, meaning that every VI will have both a front panel and an associated block diagram.

The number of VI inputs and outputs is dependent on the VI function. In this example, two inputs and two outputs are needed, one output generating the sum and the other generating the average of two input values. Create the inputs by locating two numeric controls on the front panel. This can be done by right-clicking on an open area of the front panel to bring up the Controls palette, followed by choosing Controls Modern Numeric Numeric Control. Each numeric control automatically places a corresponding terminal icon on the block diagram. Double-clicking on a numeric control highlights its counterpart on the block diagram and vice versa.

Next, label the two inputs as x and y using the Labeling tool from the Tools Palette, which can be displayed by choosing View Tools Palette from the menu bar. Choose the Labeling tool and click on the default labels, Numeric and Numeric 2, to edit them. Alternatively, if the automatic tool selection mode is enabled by clicking Automatic Tool Selection in the Tools Palette, the labels can be edited by simply double-clicking on the default labels. Editing a label on the front panel changes its corresponding terminal icon label on the block diagram and vice versa.

Similarly, the outputs are created by locating two numeric indicators (Controls Modern Numeric Numeric Indicator) on the front panel. Each numeric indicator automatically places a corresponding terminal icon on the block diagram. Edit the labels of the indicators to read “Sum“ and “Average.”

For a better visual appearance, one can align, distribute and resize objects on a front panel window using the front panel toolbar. To do this, select the objects to be aligned or distributed and apply the appropriate option from the toolbar menu. Figure 3 shows the configuration of the front panel just created.

Now build a graphical code on the block diagram to perform the summation and averaging operations. Note that <Ctrl-E> toggles between a front panel and a block diagram window. If objects on a block diagram are too close to insert other functions or VIs in-between, one can insert a horizontal or vertical space by holding down the <Ctrl> key to create space horizontally and/or vertically. As an example, Figure 4b illustrates a horizontal space inserted between the objects shown in Figure 3a.

Next, place an Add function (Functions Express Arith & Compar Express Numeric Add) and a Divide function (Functions Express Arith & Comp Express Numeric Divide) on the block diagram. Enter the divisor, in this case 2, in a Numeric Constant(Functions Express Arith & Compar Express Numeric Numeric Constant) and connect it to the y terminal of the Divide function using the Wiring tool.

To achieve proper data flow, wire functions, structures and terminal icons on a block diagram using the Wiring tool. To wire these objects, point the Wiring tool at the terminal of the function or subVI to be wired, left-click on the terminal, drag the mouse to a destination terminal and left-click once again. Figure 5 illustrates the wires placed between the terminals of the numeric controls and the input terminals of the Add function. Notice that the label of a terminal gets displayed whenever one moves the cursor over the terminal if the automatic tool selection mode is enabled. Also, note that the Run button on the toolbar remains broken until one completes the wiring process.

For better block diagram readability, one can clean up wires hidden behind objects or crossed over other wires by right-clicking on them and choosing Clean Up Wire from the shortcut menu. Any broken wires can be cleared by pressing <Ctrl-B> or Edit Remove Broken Wires.

To view or hide the label of a block diagram object, such as a function, right-click on the object and check (or uncheck) Visible Items Label from the shortcut menu. Also, one can show a terminal icon corresponding to a numeric control or indicator as a data type terminal icon by right-clicking on the terminal icon and unchecking View As Icon from the shortcut menu. Figure 6 shows an example where the numeric controls and indicators are depicted as data type terminal icons. The notation DBL indicates double precision data type.

It is worth noting that there is a shortcut to build the above VI. Instead of choosing the numeric controls, indicators or constants from the Controls or Functions palette, one can use the shortcut menu Create, activated by right-clicking on a terminal of a block diagram object such as a function or a subVI. As an example of this approach, create a blank VI and locate an Add function. Right-click on its x terminal and choose Create Control from the shortcut menu to create and wire a numeric control or input. This locates a numeric control on the front panel as well as a corresponding terminal icon on the block diagram. The label is automatically set to x. Create a second numeric control by right-clicking on the y terminal of the Add function. Next, right-click on the output terminal of the Add function and choose Create Indicator from the shortcut menu. A data type terminal icon, labeled as x+y, is created on the block diagram as well as a corresponding numeric indicator on the front panel.

Next, right-click on the y terminal of the Divide function to choose Create Constant from the shortcut menu. This creates a numeric constant as the divisor and wires its y terminal. Type the value 2 in the numeric constant. Right-click on the output terminal of the Divide function, labeled as x/y, and choose Create Indicator from the shortcut menu. If the wrong option is chosen, the terminal does not get wired. An incorrect terminal option can easily be changed by right-clicking on the terminal and choosing Change to Control from the shortcut menu.

To save the created VI for later use, choose File Save from the menu or press <Ctrl-S> to bring up a dialog window to enter a name. Type “Sum and Average” as the VI name and click Save.

To test the functionality of the VI, enter some sample values in the numeric controls on the front panel and run the VI by choosing Operate Run, by pressing <Ctrl-R> or by clicking the Run button on the toolbar. From the displayed output values in the numeric indicators, the functionality of the VI can be verified. Figure 7 illustrates the outcome after running the VI with two inputs, 10 and 15.

## SubVI Creation

If it is desired to use a VI as part of a higher-level VI, one needs to configure its connector pane. A connector pane assigns inputs and outputs of a subVI to its terminals through which data are exchanged.

The default pattern of a connector pane is determined based on the number of controls and indicators. In general, the terminals on the left side of a connector pane pattern are used for inputs and the ones on the right side for outputs. One can add terminals to or remove them from a connector pane by right-clicking and choosing Add Terminal or Remove Terminal from the shortcut menu. If the number of inputs/outputs or the distribution of terminals are changed, the connector pane pattern can be replaced with a new one by right-clicking and choosing Patterns from the shortcut menu. Once a pattern is selected, one needs to reassign each terminal to a control or an indicator by using the Wiring tool or by enabling the automatic tool selection mode.

Figure 8a illustrates how to assign a Sum and Average VI terminal to a numeric control. The completed connector pane is shown in Figure 8b. Notice that the output terminals have thicker borders. The color of a terminal reflects its data type.

Considering that a subVI icon is displayed on the block diagram of a higher-level VI, it is important to edit the subVI icon for it to be explicitly identifiable. Double-clicking on the top-right corner icon of a block diagram opens the Icon Editor. The Icon Editor tools are similar to those in other graphical editors, such as Microsoft Paint. Editing the Sum and Average VI icon is illustrated in Figure 9.

A subVI can also be created from a section of a VI. To do so, select the nodes on the block diagram to be included in the subVI, as shown in Figure 10a. Then, choose Edit Create SubVI to insert a new subVI icon. Figure 10b illustrates the block diagram with an inserted subVI. One can open and edit this subVI by double-clicking on its icon on the block diagram. Save this subVI as Sum and Average.vi. This subVI performs the same function as the original Sum and Average VI.

## Using Structures and SubVIs

Now let us consider another example to understand the use of structures and subVIs. In this example, we use a VI to show the sum and average of two input values, which are altered in a continuous fashion. If the average of the two inputs becomes greater than a preset threshold value, a LED warning light turns on.

First, build a front panel as shown in Figure 11a. For the inputs, consider two Knobs(Controls Modern Numeric Knob). Adjust the size of the knobs by using the Positioning tool. One can modify knob properties such as precision and data type by right-clicking and choosing Properties from the shortcut menu. A Knob Properties dialog box opens and an Appearance tab is shown by default. Edit the label of one of the knobs to read Input 1. Select the Data Type tab, click Representation and select Byte to change the data type from double precision to byte. One can also perform this by right-clicking on the knob and choosing Representation Byte from the shortcut menu. In the Data Type tab, a default value needs to be specified. In this example, the default value is considered to be 0. The default value can be set by right-clicking on the control and choosing Data Operations Make Current Value Default from the shortcut menu. Also, this control can be set to a default value by right-clicking and choosing Data Operations Reinitialize to Default Value from the shortcut menu.

Label the second knob as Input 2 and repeat all the adjustments as carried out for the first knob except for the data representation part. Specify the data type of the second knob to be double precision to demonstrate the difference in the outcome. As the final front panel configuration step, align and distribute the objects using the appropriate buttons on the front panel toolbar.

To set the outputs, locate and place a numeric indicator, a round LED (Controls Modern Boolean Round LED) and a gauge (Controls Modern Numeric Gauge). Edit the labels of the indicators as shown in Figure 11.

Locate a Greater or Equal? function from Functions Programming Comparison Greater or Equal? to compare the average output of the subVI with a threshold value. Create a wire branch on the wire between the Average terminal of the subVI and its indicator via the Wiring tool. Then, extend this wire to the x terminal of the Greater or Equal? function. Right-click on the y terminal of the Greater or Equal? function and choose Create Constant to place a numeric constant. Enter 9 in the numeric constant and wire the round LED, labeled as Warning, to the x>=y? terminal of this function to provide a Boolean value.

To run the VI continuously, use a while loop structure. Choose Functions Programming Structures While Loopto create a while loop. Change the size by dragging the mouse to enclose the objects in the while loop, as illustrated in Figure 12.

Once this structure is created, its boundary, together with the loop iteration terminal and conditional terminal , get shown on the block diagram. If one creates the while loop by using Functions Programming Structures While Loop, the Stop button is not included as part of the structure. One can create this button by right-clicking on the conditional terminal and choosing Create Control from the shortcut menu. It is possible to wire a Boolean condition to a conditional terminal, instead of a Stop button, to stop the loop programmatically.

Next run the VI to verify its functionality. After clicking the Run button on the toolbar, adjust the knobs to alter the inputs. Verify whether the average and sum are displayed correctly in the gauge and numeric indicators. Note that only integer values can be entered via the Input 1 knob while real values can be entered via the Input 2 knob. This is due to the data types associated with these knobs. The Input 1 knob is set to byte type, in other words, I8 or 8-bit signed integer. As a result, one can enter only integer values within the range -128 and 127. Considering that the minimum and maximum values of this knob are set to 0 and 10, respectively, one can enter only integer values from 0 to 10 for this input.

## Debugging VIs: Probe Tool

Use the Probe tool to observe data that are being passed while a VI is running. A probe can be placed on a wire by using the Probe tool or by right-clicking on a wire and choosing Probe from the shortcut menu. Probes can also be placed while a VI is running.

Placing probes on wires creates probe windows through which one can observe intermediate values. As an example of using custom probes, use four probe windows at the probe locations 1 through 4 in the Sum and Average VI to probe the values at those locations. These probes and their locations are illustrated in Figure 14.

## Profile Tool

With the Profile tool, one can gather timing and memory usage information. Make sure to stop the VI before selecting Tools Profile Performance and Memory to open a Profile window.

Place a checkmark in the Timing Statistics checkbox to display timing statistics of the VI. The Timing Details option offers more detailed VI statistics such as drawing time. To profile memory usage as well as timing, check the Memory Usage checkbox after checking the Profile Memory Usage checkbox. Note that this option can slow down VI execution. Start profiling by clicking the Start button on the profiler, then run the VI. Obtain a snapshot of the profiler information by clicking on the Snapshot button. After viewing the timing information, click the Stop button. The profile statistics can be stored in a text file by clicking the Save button.

An outcome of the profiler is shown in Figure 15 after running the Sum and Average or L1.1 VI. (Reference) provides more details on the Profile tool.

## Lab Exercises

### Exercise 1

Build a VI to compute the variance of an array xx size 12{x} {}. The variance σσ size 12{σ} {} is defined as:

σ=1Nj=1N(xjμ)2σ=1Nj=1N(xjμ)2 size 12{σ= { {1} over {N} } Sum cSub { size 8{j=1} } cSup { size 8{N} } { $$x rSub { size 8{j} } - μ$$ rSup { size 8{2} } } } {}
(1)

where μμ size 12{μ} {}denotes the average of the array x. For x, use all the integers from 1 to 1000.

#### Solution

Insert Solution Text Here

### Exercise 2

Build a VI to check whether a given positive integer nn size 12{n} {} is a prime number and display a warning message if it is not a prime number.

#### Solution

Insert Solution Text Here

### Exercise 3

Build a VI to generate the first NN size 12{N} {}prime numbers and store them using an indexing array. Display the outcome.

#### Solution

Insert Solution Text Here

### Exercise 4

Build a VI to sort NN size 12{N} {} integer numbers (positive or negative) in ascending or descending order.

#### Solution

Insert Solution Text Here

## Content actions

### Give feedback:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks