0% found this document useful (0 votes)
98 views43 pages

LabView Pratimai

LabView pratimai ir uzduotys

Uploaded by

Yashman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views43 pages

LabView Pratimai

LabView pratimai ir uzduotys

Uploaded by

Yashman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

LabVIEW Basics I TM

Course Manual

UŽDUOTYS IR PRATIMAI

Course Software Version 6.0


September 2000 Edition
Part Number 320628G-01

LabVIEW Basics I Course Manual

Copyright
Copyright © 1993, 2000 by National Instruments Corporation, 11500 North Mopac Expressway, Austin, Texas 78759-3504.
Under the copyright laws, this publication may not be reproduced or transmitted in any form, electronic or mechanical, including
photocopying, recording, storing in an information retrieval system, or translating, in whole or in part, without the prior written
consent of National Instruments Corporation.

Trademarks
LabVIEW™, National Instruments™, ni.com™, and PXI™ are trademarks of National Instruments Corporation.
Product and company names mentioned herein are trademarks or trade names of their respective companies.
Lesson 2 Creating, Editing, and Debugging a VI

Exercise 2-1 Convert C to F VI


Objective: To build a VI.
Complete the following steps to create a VI that takes a number representing
degrees Celsius and converts it to a number representing degrees Fahrenheit.
1
In wiring illustrations, the arrow at the end of this mouse icon shows where
to click and the number on the arrow indicates how many times to click.

Front Panel

1. Select File»New to open a new front panel.


(Windows, Sun, and HP-UX) If you closed all open VIs, click the New VI
button on the LabVIEW dialog box.
2. (Optional) Select Window»Tile Left and Right to display the front
panel and block diagram side by side.
3. Create a numeric digital control. You will use this control to enter the
value for degrees Centigrade.
a. Select the digital control on the Controls»Numeric palette. If the
Controls palette is not visible, right-click an open area on the front
panel to display it.
b. Move the control to the front panel and click to place the control.
c. Type deg C 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, shown at left.
4. Create a numeric digital indicator. You will use this indicator to display
the value for degrees Fahrenheit.
a. Select the digital indicator on the Controls»Numeric palette.
b. Move the indicator to the front panel and click to place the indicator.
c. Type deg F inside the label and click outside the label or click the
Enter button.
LabVIEW creates corresponding control and indicator terminals on the
block diagram. The terminals represent the data type of the control or
indicator. For example, a DBL terminal, shown at left, represents a
double-precision, floating-point numeric control or indicator.

Note Control terminals have a thicker border than indicator terminals.

LabVIEW Basics I Course Manual 2-8 ni.com


Lesson 2 Creating, Editing, and Debugging a VI

Block Diagram

5. Display the block diagram by clicking it or by selecting Window»Show


Diagram.
6. Select the Multiply and Add functions on the Functions»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.
7. Select the numeric constant on the Functions»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.
8. Type 1.8 in one constant and 32.0 in the other.
If you moved the constants before you typed a value, use the Labeling
tool to enter the values.
9. Use the Wiring tool, shown at left, to wire the icons as shown in the
previous block diagram.
• 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.

1 1

• 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.
• To identify terminals on the nodes, right-click the Multiply and Add
functions and select Visible Items»Terminals from the shortcut
menu to display the connector pane. Return to the icons after
wiring by right-clicking the functions and selecting Visible
Items»Terminals from the shortcut menu to remove the checkmark.

© National Instruments Corporation 2-9 LabVIEW Basics I Course Manual


Lesson 2 Creating, Editing, and Debugging a VI

• 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, listing the name of the terminal.
• To cancel a wire you started, press the <Esc> key, right-click, or
click the source terminal.
10. Display the front panel by clicking it or by selecting Window»Show
Panel.
11. Save the VI, because you will use this VI later in the course.
a. Select File»Save.
b. Navigate to c:\exercises\LV Basics I.

Note Save all the VIs you edit in this course in c:\exercises\LV Basics I.

c. Type Convert C to F.vi in the dialog box.


d. Click the Save button.
12. 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.
13. Select File»Close to close the Convert C to F VI.

End of Exercise 2-1

LabVIEW Basics I Course Manual 2-10 ni.com


Lesson 3 Creating a SubVI

Exercise 3-1 Convert C to F VI


Objective: To create an icon and a connector pane so you can use a VI as a subVI.
Complete the following steps to create an icon and a connector pane for the
VI you built to change temperature from degrees C to degrees F.

Front Panel
1. Select File»Open and navigate to c:\exercises\LV Basics I to
open the Convert C to F VI.
(Windows, Sun, and HP-UX) If you closed all open VIs, click the Open VI
button on the LabVIEW dialog box.
The following front panel appears.

2. Right-click the icon in the upper right corner of the front panel and select
Edit Icon from the shortcut menu. The Icon Editor dialog box appears.
3. Double-click the Select tool, shown at left, on the left side of the Icon
Editor dialog box to select the default icon.
4. Press the <Delete> key to remove the default icon.
5. Double-click the Rectangle tool, shown at left, to redraw the border.
6. Create the following icon.

a. Use the Text tool, shown at left, to click the editing area.
b. Type C and F.
c. Double-click the Text tool and change the font to Small Fonts.
d. Use the Pencil tool, shown at left, to create the arrow.

© National Instruments Corporation 3-7 LabVIEW Basics I Course Manual


Lesson 3 Creating a SubVI

Note To draw horizontal or vertical straight lines, press the <Shift> key while you use
the Pencil tool to drag the cursor.

