Вы находитесь на странице: 1из 16

Experiment

Building a Virtual Thermometer

A LabVIEW program is called a virtual instrument (VI). It looks and operates like a real physical device. There are two components to a LabVIEW VI: the Front Panel and the Block Diagram. The Front Panel is a user interface that contains controls and indicators for collecting, analyzing, and displaying data. A control gets data from the user and sends it to the Block Diagram, while an indicator gets data from the Block Diagram and displays it to the user. Typical control devices include switches, knobs, dials, and sliders. Typical indicators include digital displays, graphs, thermometers, and LEDs. The Block Diagram contains code for operating the virtual instrument. As controls and indicators are added to the Front Panel, graphical program icons are automatically coded on the Block Diagram. Additional programming elements, such as loops, Boolean logic, or file I/O handlers can be added to the Block Diagram by the developer. In this activity, you will learn the basics of LabVIEW as you build a program to convert degrees Celsius to degrees Fahrenheit. In addition, you will become familiar with acquiring real temperature data using the SensorDAQ interface.

OBJECTIVES
In this experiment, you will

Learn the basics of LabVIEW programming while creating a virtual thermometer. Write a LabVIEW program to convert degrees Celsius to degrees Fahrenheit. Write a LabVIEW program to collect data from a temperature sensor connected to the SensorDAQ interface. 0-1

Engineering with Computers

Experiment 1

MATERIALS
Windows computer Vernier SensorDAQ LabVIEW Vernier Stainless Steel Temperature Sensor

PRELIMINARY SETUP
1. Connect the SensorDAQ to the USB port of a Windows computer. Tip: When the SensorDAQ is connected properly to the computer via the USB cable, the green LED at the top of the SensorDAQ housing will light up. You must have LabVIEW software and the driver software (from the SensorDAQ CD) installed. In addition, the SensorDAQ folder must be manually copied from the SensorDAQ CD to the C:\Program Files\National Instruments\LabVIEW 8.x\user.lib directory. See the SensorDAQ User Manual for directions. 2. Plug a Vernier Stainless Steel Temperature Probe into Ch.1 on the SensorDAQ.

PROCEDURE
Part I Write a LabVIEW Program to Convert Degrees Celsius to Degrees Fahrenheit

You will write a LabVIEW program that will convert degrees Celsius to degrees Fahrenheit. The finalized Front Panel and Block Diagram are shown below. The steps that follow provide the instructions and tips that will allow you to create this program from scratch.

1. 2.

Launch LabVIEW. Under the New section of the Getting Started window, select Blank VI. Two windows will appear: the Front Panel (gray) and the Block Diagram (white). 0-2

Engineering with Computers

Virtual Thermometer

3.

Add a control to the Front Panel. a. If the Front Panels control palette is not visible, select View Controls Palette from the Front Panels Menu bar to display it. (If you are using LabVIEW 7.1 select Window Show Control Palette). Tip: The Controls palette contains the controls and indicators you use to create the front panel. Right-clicking the mouse on the Front Panel will bring up the Controls Palette. Click your mouse on the thumbtack icon (upper left corner) to tack it down. After selecting a control, close the window.

b. Position your cursor on the Numeric subpalette to open it. c. In the Numeric subpalette find the control called Numeric Control and click on it to attach it to the cursor. Drag it on the Front Panel, and place it on the Front Panel by clicking a second time. d. Rename the Numeric Control deg Celsius.

4.

Add an indicator to the Front Panel. a. From the Numeric subpalette click on the control called Thermometer to attach it to the cursor, and then place it on the Front Panel to the right of the deg Celsius control. b. Rename the Thermometer deg Fahrenheit.

Engineering with Computers

0-3

Experiment 1

5.

Resize the Thermometer. a. If the Tools palette is not visible select View Tools Palette to display it.

b. Make sure that the Automatic Tool Selection button is enabled (square LED at the top should be bright green). If it is not enabled, click on it.

c. Move your cursor back and forth over the thermometer and note how the cursor changes tools automatically. Also note the appearance of resizing handles (little blue squares) at the points where you can resize the object. Tip: You can manually choose your tool by disabling the automatic tool selection and clicking the tool from the Tools Palette that you wish to use. d. Place your cursor on top of the resizing handles until the cursor changes into a double-sided arrow. Click and drag to resize the thermometer. Change properties of the Thermometer. a. Right-click on the Thermometer and select Properties. b. The temperature scale should encompass the freezing and boiling points of water (32-212F). To increase the data range, click on the Scale tab. In the Scale Range box, set the Maximum value to 250. c. Click on the Appearance tab. Change the thermometers fill color by clicking on the red box inside the Colors section and selecting a color of your choice. d. Click the OK button to return to the Front Panel. Go to the Block Diagram by selecting Window Show Block Diagram from the Front Panel Menu. Tip: A shortcut for toggling back and forth between the Front Panel and the Block Diagram is to press Ctrl E. You can also get to the Block Diagram by clicking on the Block Diagram window.

