You are on page 1of 12

Introduction to LabVIEW Programming I

Laboratory #1
Sub VI, Case Structure, Formula nodes, and Sequence Structure
ME 337L Engineering Measurement Laboratory
Dept of Mechanical Engineering
University of Nevada, Las Vegas

Summary
In this tutorial, you will become familiar with the basic operations in LabVIEW. Also you will be able to
create VI’s (Virtual Instrument) and sub-VI’s, as well as learning how to use case structures, formula nodes,
and sequence structures.

Equipment List
 Computer with LabVIEW

Exercise 1 Particle Under Free Fall


Objective: To build a VI and create a subVI
In the following exercise, you will be able to create a program that will give you the displacement of an
object under free fall. You will be introduced to the concept of editing mathematical operations, creating
inputs and outputs, and wiring the components of your code.

The Problem Analysis:


Consider a particle at a reference level considered as y0=0. The particle will fall in the negative y-direction
due to the gravitational force. The displacement covered by this particle is given by the well known
equation:
1
y   gt 2
2

Where g is the gravitational acceleration constant (9.81 m/s2) and t is the time at which we need to find the
displacement.

Procedure:
1. Launch LabVIEW application (use the shortcut in the desktop or use the start menu to locate
the LabVIEW: Start>>All Programs>>National Instruments>>LabVIEW
2009>>LabVIEW
2. From the Getting Started window, select a new/blank VI. (File>>New VI)
3. In the front panel, create a numeric digital control. You will use this control as an input to
enter the time.
a. Right-click an open area on the front panel to display it. Select the numeric control on
the Controls>>Modern>>Numeric palette.
b. Move the control to the front panel and click to place the control.
c. Type “Time” inside the label and click outside the label or click the Enter button on the
toolbar, shown at left. If you do not type the name immediately, LabVIEW uses a default
label. You can edit a label at any time by using the Labeling tool.
4. Create a numeric digital indicator. You will use this indicator as an output to display the
displacement.
a. Select the digital indicator on the Controls>> Modern>>Numeric Indicators palette.
b. Move the indicator to the front panel and click to place the indicator.
c. Type “Displacement” inside the label and click outside the label or click the Enter
button.
Note: Always notice the mouse pointer shape that change to the proper tool according to its
location.

Block Diagram

OR

Figure 1 Diagram of the Free Fall VI

LabVIEW creates corresponding control and indicator terminal icons on the block diagram
when you place controls and indicators on the front panel. The terminals represent the data
type of the control or indicator. You should see two double-precision, floating-point terminals
on the block diagram, one indicator, and one control.
5. Select the Multiply and Square functions located on the Functions>>
Programming>>Numeric palette and place them on the block diagram. If the Functions
palette is not visible, right-click an open area on the block diagram to display it.
6. Select the Numeric Constant, located on the Functions>> Programming>>Numeric
palette, and place two of them on the block diagram. When you first place the numeric
constant, it is highlighted so you can type a value.
7. Type -9.81 in one constant and 0.5 in the other. If you moved the constants before you typed
a value, use the Labeling tool to enter the values.
8. To wire from one terminal to another, use the Wiring tool to click the first terminal, move
the tool to the second terminal, and click the second terminal, as shown in the following
illustration. You can start wiring at either terminal.
 You can bend a wire by clicking to tack the wire down and moving the cursor in a
perpendicular direction. Press the spacebar to toggle the wire direction.
 When you move the Wiring tool over a terminal, the terminal area blinks, indicating that
clicking will connect the wire to that terminal and a tip strip appears, displaying the name
of the terminal.
 To cancel a wire you started, press the <Esc> key, right-click, or click the source
terminal.
9. Display the front panel by clicking it or by selecting Window>>Show Panel.
10. Save the VI, because you will use this VI later in the course.
a. Select File>>Save.
b. Navigate to …\\Desktop\ME337L_S13\SEC<section number>\ where <section
number> is the lab section you are in (i.e. “001”).
c. Type “<lastname1_lastname2>_Free Fall” in the File name field.
d. Click the Save button.

Run the VI
11. Enter a number in the digital control and run the VI.
a. Use the Operating tool, shown at left, or the Labeling tool to double-click the digital
control and type a new number.

b. Click the Run button, shown at left, to run the VI.


c. Try several different numbers and run the VI again.
12. Take screenshots for both the Front Panel and the Block Diagram to include them in
your Lab Report. Make sure that the front panel is showing working values for input
(control) and output (indicator).
13. In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.

Create a subVI
14. Right-click the icon to the left of the icon just created on the front panel and select Pattern
from the shortcut menu to define the connector pane terminal pattern. LabVIEW selects a
connector pane pattern based on the number of controls and indicators on the front panel. For
example, this front panel has two terminals, Time (input) and Displacement (output), so
LabVIEW selects a connector pane pattern with two terminals. You can choose the pattern
you desire by right clicking the icon and choosing patterns.
15. Assign the terminals to the digital control and digital indicator.
a. Click the left terminal in the connector pane. The tool automatically changes to the Wiring
tool, and the terminal turns black.
b. Click the Time control. The left terminal turns orange and a marquee highlights the control.
Click an open area of the front panel. The marquee disappears and the terminal changes to
the data type color of the control to indicate that you connected the terminal.
c. Repeat part a and b for the right terminal. Both terminals should be orange.
16. Select File>>Save to save the VI, because you will use this VI later in the Lab.
17. Select File>>Close to close the Free Fall VI.
________________________________________________________________________
End of Exercise 1
Loops
Exercise 2 Displacement Monitor
Objective: To use a While Loop and a waveform chart to acquire and display data.

Complete the following steps to build a VI that measures the displacement of a free fall and displays it on
the waveform chart.

Front Panel
1. Open a new VI.
2. Build the following front panel.

Figure 2 Front Panel of the Displacement Monitor VI

a. Select the vertical toggle switch on the Controls>> Modern>> Boolean>> Vertical
Toggle Switch palette, and place it on the front panel. You will use the switch to stop
the acquisition. Type Power inside the label and click outside the label or click the
Enter button on the toolbar, shown at left.
b. Select a waveform chart on the Controls>>Modern>>Graph palette and place it
on the front panel. The waveform chart will display the simulated displacement. Type
displacement Histogram inside the label and click outside the label or click the Enter
button.
c. The waveform chart legend labels the plot "Plot0". Double-click "Plot0" in the chart
legend, type Displacement, and click outside the label or click the Enter button to re-
label the legend. Double-click the upper bound (10) in the y-axis. Type 0, and click
outside the label or click the Enter button to rescale the chart. Change the lower
bound in the y-axis to -1000 (lower limit depends on the simulated time).
d. Label the y-axis Displacement, m and the x-axis Time, s.
Block Diagram

Figure 3 Block Diagram of the Displacement Monitor VI

3. Enclose the two terminals in a While Loop, as shown in the block diagram.
a. Select the While Loop on the Functions>>Programming>>Structures palette.
b. Left click and drag a selection rectangle around the two terminals.
4. Select Functions>>Select a VI, navigate to …\\Desktop\ME337L_S13\SEC <section
number>\ double-click the Free Fall VI. This will be our data source.
5. Wire the loop’s exit condition.
a. Left click on the Stop If True exit terminal in the While Loop. The terminal should
change appearance to the icon shown to the left, which represents Continue If True.
b. Wire the Boolean vertical toggle switch (labeled “Power”) to the exit condition.
6. Connect the data source to the waveform chart.

The While Loop executes as quickly as possible. Complete the following steps to take data at
certain intervals, such as once every half-second.

7. Do the following steps to control the timing of the readings:


a. Place the Wait Until Next ms Multiple function located on the
Functions>>Programming>>Timing palette. This function ensures that each
iteration occurs every half-second (1000 ms).
b. Right-click the millisecond multiple input of the Wait Until Next ms Multiple
function, select Create>>Constant from the shortcut menu, type 1000, and press the
<Enter> key. The numeric constant specifies a wait of 1000 ms so the loop executes
once every one second.
c. On the front panel, right-click the chart and select X Scale>>Formatting from the
shortcut menu. Click on the Scales tab. From the axis drop down, make sure that
Time [sec] (X-Axis) is selected. Change the Multiplier value to 1 because you added
a 1000 ms wait between loop iterations.
d. Format and customize the X and Y scales of the waveform chart.
i. Right-click the chart and select Y Scale>>Formatting from the shortcut
menu. The following dialog box appears.
ii. Click on the Scales tab. In the Scale Styles and Colors box, you can change
the scale style by clicking on the style icon and choosing a different style.
You can also select different mapping modes, grid options, scaling factors,
colors, and formats and precisions.
8. Save the VI as “<lastname1_lastname2>_Free Fall Monitor.vi” in your section’s folder.

Run and stop the VI


9. Display the front panel by clicking it or by selecting Window>>Show Front Panel.
10. Use the Operating tool to click the vertical toggle switch and turn it to the ON position.
11. Run the VI.
12. Take screenshots for both the front panel and the block diagram to include them in
your lab report. Make sure that the front panel is showing a displacement graph in the
output (Histogram).
13. In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.
14. Click the vertical toggle switch to stop the acquisition. The condition is FALSE and the loop
stops executing.
a. The section of the block diagram within the While Loop border, or sub-diagram,
executes until the specified condition is FALSE. For example, while the switch is on
(TRUE), the Free Fall VI takes and returns a new measurement and displays it on the
waveform chart. When the switch is turned to the off position (FALSE), the loop
will exit.

Format the Chart


15. Click on the Display format tab within the chart properties. Select the options shown in the
following dialog box (floating point format with 2 digit of precision with trailing zeros
hidden) and click the OK button.
16. Right-click the waveform chart and select Data Operations>>Clear Chart from the shortcut
menu to clear the display buffer and reset the waveform chart. If the VI is running, you can
select Clear Chart from the shortcut menu.

Figure 4 Chart Properties dialog box

17. Modify the vertical toggle switch so displacement is plotted on the graph each time you run
the VI.
a. Stop the VI if it is running.
b. Use the Operating tool to click the vertical toggle switch and turn it to the ON
position.
c. Right-click the switch and select Data Operations>>Make Current Value Default
from the shortcut menu. This sets the ON position as the default value.
d. Right-click the switch and select Mechanical Action>>Latch When Pressed from
the shortcut menu.
18. Run the VI.

End of Exercise 2

Exercise 3 Square Root VI


Objective: To use the Case Structure
Complete the following steps to build a VI that checks whether a number is positive. If it is, the
VI calculates the square root of the number. Otherwise, the VI returns an error message.
Front Panel
1. Open a new VI and build the following front panel.

Figure 5 Front Panel of Square Root VI


Block Diagram
2. Build the block diagram to match Figure 6 and Figure 7.
a. Place a Case Structure located on the Functions>> Programming >>
Structures palette.
b. Click the decrement and increment arrow buttons on the case selector to select
the FALSE case.
c. Place the "Greater or Equal to 0" function located on the Functions>>
Programming >> Comparison palette. This function returns TRUE if
Number is greater than or equal to 0.
d. Place a Numeric Constant located on the Functions >> Programming >>Numeric
palette and give it the value -99999.0. Right-click the numeric constant
and select Display Format. Set Digits of Precision to 1, and Type to
Floating Point Notation, and click the OK button.
e. Place the One Button Dialog function located on the Functions>>
Programming >> Dialog and User Interfaces palette. Right-click the
message terminal (top-left terminal) of the One Button Dialog function, select
Create>>Constant from the shortcut menu, type “Error ... Negative
Number”, and press the <Enter> button.
Figure 6 Block Diagram of Square Root VI, Case of negative input.

f. Select the TRUE case and place the "Square Root" function located on the
Functions>> Programming >> Numeric palette, as shown in the following
block diagram. This function returns the square root of Number.

Figure 7 Block Diagram of Square Root VI, Case of non-negative input.

3. Save the VI as “<lastname1_lastname2>_Square Root.vi” in your folder.


4. Display the front panel and run the VI.
5. Take screenshots for both the front panel and the block diagram to include them in
your lab report. Make sure that the front panel is showing working values for input
(control) and output (indicator) for positive and negative inputs.
6. In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.
7. Close the VI.
________________________________________________________________________
End of Exercise 3
Exercise 4 Formula Node Exercise VI
Objective: To use the Formula Node.
Complete the following steps to build a VI that uses the Formula Node to perform a complex
mathematical operation and graphs the results.

Front Panel
1. Open a new VI and build the following Front Panel.

Figure 8 Front Panel of Formula Node Exercise.

Block Diagram
2. Build the block diagram to match Figure 9.
a. Place the Formula Node located on the Functions >> Programming >>
Structures palette within a For Loop.
b. Create the x input terminal by right-clicking the left border of the Formula
Node and selecting Add Input from the shortcut menu.
c. Create the y output terminal by right-clicking the right border and selecting
Add Output from the shortcut menu.
Note: When you create an input or output terminal, you must use a variable name
that exactly matches the one in the equation. Variable names are case
sensitive.

d. Type the following equations in the Formula Node, where ** is the


exponentiation operator. Refer to the LabVIEW Help for more information
about syntax for the Formula Node.
float64 f = tanh(x) + cos(x);
y = f**3 + f;

Figure 9 Block Diagram of Formula Node Exercise.


3. Save the VI as “<lastname1_lastname2>_Formula Node.vi” in your folder.
4. Display the front panel and run the VI. The graph displays the plot of the equation y
= f(x)3 + f(x), where f(x) = tanh(x) + cos(x).
5. During each iteration, the VI divides the iteration terminal value by 15.0. The
quotient is wired to the Formula Node, which calculates the function value. The VI
plots the array on the waveform chart.
6. You can also play with different values of x and see the response.
7. Take screenshots for both the front panel and the block diagram to include them in
your lab report. Make sure that the front panel is showing the resulting graph.
8. In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.
9. Define the range of values used for variable x in this VI, x=[#1, #2, ……#N]
10. Close the VI.
________________________________________________________________________
End of Exercise 4

Exercise 5 Average Temperature Limit VI


Objective: To use the Sequence Structure.
1) AVERAGE TEMPERATURE LIMIT VI
a. Build a VI that calculates the average of the five random temperatures values and
check whether the average within the required bounds.
Tips/Requirements:
 Input the five temperature readings of your choice
 Use Formula Nodes to calculate the average and to figure out if the average
temperature is out of bounds.
 Use Flat sequence located in Functions>> Programming >> Structures to
execute the formula nodes one after another.

“Average” formula node. “Out of Bounds” formula node. Returns


1 if out of bounds, 0 if within bounds.
Figure 10 Formula Nodes for Average Temperature Exercise.
Figure 11 Formula Nodes for Average Temperature Exercise.

 For true case (out of bounds), Case 1:


o Add error message function by clicking on the "One Btn Dialogue"
located in the Functions>> Programming >> Dialog and User interface
palette. To control whether or not the beep occurs, use a Case structure
(Cases 0 and 1. 0 would do nothing (empty), 1 would trigger the beep.
The input to the case structure’s selector terminal would be the output
from the “Out of Bounds” formula node). Right click on the upper left
input of the One Btn Dialogue to add the error message.
o Go Functions>> Programming >> Boolean >> True Constant and add
that block within the case structure.
o Go to the front panel to add a round LED (Controls>> Modern >>
Boolean).
 For false case (within bounds), Case 0:
o Go Functions>> Programming >> Boolean >> False Constant and add
that block within the case structure.
b. Save the VI as “<lastname1_lastname2>_Average Temperature.vi” in your folder.
c. Show the instructor that it works before proceeding to the Lab Work part.
d. Take screenshots for both the front panel and the block diagram to include them in your
lab report. Make sure that the front panel is showing working values for inputs and outputs
and show values for both ‘within-bound’ and ‘out of bound’ cases.
e. In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.
________________________________________________________________________
End of Exercise 5
Lab Work Finding a Factorial
Objective: Develop a code for finding the factorial of a number
Build a VI to find the factorial of a number using a For Loop. Given below is the pseudo code for finding
the factorial of a number. (Hint: Use either feedback node from the structures palette or shift register)

n=N;
factN(1)=1;
for ii=1 to n
factN(ii+1)=factN(ii)*(ii);
end
factorial=factN(n+1);

Remember N!=N*(N-1)*(N-2)* …… 3*2*1 & 0!=1

 Take screenshots for both the front panel and the block diagram to include them in your lab
report. Make sure that the front panel is showing working values for the input number
(control) and output factorial (indicator).
 In your lab report, briefly describe the steps you made to build this VI and what are the
functions of each component appearing in the block diagram.
 Save the VI as “<lastname1_lastname2>_Factorial.vi” in your section’s folder.