e. Use the Select tool and the arrow keys to move the text and arrow
you created.
f. Select the B & W icon and select 256 Colors in the Copy from field
to create a black and white icon, which LabVIEW uses for printing
unless you have a color printer.
g. When the icon is complete, click the OK button to close the Icon
Editor dialog box. The icon appears in the icon in the upper right
corner of the front panel and block diagram.
7. Right-click the icon on the front panel and select Show Connector 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, deg C and deg F, so LabVIEW selects a connector
pane pattern with two terminals, shown at left.
8. Assign the terminals to the digital control and digital indicator.
a. Select Help»Show Context Help to display the Context Help
window. View each connection in the Context Help window as you
make it.
b. Click the left terminal in the connector pane. The tool automatically
changes to the Wiring tool, and the terminal turns black.
c. Click the deg C control. The left terminal turns orange and a
marquee highlights the control.
d. 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.
e. Click the right terminal in the connector pane and click the deg F
indicator. The right terminal turns orange.
f. Click an open area on the front panel. Both terminals are orange.
g. Move the cursor over the connector pane. The Context Help
window shows that both terminals are connected to floating-point
values.
9. Select File»Save to save the VI, because you will use this VI later in the
course.
10. Select File»Close to close the Convert C to F VI.

End of Exercise 3-1

LabVIEW Basics I Course Manual 3-8 ni.com


Lesson 3 Creating a SubVI

Exercise 3-2 Thermometer VI


Objective: To build a VI and create its icon and connector pane so you can use it as a subVI.

Complete the following steps to create a VI that measures temperature using


the temperature sensor on the DAQ Signal Accessory. The sensor returns a
voltage proportional to temperature. For example, if the temperature is
23 °C, the sensor output voltage is 0.23 V. You also can display the
temperature in degrees Fahrenheit.

Measure the voltage using the plug-in DAQ device in your computer and
convert the voltage into a temperature reading. The sensor is hard-wired to
Channel 0 of the DAQ device.

Front Panel
1. Select File»New to open a new front panel.
(Windows, Sun, and HP-UX) If you closed all open VIs, click the New VI
button on the LabVIEW dialog box.
2. Create the thermometer indicator, as shown in the following front panel.

a. Select the thermometer on the Controls»Numeric palette and place


it on the front panel.
b. Type Temperature inside the label and click outside the label or
click the Enter button on the toolbar, shown at left.
c. Right-click the thermometer and select Visible Items»Digital
Display from the shortcut menu to display the digital display for the
thermometer.
3. Create the vertical switch control.
a. Select the vertical toggle switch on the Controls»Boolean palette.
b. Type Temp Scale inside the label and click outside the label or
click the Enter button.
c. Use the Labeling tool, shown at left, to place a free label, deg C,
next to the TRUE position of the switch, as shown in the previous
front panel.
d. Place a free label, deg F, next to the FALSE position of the switch.

© National Instruments Corporation 3-11 LabVIEW Basics I Course Manual


Lesson 3 Creating a SubVI

4. Document the VI with a description that appears in the Context Help


window when you move the cursor over the VI icon.
a. Select File»VI Properties. The VI Properties dialog box appears.
b. Select Documentation from the Category pull-down menu.
c. Type the following description for the VI in the VI Description
field:
This VI measures temperature using the temperature
sensor on the DAQ Signal Accessory.
d. Click the OK button.
5. Document the thermometer indicator and switch control with
descriptions that appear in the Context Help window when you move
the cursor over an object and with tip strips that appear on the front panel
or block diagram when you move the cursor over an object.
a. Right-click the thermometer indicator and select Description and
Tip from the shortcut menu.
b. Type the following description for the thermometer in the
Description field:
Displays the temperature measurement.
c. Type temperature in the Tip field.
d. Click the OK button.
e. Right-click the vertical switch control and select Description and
Tip from the shortcut menu.
f. Type the following description for the vertical switch control in the
Description field:
Determines the scale (Fahrenheit or Celsius) to
use for the temperature measurement.
g. Type scale - C or F in the Tip field.
h. Click the OK button.
6. Select Help»Show Context Help to display the Context Help window.
7. Move the cursor over the front panel objects and the VI icon to display
the descriptions in the Context Help window.

Block Diagram
8. Select Window»Show Diagram to display the block diagram.
9. Build the following block diagram.

LabVIEW Basics I Course Manual 3-12 ni.com


Lesson 3 Creating a SubVI

a. Place the Read Voltage VI located on the Functions»User


Libraries»Basics I Course palette. This VI reads the voltage at
Channel 0 or device 1.

Note If a DAQ device and/or DAQ Signal Accessory is not available, use the (Demo)
Read Voltage VI located on the Functions»User Libraries»Basics I Course palette
instead of the Read Voltage VI to simulate the Read Voltage VI operation.

b. Place the Multiply function located on the Functions»Numeric


palette. This function multiplies the voltage that the Read Voltage VI
returns by 100.0 to obtain the Celsius temperature.
c. Select Functions»Select a VI, navigate to c:\exercises\LV
Basics I, double-click the Convert C to F VI, which you built in
Exercise 3-1, and place the VI. This VI converts the Celsius readings
to Fahrenheit.
d. Place the Select function located on the Functions»Comparison
palette. This function returns either the Fahrenheit (FALSE) or
Celsius (TRUE) temperature value, depending on the value of Temp
Scale.
e. Right-click the device terminal of the Read Voltage VI, select
Create»Constant, type 1, and press the <Enter> key to create a
numeric constant.
f. Right-click the y terminal of the Multiply function, select
Create»Constant, type 100, and press the <Enter> key to create
another numeric constant.
g. Right-click the channel terminal of the Read Voltage VI, select
Create»Constant, type 0, and press the <Shift-Enter> keys to create
a string constant.
h. Use the Positioning tool, shown at left, to place the icons as shown
in the previous block diagram and use the Wiring tool, shown at left,
to wire them together.

Tip To identify terminals on the nodes, right-click the icon and select Visible
Items»Terminal from the shortcut menu to display the connector pane.

© National Instruments Corporation 3-13 LabVIEW Basics I Course Manual


Lesson 3 Creating a SubVI

10. Display the front panel by clicking it or by selecting Window»Show


Panel.
11. Click the Continuous Run button, shown at left, to run the VI
continuously.
12. Put your finger on the temperature sensor and notice the temperature
increase.
13. Click the Continuous Run button again to stop the VI.
14. Create the following icon, so you can use the Temperature VI as a subVI.