6.

7.

0-4

Engineering with Computers

Virtual Thermometer

8.

Two icons that are linked to the Front Panel control and indicator appear on the Block Diagram. Position the deg Celsius control to the left of the deg Fahrenheit indicator.

Tip: The control icon has a small white arrow on the right border, whereas the indicator icon has a small white arrow on the left border. Also, the control has a thick border, and the indicator has a thin border. The color and symbol of each terminal indicate the data type of the corresponding control or indicator. Tip: The control and indicator shown above are icon terminals, which is the default style. However, you can configure front panel controls or indicators to appear as data type terminals on the block diagram. To do this, right-click the icon terminal and remove the checkmark next to the View As Icon shortcut menu item. 9. Add the mathematics functions in your Block Diagram code to convert temperatures in degrees Celsius to temperatures in degrees Fahrenheit using the formula: F = 9/5C + 32. a. If the Functions palette is not visible, select View Functions Palette to display it. (If you are using LabVIEW 7.1 this will be found by selecting Window Show Functions Palette). Tip: Right-clicking the mouse on the Block Diagram is a quick way to bring up the Functions Palette. Click your mouse on the thumbtack icon (upper left corner) to tack it down. After selecting a function, close the window.

b. Click on the Numeric subpalette to open it.

Engineering with Computers

0-5

Experiment 1 c. Click on the numeric function called Divide to attach it to the cursor. Then place this function on the Block Diagram above the deg Celsius control.

d. Position the cursor over the Divide icon. Three orange circles will appear at the location of the input and output terminals. Wires connect to these terminals to pass the data into and out of the function. The divide function has two input terminals (on the left) and one output terminal (on the right).

Tip: Select Show Context Help from the Help menu to display a popup window identifying each terminal.

e. Right-click on the upper left input terminal and select Create Constant from the popup window. A constant is not linked to the Front Panel and cannot be changed by the user when the program is running. f. Enter 9 into the box. Note that this numeric constant is automatically wired to the terminal.

Tip: When you create the constant it will be highlighted, allowing the value to be modified. If it is not highlighted simply double-click on the value to highlight and edit. g. Right-click on the lower left terminal and select Create Constant from the popup window. h. Enter 5 into the box. i. Click on the Multiply function in the Numeric subpalette and place it on the Block Diagram to the right of the Divide function.

j. Move your cursor to the Divide functions output terminal. When the cursor is located at the

0-6

Engineering with Computers

Virtual Thermometer terminal it will change to the wiring tool. At this location click the mouse to start building a wire. Move your wiring tool to the upper left input terminal of the Multiply function. When the wiring tool is located at the terminal the terminals orange circle will pulsate. Click the mouse at the terminal to finish the wire.

Tip: In LabVIEW, wires transfer data between objects. In this case, the result of the divide function is transferred to the multiply function. The data type that is being transferred determines the thickness, color, and style of the wire. Tip: If the automatic tool selection button is enabled the cursor automatically changes to the wiring tool when the cursor is positioned over a terminal (otherwise you must manually select the wiring tool from the Tools palette). k. Connect the lower left input terminal of the Multiply function to the deg Celsius terminal.

l. Click on the Add function in the Numeric subpalette and place it on the Block Diagram between the Multiply function and the deg Fahrenheit terminal. m. Connect the upper left input terminal of the Add function to the output terminal of the Multiply function. Tip: When placing a new object, if you position it near another object, LabVIEW will automatically wire valid connections. n. Right-click on the lower left input terminal of the Add function and select Create Constant. o. Enter 32 into the box. p. Connect the output terminal of the Add function to the input terminal of the deg Fahrenheit terminal.

10. 11. 12.

Select File Save and save the file on your desktop with the name Temperature Conversion. Return to the Front Panel (Ctrl E) and set the deg Celsius control with a value of 0. Run the VI by clicking the Run button (white arrow in the upper left screen). The thermometer indicator should jump to 32. 0-7

Engineering with Computers

Experiment 1

Tip: If the Run button looks like a black broken arrow, it means you have an error in your program. Clicking on the broken arrow will bring up a list of errors. Double-clicking on a specific error will take you to the exact location in the Block Diagram where the error exists.

13.

Run the program several more times using different values in the deg Celsius control. Consider adding a digital display to the Thermometer by right-clicking on the Thermometer and selecting Visible Items Digital Display.

Part II Modify the Block Diagram to Make the Program Repeatable