a. Right-click the icon in the upper right corner of the front panel and
select Edit Icon from the shortcut menu. The Icon Editor dialog
box appears.
b. Double-click the Select tool, shown at left, on the left side of the
Icon Editor dialog box to select the default icon.
c. Press the <Delete> key to remove the default icon.
d. Double-click the Rectangle tool, shown at left, to redraw the border.
e. Use the Pencil tool, shown at left, to draw an icon that represents the
thermometer.
f. Use the Foreground and Fill tools to color the thermometer red.

Note To draw horizontal or vertical straight lines, press the <Shift> key while you use
the Pencil tool to drag the cursor.

g. Double-click the Text tool, shown at left, and change the font to
Small Fonts.
h. Select the B & W icon and select 256 Colors in the Copy from field
to create a black and white icon, which LabVIEW uses for printing
unless you have a color printer.
i. When the icon is complete, click the OK button. The icon appears
in the upper right corner of the front panel.

LabVIEW Basics I Course Manual 3-14 ni.com


Lesson 3 Creating a SubVI

15. Right-click the icon and select Show Connector from the shortcut menu
and assign terminals to the switch and the thermometer.
a. Click the left terminal in the connector pane.
b. Click the Temp Scale control. The left terminal turns green.
c. Click the right terminal in the connector pane.
d. Click the Temperature indicator. The right terminal turns orange.
e. Click an open area on the front panel.
16. Save the VI, because you will use this VI later in the course.
a. Select File»Save.
b. Navigate to c:\exercises\LV Basics I.
c. Type Thermometer.vi in the dialog box.
d. Click the Save button.
17. Select File»Close to close the VI.

End of Exercise 3-2

© National Instruments Corporation 3-15 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

Exercise 4-1 Temperature Monitor


Objective: To use a While Loop and a waveform chart for acquiring data in real time.

Build a VI to measure temperature and display it on the waveform chart


using the Thermometer VI as a subVI, which you built in Exercise 3-2.

Front Panel

1. Open a new VI.


2. Select the vertical toggle switch on the Controls»Boolean palette and
place it on the front panel. You will use the switch to stop the acquisition.
3. Type Power inside the label and click outside the label or click the
Enter button on the toolbar, shown at left.
4. Select a waveform chart on the Controls»Graph palette and place it on
the front panel. The waveform chart will display the temperature in real
time.
5. Type Temperature History inside the label and click outside the
label or click the Enter button.
6. The waveform chart legend labels the plot Plot 0, so relabel the legend
by using the Labeling tool to triple-click Plot 0 in the chart legend,
type Temp, and click outside the label or click the Enter button.
7. The temperature sensor measures room temperature, so rescale the
waveform chart to display the temperature by using the Labeling tool to
double-click 10.0 in the waveform chart scale, type 90, and click
outside the label or click the Enter button. Change –10.0 to 70 in the
same way. Change the y-axis label to Temp (Deg F) and the x-axis
label to Time (sec).

LabVIEW Basics I Course Manual 4-6 ni.com


Lesson 4 Loops and Charts

Block Diagram

8. Select Window»Show Diagram to display the block diagram.


9. Enclose the two terminals inside a While Loop.
a. Select a While Loop on the Functions»Structures palette.
b. Click and drag a selection rectangle around the two terminals.
c. Use the Positioning tool to drag one corner to enlarge the loop.
10. Select Functions»Select a VI, navigate to c:\exercises\LV
Basics I, double-click the Thermometer VI, which you built in
Exercise 3-2, and place the VI on the block diagram. This VI returns one
temperature measurement from the temperature sensor.
11. Wire the block diagram objects as shown in the previous block diagram.

Note To measure temperature in Celsius, wire a Boolean constant located on the


Functions»Boolean palette to the Temp Scale input of the Thermometer VI. If you make
this change, you need to change the scales on charts and graphs in subsequent exercises
to be between 20 and 32 instead of 70 and 90, as shown in the examples in this manual.

READ NOTE PRIOR TO WIRING

12. Save the VI as Temperature Monitor.vi, because you will use this
VI later in the course.
13. Display the front panel by clicking it or by selecting Window»Show
Panel.

© National Instruments Corporation 4-7 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

14. Use the Operating tool to click the vertical toggle switch and turn it to
the ON position.
15. Run the VI.
The While Loop is an indefinite looping structure. The diagram within
its border executes as long as the specified condition is true. In this
example, as long as the switch is on (TRUE), the Thermometer VI takes
and returns a new measurement and displays it on the waveform chart.
16. Click the vertical toggle switch to stop the acquisition. This changes the
loop condition to FALSE and the loop ends.
17. Format and customize the X and Y scales of the waveform chart to suit
your display preferences and data.
a. Right-click the chart and select Y Scale»Formatting from the
shortcut menu. The following dialog box appears.

b. Click the grid style selector and select different styles for the axes
from the sub-menu that appears to experiment with different x- and
y-axis grid options. You also can experiment with scale styles,
scaling factors, mapping mode, and the format and precision of the
axis displays.
c. Select the values shown in the previous dialog box and click the OK
or Cancel buttons.
18. 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, select Clear Chart from the
shortcut menu.

LabVIEW Basics I Course Manual 4-8 ni.com


Lesson 4 Loops and Charts

Mechanical Action of Boolean Switches


You might notice that each time you run the VI, you first must turn on the
vertical toggle switch and then click the Run button. With LabVIEW, you
can modify the mechanical action of Boolean controls. The choices for the
mechanical action include: Switch When Pressed, Switch When Released,
Switch Until Released, Latch When Pressed, Latch When Released, and
Latch Until Released.

For example, consider the following vertical toggle switch. The default
value of the switch is off (FALSE).

Switch When Pressed—Changes the control value each time you click the
control with the Operating tool. The action is similar to that of a ceiling light
switch. How often the VI reads the control does not affect this action.

Switch When Released—Changes the control value only after you release
the mouse button during a click within the graphical boundary of the
control. How often the VI reads the control does not affect this action.

Switch Until Released—Changes the control value when you click the
control and retains the new value until you release the mouse button, at
which time the control reverts to its original value. The action is similar to
that of a door buzzer. How often the VI reads the control does not affect this
action.

Latch When Pressed—Changes the control value when you click the
control and retains the new value until the VI reads it once, at which time
the control reverts to its default value. This action happens whether or not
you continue to hold down the mouse button. This action is similar to that
of a circuit breaker and is useful for stopping While Loops or having the VI
do something only once each time you set the control.

Latch When Released—Changes the control value only after you release
the mouse button. When the VI reads the value once, the control reverts to
the old value. This action guarantees at least one new value.

Latch Until Released—Changes the control value when you click the
control and retains the value until the VI reads the value once or until you
release the mouse button, whichever occurs last.

© National Instruments Corporation 4-9 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

19. Modify the vertical toggle switch so temperature 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.
20. Run the VI.
21. Use the Operating tool to click the vertical switch to stop the acquisition.
The switch changes to the OFF position and changes back to ON after
the While Loop condition terminal reads the value.

Adding Timing
When this VI runs, the While Loop executes as quickly as possible.
However, you might want to take data at certain intervals, such as once per
second or once per minute.

Control loop timing with the Wait Until Next ms Multiple function located
on the Functions»Time & Dialog palette. This function ensures that no
iteration is shorter than the specified number of milliseconds.

LabVIEW Basics I Course Manual 4-10 ni.com


Lesson 4 Loops and Charts

22. Modify the VI to take a temperature measurement once every


half-second, as shown in the previous block diagram.
Wait Until Next ms Multiple function located on the Functions»Time
& Dialog palette. Ensures that each iteration occurs every half-second
(500 ms).
Numeric Constant located on the Functions»Numeric palette. When
wired to the Wait Until Next ms Multiple function, the constant specifies
a wait of 500 ms (one half-second). Thus, the loop executes once every
half-second.
Adjust the x-axis scale of the chart by selecting X Scale»Formatting
from the chart shortcut menu. Change the dX value to 0.5 (5.0 10-1)
because you added a 500 ms wait between loop iterations.
23. Save the VI, because you will use this VI later in the course.
24. Run the VI. Try different values for the number of milliseconds.
25. Close the VI when you are finished.

End of Exercise 4-1

© National Instruments Corporation 4-11 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

Exercise 4-5 Temperature Running Average


Objective: To use shift registers to perform a running average.

Modify the Temperature Monitor VI to average the last three temperature


measurements and display the average on a waveform chart.

Front Panel
1. Open the Temperature Monitor VI, which you built in Exercise 4-1.

2. Select File»Save As and rename the VI Temperature Running


Average.vi.

Block Diagram
3. Display the block diagram.

© National Instruments Corporation 4-21 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

4. Right-click the right or left border of the While Loop and select Add
Shift Register from the shortcut menu to create a shift register.
5. Right-click the left terminal of the shift register and select Add Element
from the shortcut menu to add an element to the shift register.
6. Modify the block diagram as shown in the previous block diagram.
7. Select Functions»Select a VI, navigate to c:\exercises\LV
Basics I, double-click the Thermometer VI, which you built in
Exercise 3-2, and place the VI on the block diagram. This VI returns one
temperature measurement from the temperature sensor and is used to
initialize the left shift registers before the loop starts.
The Compound Arithmetic function located on the
Functions»Numeric palette returns the sum of the current temperature
and the two previous temperature readings. Place the Positioning tool at
the corner of the function until the cursor changes to the resizing
handles, shown at left. Click the corner and drag to stretch the function
into a three-input Add function.
The Divide function located on the Functions»Numeric palette returns
the average of the last three temperature readings.
During each iteration of the While Loop, the Thermometer VI takes one
temperature measurement. The VI adds this value to the last two
measurements stored in the left terminals of the shift register. The VI
divides the result by three to find the average of the three measurements,
the current measurement plus the previous two. The VI displays the
average on the waveform chart. Notice that the VI initializes the shift
register with a temperature measurement.
8. Save the VI, because you will use this VI later in the course.
9. Run the VI.

Multiplot Charts
Charts can accommodate more than one plot. You must bundle the data
together in the case of multiple scalar inputs.
10. Modify the block diagram to display both the average and the current
temperature measurement on the same waveform chart.
a. Modify the block diagram as shown in the following block diagram.

LabVIEW Basics I Course Manual 4-22 ni.com


Lesson 4 Loops and Charts

Bundle function located on the Functions»Cluster palette. This


function bundles, or groups, the average and current temperature for
plotting on the waveform chart. The Bundle node appears as shown at
left when you place it on the block diagram. You can add additional
elements using the Positioning tool.
b. Save and run the VI. The VI displays two plots on the waveform
chart. The plots are overlaid. That is, they share the same vertical
scale.

Customizing Charts
You can customize waveform charts to match your data display
requirements or to display more information. Features available for
waveform charts include: a plot legend, a scale legend, a graph palette,
a digital display, a scroll bar, and a buffer. By default, a waveform chart
displays the graph legend showing when you place it on a front panel.

© National Instruments Corporation 4-23 LabVIEW Basics I Course Manual


Lesson 4 Loops and Charts

11. Customize the y-axis.


a. Use the Labeling tool to click 70.0 in the Y scale, type 75.0, and
press the <Enter> key.
b. Use the Labeling tool to click the second number from the bottom
on the Y axis. Change this number to 77.5 or 80.0. This number
determines the numerical spacing of the Y axis divisions.
For example, if the number above 75.0 is 77.5, indicating a Y axis
division of 2.5, changing the 77.5 to 80.0 reformats the Y axis to
multiples of 5.0 (75.0, 80.0, 85.0, and so on).

Note The waveform chart size has a direct effect on the display of axis scales. Increase
the waveform chart size if you encounter problems customizing the axis.