In the previous VI, every time you want to convert a number you must run the program. In this second part of the experiment a While Loop will be placed around your code so that the program will execute continuously until you press a Stop button located on the Front Panel. The finalized Front Panel and Block diagram are shown below.

1. Go to the Block Diagram (Ctrl E).

0-8

Engineering with Computers

Virtual Thermometer 2. From the Functions palette, open the Structures subpalette and click on the While Loop.

3. Encircle your code with the While Loop by clicking the Block Diagram where you want to place the top left corner, and then drag the cursor down and to the right until all of the Block Diagram objects are within the structure.

4. Right-click on the While Loops conditional terminal (stop sign) in the bottom right corner, and select Create Control. A Stop button control will automatically be placed on the Front Panel. Double-click the stop icon to automatically locate this control on the Front Panel.

Tip: The continuation behavior of the While Loop depends on how the conditional terminal is configured. Right-click the conditional terminal to see that there are two configuration options in the shortcut menu: Stop if True or Continue if True. In this example we will use Stop if True. The While Loop will repeat the code that is encircled within the structure until the Boolean value wired into the conditional terminal is True. The While Loop always executes at least once. The iteration terminal (blue box with an i in the bottom left corner) provides the current loop iteration count, which is zero for the first iteration. 5. Go to the Front Panel if you are not already there (Ctrl E). 6. Run the program. a. Use the up and down arrows next to the deg Celsius control to change the value. Observe the deg Fahrenheit indicator automatically display the corresponding reading in degrees Fahrenheit.

Engineering with Computers

0-9

Experiment 1 b. Now change the deg Celsius control value by typing in a value. To replace the old value you must enter the new value. Do this by clicking on the Enter Text icon that appears on the Front Panel toolbar, pressing the <Enter> key, or clicking the Front Panel workspace. 7. End program execution by clicking the Stop button control.

Tip: When using any form of looping structure, you should avoid ending the program execution by clicking the Abort Execution button (stop sign icon) on the tool bar. Doing so may possibly leave the program or equipment in a precarious state.
Part III Modify the Program to Use a Temperature Sensor for Data Collection

The SensorDAQ interface allows you to collect real data with your LabVIEW VI. There are four input ports (one digital and three analog) for connecting various sensors to the SensorDAQ. In this activity, you will acquire actual temperature data from a Vernier Stainless Steel Temperature Sensor connect to analog port Ch.1. The picture below shows the finalized Front Panel and Block Diagram.

Tip: Simple instructions to create your own program follow. However, another learning method is to open a similar VI and modify it to fit your requirements. The example file, AnalogInEx01_Ch1.vi, is more complex than this activity, but could be used as a starter VI. AnalogInEx01_Ch1.vi reads data from any analog sensor plugged into Ch.1 on the SensorDAQ and plots the results to a chart. 0 - 10
Engineering with Computers

Virtual Thermometer 1. 2. Go to the Block Diagram (Ctrl E). You will need to make room inside the While Loop for more objects. Enlarge the While Loop by placing your Position tool on the lower left corner until it changes to an arrow (this will happen automatically if you have the automatic tool selection button enabled). Then click and drag to resize the loop.

3.

Open the Functions Palette (right click anywhere in the Block Diagram) and open the SensorDAQ subpalette found inside of the User Libraries palette.

Tip: Do not use the User Libraries subpalette that is found within the Express palette.

Tip: There are four main driver subVIs that are required to operate the SensorDAQ for data acquisition: SETUP, START, READ, and STOP. A subVI is a block of code that has been preprogrammed for you and stored within a single icon.

Engineering with Computers

0 - 11

Experiment 1 4. Place the SETUP, START, READ, and STOP subVIs on the Block Diagram as shown in the figure below. Make sure to line up the icons horizontally. This can be done manually or it can be done automatically by selecting the four icons and using the Align Objects button on the Block Diagram toolbar.

5.

Wire the four SensorDAQ subVIs together via the terminals labeled DAQmx Task Cluster, Setup Parameters, and Error In/Out. a. Move your cursor over the SETUP subVI. Notice that when the cursor is located over a terminal a tip strip will appear providing the name of the terminal, and the cursor will automatically turn into the wiring tool. Find the output terminal called DAQmx Task Cluster, located in the upper right hand corner, and click the wiring tool to start a wire. b. Move the wiring tool to the input terminal of the START subVI called DAQmx Task Cluster (upper left hand corner). When the wiring tool is located directly on this terminal the name of the terminal will appear. Click the mouse to complete the wire from the SETUP subVI to the START subVI.

c. Wire the DAQmx Task Cluster output terminal of the START subVI (upper right corner) to the DAQmx Task Cluster input terminal of the READ subVI (upper left corner).