12. Right-click the waveform chart and select Visible Items»Scale Legend
from the shortcut menu to show the scale legend. You can place the scale
legend anywhere on the front panel. The scale legend contains the
following components.

X Axis
Y Axis

Scale Format Buttons


Scale Labels Autoscale Buttons
Lock Autoscale Switches

13. Use the scale legend to customize each axis.


a. Type in the scale labels of the legend or directly type in the labels on
the chart to change the axis labels.
b. Click the Autoscale button for the Y axis to make the scale adjust
the minimum and maximum values to fit the data in the chart. To
lock the autoscaling, press the Lock Autoscale switch to the right to
hold down the Autoscale button.
c. Click the Scale Format button to change the format, precision,
mapping mode, scale visibility, and grid options for each axis.
d. Use the Operating tool to click the Scale Legend options to see how
they work.
14. By default, the waveform chart displays the plot legend. You can move
the plot legend anywhere on the front panel. Stretch the legend to
include two plots using the Positioning tool. To change Temp to
Running Avg, double-click the label with the Labeling tool and type in

LabVIEW Basics I Course Manual 4-24 ni.com


Lesson 4 Loops and Charts

the new text. Change Plot 1 to Current Temp in the same way. If the
text does not fit, resize the plot legend from the left corner of the plot
legend with the Positioning tool. The Positioning tool changes to a
frame corner when you can resize the plot legend.
Right-click the plot in the plot legend to set the plot line style and point
style. You also can color the plot background or traces by right-clicking
the plot legend and selecting Color from the shortcut menu.
15. Right-click the waveform chart and select Visible Items»Graph
Palette from the shortcut menu to show the graph palette. You can place
the graph palette anywhere on the front panel. The graph palette contains
the following options.

Return to Zoom Button Pan Button


Standard
Mode

Zoom Subpalette

Use the Zoom button to zoom in on specified sections of the chart or on


the whole chart, as shown in the previous Zoom palette. The Pan button
allows click-and-drag scrolling (panning) in the chart. The Return to
Standard Mode button deactivates panning and zooming and returns
the cursor to its previous format.
16. Save and run the VI. While you run the VI, use the buttons in the scale
legend and graph palette to modify the waveform chart. Practice using
the formatting, autoscale, panning, and zooming features.

Note Often, modifying the axis text format requires more physical space than was
originally set aside for the axis. If you change the axis, the display might become larger
than the maximum size that the VI can correctly present.

17. Stop the VI.


18. Save and close the VI.

End of Exercise 4-5

© National Instruments Corporation 4-25 LabVIEW Basics I Course Manual


Lesson 5 Arrays, Graphs, and Clusters

Exercise 5-1 Array Exercise VI


Objective: To create arrays and become familiar with array functions.

Build a VI that creates an array of random numbers, scales the resulting


array, and takes a subset of that final array.

Front Panel
1. Open a new VI and build the front panel shown below.

a. Create a digital indicator array. Place an array shell, available on the


Controls»Array & Cluster palette, on the front panel. Label the
array shell Random Array. Place a digital indicator, available on
the Controls»Numeric palette, inside the array shell using the
shortcut menu. This indicator displays the array contents.

Choose a digital indicator from


the Numeric subpalette and
place it into the array shell.

b. Create two more digital array indicators to display data in Final


Array and Subset Array.
2. Place three digital controls to correspond to Scaling Factor, Start
Subset, and # of Elements. Enter values into these controls.

© National Instruments Corporation 5-11 LabVIEW Basics I Course Manual


Lesson 5 Arrays, Graphs, and Clusters

The VI will generate an array of 10 random numbers, scale them by the


value in Scaling Factor, take a subset of that Final Array starting at
Start Subset for # of Elements, and display the subset in Subset Array.

Block Diagram

3. Build the block diagram shown above.


For Loop structure, available on the Functions»Structures
palette—Accumulates an array of 10 random numbers at the tunnel as
the wire leaves the loop. To set the loop to run 10 times, right-click the
N and select Create»Constant from the shortcut menu. Type 10 into the
numeric constant.
Random Number function, available on the Functions»Numeric
palette—Generates a random number between 0 and 1.
Multiply function, available on the Functions»Numeric palette—Uses
the polymorphic capability of the LabVIEW arithmetic functions to
multiply each value in the Random Array by the scalar Scaling Factor.
Array Subset, available on the Functions»Array palette—Removes a
portion of an array starting where you specify and for a length you
specify. The result displays on the front panel.
4. Save the VI as Array Exercise.vi.
5. Return to the front panel and run the VI a few times.
The For Loop runs for 10 iterations. Each iteration generates a random
number and stores it at the loop boundary. The Random Array is created
at the tunnel when the For Loop completes. Then each value in the
Random Array is multiplied by Scaling Factor to create Final Array.
Lastly, a portion of the Scaled Array is displayed after the Subset Array
function removes # of Elements starting at Start Subset.
6. Close the VI.

End of Exercise 5-1

LabVIEW Basics I Course Manual 5-12 ni.com


Lesson 5 Arrays, Graphs, and Clusters

Exercise 5-3 Temperature Analysis VI


Objective: To graph data and use the analysis VIs.

Build a VI that measures temperature every 0.25 s for 10 s. During the


acquisition, the VI displays the measurements in real time on a waveform
chart. After the acquisition is complete, the VI plots the data on a graph and
calculates the minimum, maximum, and average temperatures. The VI
displays the best fit of the temperature graph.

You will use this VI later, so be sure to save it as the instructions below
describe.

Front Panel
1. Open a new VI and build the front panel shown below.

Stretch the legend using


the Positioning tool.

Change the label from


"Plot 0" to "Temp" and
"Plot 1" to "Fitted"
with the Labeling tool.

The Temperature chart displays the temperature as it is acquired. After


acquisition, the VI plots the data and its best fit in Temp Graph. The
Mean, Max, and Min digital indicators will display the average,
maximum, and minimum temperatures, respectively.

Block Diagram
2. Build the block diagram shown below. Refer to the following
instructions.

© National Instruments Corporation 5-23 LabVIEW Basics I Course Manual


Lesson 5 Arrays, Graphs, and Clusters

You can display more than one plot on a graph. This feature not only
saves space on the front panel, it is also an effective means of making
comparisons between plots. XY and waveform graphs automatically
adapt to multiple plots.
Thermometer VI, available on the Functions»User Libraries»Basics 1
Course palette—Returns one temperature measurement.
Wait Until Next ms Multiple function, available on the Functions»Time
& Dialog palette—Causes the For Loop to execute every 0.25 s
(250 ms) in this exercise.
Array Max & Min function, available on the Functions»Array
palette—Returns the maximum and minimum temperature measured
during the acquisition in this exercise.
Mean VI, available on the Functions»Mathematics»Probability and
Statistics palette—Returns the average of the temperature
measurements in this exercise.
Bundle function, available on the Functions»Cluster
palette—Assembles the plot components into a single cluster in this
exercise. The components include the initial X value (0), the delta X
value (0.25), and the Y array (temperature data). Use the Positioning
tool to resize the function by dragging one of the corners.
General Polynomial Fit VI, available on the
Functions»Mathematics»Curve Fitting palette—Returns an array
that is a polynomial fit to the temperature array in this exercise. This
exercise uses five as the polynomial order. The General Polynomial Fit
VI determines the best fit for the points in the temperature array.

LabVIEW Basics I Course Manual 5-24 ni.com


Lesson 5 Arrays, Graphs, and Clusters

Build Array function, available on the Functions»Array


palette—Creates an array of clusters from the temperature cluster and
the best fit cluster in this exercise. You can increase the number of inputs
for the function using the same method you employed for the Bundle
function. The Build Array function assembles data for the multiplot
graph into an array.
3. Save the VI. Name it Temperature Analysis.vi.
4. Return to the front panel and run the VI.
5. The graph displays the temperature data plot and best fit curve of the
temperature waveform on the same graph. Try different values for the
polynomial order constant in the block diagram.
The For Loop executes 40 times. The Wait Until Next ms Multiple
function causes each iteration to take place every 250 ms. The VI stores
the temperature measurements in an array created at the loop boundary
(auto-indexing). After the For Loop completes, the array passes to
various nodes. The Array Max & Min function returns the maximum
and minimum temperature. The Mean VI returns the average of the
temperature measurements. The VI bundles the data array with an initial
X value of 0 and a delta X value of 0.25. The delta X value of 0.25 is
required so that the VI plots the temperature array points every
0.25 seconds on the waveform graph.
6. You can modify the appearance of your plots by modifying options such
as plot styles and fill styles. You can create histogram graphs, general
bar plots, or filled plots. The Common Plots and Bar Plots palettes, in
the plot legend shortcut menu, allow you to configure plot styles such as
a scatter plot, a bar plot, or a fill to zero plot. You can configure the point,
line, and fill styles in one step.
a. Right-click the Temp plot display in the legend of the Temp graph.
Select Common Plots and select the Scatter Plot, the top middle
choice in Common Plots.

© National Instruments Corporation 5-25 LabVIEW Basics I Course Manual


Lesson 5 Arrays, Graphs, and Clusters

b. Right-click the Fitted plot display in the Legend of the Temp Graph
and select the second choice from Bar Plots in the plot legend
shortcut menu.

7. Save and close the VI.

End of Exercise 5-3

LabVIEW Basics I Course Manual 5-26 ni.com


Lesson 6 Case and Sequence Structures

Exercise 6-2 Temperature Control


Objective: To use the Case structure.

You will modify the Temperature Running Average VI to detect when a


temperature is out of range. If the temperature exceeds the set limit, a front
panel LED will turn on and a beep will sound.

You will use this VI later, so be sure to save it as the instructions below
describe.

1. Open the Temperature Running Average VI.


2. Modify the front panel as shown above.
The High Limit digital control specifies the upper temperature limit.
The Warning LED indicates if the temperature exceeds this limit.
You create the numeric digital display values by right-clicking on the
Temp History chart and selecting Visible Items»Digital Display.
3. Select Save As from the File menu and rename the VI Temperature
Control.vi.

© National Instruments Corporation 6-9 LabVIEW Basics I Course Manual


Lesson 6 Case and Sequence Structures

Block Diagram

4. Modify the block diagram as shown above.


Greater? function (Comparison palette). In this exercise, this function
returns a TRUE if the temperature measured exceeds the temperature
you specify in the High Limit control; otherwise, the function returns a
FALSE.
Beep VI (Graphics & Sound»Sound palette). In this exercise, this VI
sounds a beep if the selection terminal of the Case structure receives a
TRUE.

Note On the Macintosh, you must provide values for the Frequency, Duration, and
Intensity inputs to the Beep VI.

Notice that there are no icons in the False case of the Case structure. If
the temperature that the Thermometer VI returns is greater than the set
limit, the LED turns on, the VI executes the True case, and a beep
sounds. If the temperature is less than the set limit, the LED turns off,
the VI executes the False case, and there is no beep.
5. Save the VI. Return to the front panel and enter 80 in the High Limit
control. Run the VI.
Place your finger on the temperature sensor. When the temperature
exceeds 80°, the LED will turn on and a beep will sound.
6. Close the VI.
End of Exercise 6-2

LabVIEW Basics I Course Manual 6-10 ni.com


Lesson 6 Case and Sequence Structures

Exercise 6-4 Formula Node Exercise.vi


Objective: To use the Formula Node.

You will build a VI that uses the Formula Node to evaluate a complex
mathematical expression and graphs the results.

Front Panel

1. Open a new VI.


2. Build the front panel shown above. The Graph indicator will display the
plot of the equation y = f(x)^3 + f(x), where f(x) = tanh(x) + cos(x).

LabVIEW Basics I Course Manual 6-18 ni.com


Lesson 6 Case and Sequence Structures

Block Diagram