Tip: Wiring across a While Loop automatically creates a loop tunnel (indicated by a square) on the border. Loop tunnels are required for wiring objects outside a loop to objects inside the loop. d. Wire the DAQmx Task Cluster output terminal of the READ subVI (upper right corner) to the DAQmx Task Cluster input terminal of the STOP subVI (upper left corner). e. Now wire the terminal called Setup Parameters between the four subVIs. Notice that this terminal is located slightly below the DAQmx Task Cluster on the icon of the subVIs.

0 - 12

Engineering with Computers

Virtual Thermometer f. Wire the terminal called Error in/out between the four subVIs. Notice that this terminal is located at the bottom of the subVI icons. Wiring the three data wires between the four subVIs should look something like the figure below.

6.

Index out a single data point from an array data type. Tip: To provide simplicity and versatility in programming, the READ subVI sends out data in array format. This allows for both single-point (an array of 1 element) and multi-point data collection (an array of N elements). However, array data cannot be sent directly to a thermometer indicator, because this type of indicator requires single point data. To extract the single point out of an array, use the Index Array function. a. Open the Function palette (right click on the Block Diagram) and locate the Array subpalette. b. Click on the function called Index Array and place it above the Read subVI. c. Wire the READ subVIs CH1 output terminal (top left corner) to the Index Arrays input terminal called array (upper left corner)

Tip: Note the thickness of this wire. Having some thickness shows that the data type transferred by this wire is an array. A thin wire represents the transfer of a single element data type. 7. Since the temperature in degrees Celsius is now being generated directly from a temperature sensor, the user-defined control labeled deg Celsius is no longer relevant. However, an indicator displaying the sensor reading in degrees Celsius would be useful. Therefore, change this control to an indicator. a. Right-click on the deg Celsius terminal in the Block Diagram and select Change to Indicator from the popup menu. Tip: This will break the wire connecting the deg Celsius terminal to the Multiply function. Pressing Ctrl B at any time will remove all broken wires.

b. Wire the terminal of the deg Celsius indicator to the Index Arrays output terminal called

Engineering with Computers

0 - 13

Experiment 1 element (bottom right corner).

c. Also wire the bottom left terminal on the Multiply function to the element terminal. Tip: A dot is used to show that two wires are connected together. If the two wires merely cross each other, no dot is shown.

8. Configure the SETUP subVI to collect from a Vernier Auto-ID Sensor that is connected to Ch.1. a. Right-click on the SETUP subVIs input terminal called Config Ch 1-3 (Off) (located at the upper left corner) and select Create Constant from the popup menu.

b. The constant that appears is an enumerated data type. Click on the down arrow next to the OFF text to see a list of options for this constant. Choose the ON selection.

9. 10. 11.

Confirm that your Block Diagram code looks similar to the figure above. Save the program. Go to the Front Panel (Ctrl E).

0 - 14

Engineering with Computers

Virtual Thermometer 12. Test your program by clicking the Run button. As the program runs, place your hand over the tip of the Stainless Steel Temperature Probe. This should cause the temperature reading to increase. Monitor the deg Celsius and deg Fahrenheit indicators as the program runs. End the program by pressing the stop button.

13.

EXTENSIONS
1. Currently the program displays degrees Celsius on one indicator and degrees Fahrenheit on another. Modify the program to display a single reading on one Thermometer indicator. A Front Panel Push Button will allow the user to select the units as degrees Celsius or degrees Fahrenheit. The function that can be used to send a value depending on a Boolean input is the Select Function (found in the Comparison subpalette). The final program will look similar to the following:

2. It is important to understand that Vernier Auto-Id sensors have many default parameters physically stored on the sensor. The code contained within the SETUP subVI reads these sensor parameters. The wire linked between the subVIs, connected to the Setup Parameters terminals, passes this sensor information between the subVIs. The sampling rate is one of these stored parameters. In many cases the default sampling rate is appropriate and no modification is required. However, if you want to have more control of this rate there is a subVI that can be used to easily modify it; this subVI is called MODIFY, and is located in the SensorDAQ subpalette. To add this subVI, delete the wires between the Setup and Start subVIs. Then, place the Modify subVI between them. Make sure the subVIs are lined up horizontally and then re-wire.

In addition, there are two terminals on the MODIFY subVI that require inputs. First, right-click on the Collection Parameter Enum input terminal (top, left side), and choose Create Constant. Make sure this input is set as Modify with inputs into this subVI. Then, right-click on the Sample Rate (samples/sec) input (left side, middle) and choose Create Control. You now have a Front Panel control for your data collection rate. Do not exceed 200 samples/second 0 - 15

Engineering with Computers

Experiment 1 with this example. The figure below shows how the Modify subVI is integrated.

0 - 16

Engineering with Computers

Вам также может понравиться