3. Build the block diagram shown above.


Formula Node (Structures palette). With this node, you can directly
enter formulas. Create the X input terminal by right-clicking the border
and choosing Add Input from the shortcut menu. You create the y
output by choosing Add Output from the shortcut menu. You must also
add an output the intermediate (“dummy”) variable a as an output.
When you create an input or output terminal, you must give it a variable
name that exactly matches the one in the formula. The names are case
sensitive—if you use a lower case “x” to name the terminal, you must
use a lower case “x” in the formula.

Note Notice that a semicolon (;) terminates each formula statement.

Numeric Constant (Numeric palette). In this exercise, this constant


specifies the number of For Loop iterations.
Divide function (Numeric palette). In this exercise, this function divides
the value of the iteration terminal by 15.0.
During each iteration, the VI divides the iteration terminal value by 15.0.
The quotient is wired to the Formula Node, which computes the function
value. The VI then stores the result in an array at the For Loop border
(auto-indexing). After the For Loop finishes executing, the VI plots the
array.
4. Save the VI. Name it Formula Node Exercise.vi.
5. Return to the front panel and run the VI.
6. Close the VI.

End of Exercise 6-4

© National Instruments Corporation 6-19 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Exercise 7-4 Temperature Logger.vi


Objective: To save data to a file in a form that a spreadsheet or a word processor can access
later.

You will modify the Temperature Control VI to save the time and current
temperature to a data file. You will use this VI later, so be sure to save it as
the instructions below describe.

Front Panel

1. Open the Temperature Control VI.


The front panel already is built. You will modify just the diagram.
2. Select Save As from the File menu and save this VI as Temperature
Logger.vi.

© National Instruments Corporation 7-23 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Block Diagram

1. Build the diagram shown above.


Open/Create/Replace File VI (File I/O palette). This VI displays an
interactive file dialog box that you use to create the new file or replace
an existing one.
The Enter File Name prompt is the prompt that the dialog box displays.
Enter File Name Right-click on the VI prompt terminal and select Create Constant.
prompt
The create or replace terminal creates a new file or replaces an existing
file. Right-click, select Create Constant, and use the Operating tool to
Create or replace change the value.
terminal
Tab Constant (String palette).
End of Line constant (String palette).
Get Date/Time String function (Time & Dialog palette). This function
returns the time, in string format, when the temperature measurement
was taken. The True Boolean constant (Boolean palette) sets the
function to include seconds in the string.
True Boolean

Format Into File function (File I/O palette). This function converts the
temperature measurement (a number) to a string and builds and writes
to file the following formatted data string:
Time String (tab) Temperature String (end of line).
Resize the function to have four argument terminals.

LabVIEW Basics I Course Manual 7-24 ni.com


Lesson 7 Strings and File I/O

Unbundle by Name function (Cluster palette). This function removes


the status Boolean from the error cluster.
Not function (Boolean palette).
And function (Boolean palette).
The Not and And functions control the While Loop condition such that
the loop continues while the Power switch is True and there is no error.
Close File function (File I/O palette). This function closes the file.

Simple Error Handler VI (Time & Dialog palette). This VI checks the
error cluster and displays a dialog box if an error occurred.

2. Save the VI.


3. Run the VI. A dialog box appears, prompting you to enter a filename.
Type temp.txt and click OK or Save.
The VI creates a file called temp.txt. The VI then takes readings every
half-second and saves the time and temperature data to a file until you
press the Power switch or an error occurs. When the VI finishes, it closes
the file.
4. Close the VI. You now can use a word processor or spreadsheet to open
the file you created.

Windows
5. Start the WordPad or NotePad application or another word processor or
a spreadsheet. Find and open the file temp.txt.

UNIX
5. Run the Text Editor application. Load the file temp.txt.

Macintosh
5. Switch to the Finder and launch TeachText or another word processor or
a spreadsheet. Find and open the file temp.txt.
6. After you load the file into the word processor or spreadsheet, notice that
the time appears in the first column and the temperature data appears in
the second column. Quit your word processor or spreadsheet and return
to LabVIEW.

End of Exercise 7-4

© National Instruments Corporation 7-25 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Exercise 7-5 Spreadsheet Example.vi


Objective: To save a 2D array in a text file so that a spreadsheet can access the file and to
display numeric data in a table control.

In the previous exercise, you formatted the string so that tabs separated the
columns and end of lines separated the rows. In this exercise, you will
examine a VI that saves numeric arrays to a file in a format you can access
with a spreadsheet.

Front Panel

1. Open the Spreadsheet Example VI. The VI is already built.


2. Run the VI.
The VI generates a 2D array (128 rows × 3 columns). The first column
contains data for a sine waveform, the second column contains data for
a noisy waveform, and the third column contains data for a cosine
waveform. The VI plots each column in a graph and displays the data in
a table indicator.
After the VI displays and plots the data, it displays a dialog box for the
filename. Type wave.txt and click OK or Save. Later, you will
examine the file that the VI created.

LabVIEW Basics I Course Manual 7-30 ni.com


Lesson 7 Strings and File I/O

Block Diagram

1. Open the block diagram to examine it.


Sine Pattern (Analyze»Signal Processing»Signal Generation palette).
In this exercise, this VI returns a numeric array of 128 elements
containing a sine pattern. The constant 90, in the second subVI call
specifies the phase of the sine pattern or cosine pattern.
Uniform White Noise (Analyze»Signal Processing»Signal
Generation palette). In this exercise, this VI returns a numeric array of
128 elements containing a noise pattern.
Build Array function (Array palette). In this exercise, this function
builds a 2D array from the sine array, noise array, and cosine array.

Sine Array
..
.

Noise Array Output of Build Array


..
.

Cosine Array
..
.

Transpose 2D Array function (Array palette). This function rearranges


the elements of the 2D array so that element [i,j] becomes element [j,i],
as shown below:

Sine Array Noise Array Cosine Array

Sine Array Cosine Array

.. .. ..
. . .

© National Instruments Corporation 7-31 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Write To Spreadsheet File (File I/O palette). This VI formats the 2D


array that Build Array creates into a spreadsheet string, and writes the
string to a file. The string has the following format:

Sine Noise Cosine


Array Array Array
¶ Tab

¶ ¶ End of Line
.. .. ..
. . .

Number To Fractional String function (String»String/Number


Conversion palette). In this exercise, this function converts an array of
numeric values to an array of strings that the table indicator displays.
The format string specifies the string to be in the 2-precision fractional
format.
2. Close the VI.

Note This example had only three arrays stored in the file. To include more arrays,
you can increase the number of inputs to the Build Array function.

Optional—Open the file using a word processor or a spreadsheet and view


its contents.

Windows
3. Open any word processing or spreadsheet application such as Notepad
or WordPad.
4. Find and open the file wave.txt and observe that the sine waveform
data appears in the first column, the random waveform data appears in
the second column, and the cosine waveform data appears in the third
column.
5. Exit the word processor and return to LabVIEW.

UNIX
3. Run the Text Editor application.
4. Find and open the file wave.txt and observe that the sine waveform
data appears in the first column, the random waveform data appears in
the second column, and the cosine waveform data appears in the third
column.
5. Exit the Text Editor and return to LabVIEW.

LabVIEW Basics I Course Manual 7-32 ni.com


Lesson 7 Strings and File I/O

Macintosh
3. Switch to the Finder and launch TeachText (or any word processor or
spreadsheet) by double-clicking on its icon.
4. Find and open the file wave.txt and observe that the sine waveform
data appears in the first column, the random waveform data appears in
the second column, and the cosine waveform data appears in the third
column.
5. Quit TeachText and return to LabVIEW.

End of Exercise 7-5

© National Instruments Corporation 7-33 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Exercise 7-6 Temperature Application.vi


In this exercise, you will apply what you have learned so far in this
Challenge course—structures, shift registers, sequence locals, waveform charts,
arrays, graphs, file I/O, and so on.

Your objective is to create a VI that does the following:


1. Takes a temperature measurement once every second until you stop
the VI or an error occurs.
2. Displays both the current temperature and the average of the last three
measurements on a waveform chart.
3. If the temperature goes over a preset limit, turns on a front panel LED.
4. After each measurement, logs the date, time (including seconds),
temperature, average of the last three measurements, and a one-word
message describing whether the temperature is “Normal” or “OVER”
the preset limit. The VI should log data so that each item appears in one
column of a spreadsheet. See the example on the next page.
5. After you stop the acquisition, plots both the raw temperature data and
a best-fit curve in a graph, and displays the average, maximum, and
minimum temperatures.
Hint: Start with the Temperature Logger VI you built in Exercise 7-4.
To complete step 5, copy and paste the appropriate portions of the
Temperature Analysis VI you built in Exercise 5-3.

Save your VI as Temperature Application.vi.

LabVIEW Basics I Course Manual 7-34 ni.com


Lesson 7 Strings and File I/O

Use the front panel shown below to get started.

Log your data as shown below in the example spreadsheet. Remember that
in a spreadsheet, tabs separate columns and end of lines separate rows.

Write the header to the


file before logging data.

End of Exercise 7-6

© National Instruments Corporation 7-35 LabVIEW Basics I Course Manual


Lesson 7 Strings and File I/O

Summary, Tips, and Tricks


• A string is a collection of ASCII characters. String controls and
indicators are in the String & Table subpalette of the Controls palette.
• LabVIEW contains many functions for manipulating strings. These
functions are in the String subpalette of the Functions palette.
• The string formatting function Format Into String converts numeric data
to string ASCII format.
• The string formatting function Scan From String converts ASCII data to
numeric format.
• Scan From String and Format Into String can automatically create the
format string for you. Right-click on the function and select Edit
Format String.
• LabVIEW features many functions and VIs for performing file input and
output (I/O) located on the Functions»File I/O palette.
• The File I/O functions are organized into three levels of
hierarchy—High-Level, Intermediate, and Advanced.
• When writing to a file, you open, create, or replace a file, write the data,
and close the file. Similarly, when you read from a file, you open an
existing file, read the data, and close the file.
• If you use the Open/Create/Replace VI and leave the file path input
unwired, an interactive file dialog box is displayed when the VI runs to
allow selection or creation of a file.
• A spreadsheet file is a special type of text file where a tab character
separates data columns and an end of line character separates data rows.

LabVIEW Basics I Course Manual 7-36 ni.com


Lesson 7 Strings and File I/O

Additional Exercises
7-7 Build a VI that generates a 2D array of 3 rows × 100 columns of
Challenge random numbers and writes the data transposed to a spreadsheet file.
The file should contain a header for each column, as shown below.
Use the high-level File VIs from the File I/O subpalette for this
exercise. Save the VI as More Spreadsheets.vi.
Hint: Use the Write Characters To File VI to write the header and
then the Write To Spreadsheet File VI to write the numerical data to
the same file.

Header

.
.
.

7-8 Write a VI that converts tab-delimited spreadsheet strings to


comma-delimited spreadsheet strings. That is, a spreadsheet string
with columns separated by commas and rows separated by
ends-of-line. The VI should output both the tab-delimited and
comma-delimited spreadsheet strings to the front panel. Save the VI
as Spreadsheet Converter.vi.
Hint: Use the Search and Replace String function.

7-9 Modify the Temperature Logger VI from Exercise 7-4 so that the VI
does not create a new file each time you run the VI. The VI should
append the data to the end of the existing file, temp.dat, that the
Temperature Logger VI created earlier. Run the VI several times
and then use a word processor to confirm that the VI appended new
temperature readings. Save the VI as Temperature Logger
2.vi.
Hint: Delete the Format Into File function and replace it with the
Format Into String and Write File functions. Use the pos mode and
pos offset parameters of the Write File function to move the current
file mark.

© National Instruments Corporation 7-37 LabVIEW Basics I Course Manual

You might also like