You are on page 1of 19

ELEC 243 Lab

Experiment 3.2
Writing a Labview Program
If all we could with Labview was to make computer based copies of our conventional
instruments, it would be of limited use. What makes Labview so valuable is that it is
programmable. By changing the program a single box (the Lab PC) can perform the functions
of a large collection of instruments, both conventional and unconventional. Some of the
instruments in this collection are ready-made, for example those in the program menu entries
for this course and the other ELEC lab courses.
Perhaps the greatest advantage of Labview however, is that it is user programmable: if no
instrument is available which meets your requirements, you can modify an existing one or
create an entirely new one.
Our goal for this Experiment will be to measure the resistance of the thermister and display
the result directly as a temperature reading. Since the DAQ card can only measure voltage,
our first step is to convert the resistance to a measureable voltage, but we already know how
to do this from Part 5 of Experiment 1.1. As a reminder, here's the circuit we used.

By measuring the voltage drop (


) across the 10 resistor we can compute the current .
Since the same current flows in both the resistor and the bulb, we can compute the resistance
of the bulb as
KVL:

. If

is known, we can determine

indirectly using

We need to make a few changes to the above circuit. Obviously we need to replace the light
bulb with the thermistor. Since the thermistor has a much higher resistance, we should also
replace the 10 resistor with one closer to the nominal resistance of the thermistor, in this
case 10k. In addition, since our goal is to measure temperature we will need to convert the
measured resistance into a displayed temperature reading.

Part 1: A Very Simple VI


Although the measurement we want to make is a fairly simple one, if you have never written a
Labview program before, even simple measurements are difficult. So we will approach our

final goal in a sequence of smaller steps, starting with what is probably the simplest possible
task for Labview program: the measurement of a single voltage.

Use a BNC-Banana adapter and a BNC patch cable to connect the 0-6 V power supply
Step 1: to J1-3, as you did in Part 1 of Experiment 3.1.
is the thermistor). Because the thermistor leads are
Step 2: Wire the following circuit (
very thin you will probably need to use your pliers to hold them very near the ends in
order to insert them into the breadboard without bending.

Step 3: Using your DMM, set the 0-6 V supply output (


DMM. Since the nominal value of

) to 5.00 V. Measure

with your

is 10 k, it should be close to 2.5 V.

Note
The thermistors in some parts kits have a 20 k nominal
value. If your thermistor is not 10 k, make appropriate
adjustments in the following steps.

Load and start the DMM-Scope Labview program that you used in Part 2 of
Step 4:

Experiment 3.1. Set the AD Channel knob to 5 and verify that


is properly
connected. This is another advantage of computer based instruments: different
instruments can be employed at the same point in a circuit without having to change
any connections.

Since we are going to build a VI from scratch, we will start with a new, blank VI. Stop
Step 5: the DMM-Scope VI and select "New VI" from the "File" menu. A pair of windows
should appear. The one on top will be a blank Front Panel window:

and the one on the bottom is a blank Block Diagram window:

Our first effort will be the simplest possible VI that will actually do something: we
Step 6:

will measure
and display its value. Since the front panel window is on top, let's
take care of the display first.
Right click over the panel window. You will get the Controls popup:

Place the cursor over the "Num Inds" button to get the Numeric Indicators popup:

Left click on the "Num Ind" button.


A pair of boxes with an open hand cursor will appear on the front panel.
Step 7:

Move this to an aesthetically pleasing location and left click. You will get a numeric
display labeled "Numeric."

The fact that the word "Numeric" is highlighted means that it is selected and can be
edited. Let's change the name so that we can remember what is being displayed.
Type "VT" and left click the check-mark box at the upper left of the window (
The indicator is now ready for use.

).

Now we need something for the indicator to display. Click on the Block Diagram
Step 8: window to bring it to the top. This may also bring up the Functions palette. If so,
move it out of the way or click the close box. Note that placing the indicator on the
front panel has also placed a block on the block diagram.

This block is the icon associated with the indicator. We can provide values for it to
display by connecting it to a signal source. The source we will use will be one of the
A/D converter inputs.
If you closed the Functions palette in the previous step, right click to bring it up.
Step 9:

From the Functions popup, select "Input". This will bring up the Input palette.

From this select "DAQ Assist" This will bring up a block labeled "DAQ Assistant,
attached to a hand cursor.

Position this somewhere to the left of the VT indicator block and left click.
Wait patiently. You may briefly see a dialog labeled "Initializing". After a second or
Step 10: two the "Create New .." wizard will appear.

Click on "Analog Input" in the left hand column, then click on "Voltage" in the list
that appears.

From the list of Supported Physical Channels that appears, select "ai5",
Step 11:

then click the "Finish" button at the bottom of the frame.


After another brief wait, the "DAQ Assistant" dialog appears. In the "Input Range"
Step 12: block set "Max" to 10 Volts and "Min" to -10 Volts. Set the "Terminal Configuration"
field to "RSE". The upper half of the panel should look like this when you are done.

In the lower half, set "Acquisition Mode" to "1 Sample (On Demand)".

When you're done, click the "OK" button.


Things will click and whir for several seconds. When it's all over, the "DAQ
Step 13: Assistant" box will have expanded, and should have a white band with the word "data"
in it.

We're almost done. All that remains is to connect the source (A/D converter block) to
Step 14: the destination (numeric indicator block). This process is called wiring.
Place the cursor over the small black triangle in the "data" field of the DAQ Assistant
block. It should change into an icon representing a small spool of wire. Left click once
and move the cursor to the small white triangle in the center of the left edge of the
numeric indicator icon and left click once more. That completes our first Labview
program. It should look something like this:

Let's try it out. Click on the Front Panel window to bring it to the top. Run the VI by
Step 15: clicking on the Run arrow or by typing Ctrl-R. The VT nemeric indicator should
display the voltage across
, which should still be about 2.5 V (unless the weather
has changed drastically in the last few minutes).
It's always a good idea to save your work from time to time, and since we currently
Step 16: have a working VI, this would be a good time. Select Save As... from the File

menu. Set the Save in: field to an appropriate directory (e.g. the desktop, your
network home directory, or your local Group nn folder).

Caution
Always set the destination directory when saving a VI.
Never try to save a VI in the default directory.
If "Untitled.vi" seems like an inadequate name for such a momentous work, think of a
more descriptive one (e.g. "lab3.vi") and enter it into the File name field. When
everything is in order, press the Save button.

Part 2: Converting Voltage to Resistance


So far we're no better off than we would have been using the DMM. In fact we're worse off
since all we have is a voltage. The DMM at least can read resistance directly. But given

, and Ohm's Law we can calculate resistance, and computers are very good at calculating
things.
There are two ways we could formulate this calculation. We could do it in two steps, first
computing
, then using Ohm's Law to get
.
Alternaltely, we could treat the circuit as a voltage divider, which with a little manipulation
gives

Because we will be interested in


approach.

Step 1:

Step 2:

in a subsequent Experiment, we will use the first

On the front panel, create a new numeric indicator and label it IT. You can place it
anywhere, but a convenient location would be near the existing indicator.

In order to compute the current we will have to do some arithmetic on the


signal.
Go to the block diagram window and right click to get the Functions palette. move
the cursor to the Arith/Compare block to bring up the Arithmetic and
Comparison palette.

Move to the Numeric block to bring up the Express Numeric palette.

This has everything we need: subtraction, division, and constants (for


Step 3:

and

).

Let's start with the subtraction. Move the cursor over the symbol labeled Subtract
and left click. Position the icon below the existing components on the diagram and
left click to put it down. Note that while the cursor is over the symbol there are three
small circles near the vertices of the triangle.

These indicate the connection points and will disappear when the cursor moves
away.
Step 4:

Step 5:

Repeat the above process, but select the Divide symbol. Place it slightly to the right
and a little lower than the subtract icon.
Now let's do the constants, starting with
. Select Num Const from the Express
Numeric palette and place the resulting block above and to the left of the subtract
icon. The highlighted text indicates that you can enter a new value. Type 5 and press
Enter or click the check box at the upper left of the window. Repeat this process,
placing the icon for
below and to the right of the divide icon. Enter a value of
10000. Here's what you should have at this point:

Step 6:

All that remains is to make the connections. We already know how to do this from
the previous Part: place the cursor over a connection point, wait for it to turn into a
spool of wire, click, move to the other terminal, and click again. A couple of things
to note:
1. The position of the terminals on the arithmetic blocks corresponds to the
conventions of grade school arithmetic. For subtract the minuend is on top and the
subtrahend is on the botton. For divide the dividend is on top and the divisor on the
botton.
2. To connect to an existing wire (in this case the one between the A/D converter
block and the VT display block), move the cursor near, but not directly on, the wire.
If you place the cursor on the wire it will turn into an arrow, indicating that you can
select the wire.
When you're done it should look something like this:

Let's test what we have so far. Go to the front panel and click on the Run button. If
Step 7:

all is well,

should still be about 2.5 V and

should be about 0.00025 A.

Now that we have found the current in the thermistor, finding the resistance is easy:
Step 8:

just divide
(which we measured directly) by (which we just computed). To do
this we need to add another numeric indicator to the front panel and a divide block to
the block diagram. Let's put the indicator to the right of the two existing ones (and
label it RT to remind us what it displays).

On the block diagram, place another divide block to the left of the RT indicator
block. Wire it up so that

Step 9:

and here's what we have:

Test the VI by clicking Run from the front panel. The RT indicator should display a
value close to 10000.

Part 3: Converting R to T
At this point, we have a result which could have been achieved with significantly less time
and effort by using the DMM, i.e. we have the resistance of the thermistor. What we really
want is the temperature, and this is where Labview pulls ahead of the competition. Given this
value of the resistance and the information on the thermistor data sheet, we (i.e. Labview) can
calculate the temperature.

Based on the information in the thermistor data sheet, derive a formula which gives
Step 1: the temperature (your choice of K, C, or F) in terms of the resistance of the
thermistor.
After convincing yourself that your formula is correct, summarize your derivation
Question in a form that will convince your labbie.
3:
If your formula is correct, it should be somewhat more complicated than the one
Step 2:
required to convert
to
. We could perform this computation the same way
we did for resistance, by using a numeric operation block for each addition,
division, etc. in the formula, but for a formula like this there is a more efficient and
compact approach, the formula node.
In the Block Diagram window, right click to bring up the Functions palette. Move

the cursor to the All Functions button to bring up the Functions palette.

Move to the Structures button


to bring up the Structures palette.

Finally, click on the Formula Node button.

The formula node is a variable sized block, so it is instantiated by clicking on the


desired location of the upper right corner and dragging to create the desired size.
Move the cursor to a convenient spot on the block diagram, preferably near the RT
indicator icon, then click, drag, and release to create the formula node. It should
look like this:

If you want to find out more about the components in the block diagram, you can
Useful Tip turn on the Context Help feature: select Show Context Help from the Help menu.
When context help is turned on, a window (labeled Context Help) appears in the
upper right corner of the screen. When the cursor is moved over any component in
the block diagram, a brief description and a link to additional information appears.
Since the context help window insists on always being on top, it can be a nuisance
when you aren't using it. To make it go away until you need it again, click the close
button.
A new formula node is not of much use. It has no formula, and it has no inputs or
Step 3:
outputs. Let's start with the formula. As an example, we'll use
. (If
you got this formula, you should check your derivation, as it is not correct.) Since
we can't type subscripts in labview, we will use
in place of
. To enter this
into the formula node, simply place the cursor inside the block, right click, and
start typing. The formula node uses the C language syntax, and each statement
must end with a semicolon; When you have finished, click on the check box (or
anywhere outside the formula node box).
At this point you may have noticed a change in the appearance of the toolbar: the
Step 4: run button has changed from a white arrow to a gray broken arrow (
). This
means that there is an error in the block diagram and the VI won't run. To find out
what the error is, click on the broken arrow button.
If you do this you should find that the problem is an undefined variable. Actually,
there are two: and
. We will define these in the next step when we create the
input and output terminals.
Place the cursor somewhere on the left-hand border of the formula node and rightStep 5: click. Select Add Input from the popup menu. This should create a small
rectangular box in the border.

The highlighted (black) interior of the box is a selected text field. Type RT and
click the check box. Repeat this process on the right-hand border, but select Add
Output and type T into the box.
The arrow is still broken, but now the error message complains of an "unwired
Step 6: input." Let's try to fix that: connect the input (RT) to the wire between the division
block and the numeric indicator which displays the value of
. Unfortunately,
this seems to have made things worse. Not only is the arrow still broken, but we
also have a broken wire. (Broken wires are indicated by dashed lines with a red X
in the middle.) The error list tells us: "You have connected two terminals of
different types."
Data
Ty
pe
s:

Like many programming languages (e.g. C) Labview maintains the notion of data
types. Labview's data types include familiar ones such as integer, floating point,
boolean, and string, as well as a number of unfamiliar ones (which we will try to
avoid for the time being). Labview denotes data type by the color of the wire which
carries it: integer wires are blue, floating point wires are orange, boolean wires are
green, and strings are pink. Labview also supports collection types, such as arrays
and structures. Scalars are denoted by thin solid lines, arrays by thick solid lines,
and other collections by various patterned lines. The wide dark blue lines with
internal dashes are a composite data type called dynamic data.
Dynamic data contains a lot of information in addition to the value of the sample,
for example, the time at which the sample was taken, whether any errors were
made in previous handling of the sample, etc. Dynamic data usually makes life
easer for us by bundling ancillary information along with a signal so we don't have
to be concerend about carrying it around separately, but at the moment it's causing
problems. While most blocks (e.g. subtract, divide, numeric indicator) can accept
any appropriate data type, including dynamic, others are more picky about what
they will take. For example, the formula node expects ordinary, pure numeric data
and doesn't know what to do with the additional stuff contained in dynamic data.
We can fix this by converting the dynamic data to scalar data.

Remove the offending broken wire. To do this, place the cursor directly over the
Step 7: wire, so that it turns into an arrow, and double click. Press the Delete key.
Right click to bring up the Functions palette. Place the cursor over the Sig Manip
Step 8: button to bring up the Signal Manipulation palette.

From this select the From DDT block. Place this just to the left of the RT terminal of
the formula node.
When you place the From DDT block, a dialog box labeled "Convert from Dynamic
Step 9: Data" will appear.

In the Resulting data type field, select Single Scalar, then click OK.
Wire the output of the From DDT block to the RT terminal of the formula node.
Step 10: Connect the input of the From DDT block to the wire between the division block
and the RT numeric indicator. The arrow should now be unbroken.
All that remains is to display the value of temperature that we have gone to so
Step 11: much trouble to calculate. Place another numeric indicator at an appropriate
location on the front panel and label it T. Return to the block diagram, position the
new indicator icon just to the right of the T terminal of the formula node, and wire
them together. Here's what you should have:

Return to the front panel and press the run button. If all is well, the T indicator will
Step 12: display the current temperature.

Part 4: Continuous Display


Although we have technically achieved our goal of measuring and displaying temperature, we
can only do so on demand. It would be much more useful if we had a system which could
continuously display the current temperature. Fortunately, this is a simple enhancement to
make. Like more conventional programming languages, Labview has looping and branching
constructs. What we need here is the while loop which repeats its contents until an exit
condition is met.

Step 1:

Right click in the block diagram to bring up the


Functions palette. Move the cursor to the Exec Ctrl
button to bring up the Execution Control palette.

Click on the While Loop button.

This is a variable sized block, like the formula node, and


we want it to enclose everything we currently have in our
VI. Bring the cursor into the block diagram window and
click above and to the left of all blocks, then drag until
everything is enclosed and release.
Note the green-bordered block labeled stop in the lower
right corner of the while loop. This is a free accessory
that allows us to stop the loop, which would otherwise
run forever. This block is an example of a control. A

control is the dual of an indicator, i.e. it provides input


from the front panel to the block diagram. Go to the front
panel and notice that there is a new object, a button
labeled STOP. Pressing this button causes the associated
icon to output a True value (when the button is not
pressed, the output is False).
Step 2:

Step 3:

From the front panel, press the Run button. The indicators
should now display continuously updated values. To
convince yourself that these aren't random, grab the body
of the thermistor between your thumb and forefinger,
being careful not to touch the leads. The displayed
temperature should increase. When it stabilizes, or you
get tired of pinching the thermistor, press the STOP
button.
The updating of the display, while certainly continuous,
might also be described as frantic. This is because new
samples are being taken and displayed as fast as the A/D
converter is able to take them (1,250,000
samples/second), which is much faster than any
phenomenon we might bring into the lab is capable of
changing its temperature. The net result is wasted
processor power and a blurry display which is difficult to
read.
There are a number of ways in which we can set the
sampling rate to a more reasonable value. We will utilize
one which requires minimal change to what we have
already constructed.
Return to the block diagram and again bring up the
Execution Control palette. This time select the Time
Delay block

and place it in a convenient location inside the while


loop. Accept the default delay of 1 second by clicking OK
in the Configure Time Delay dialog box. (If the block
automatically wires itself to something, remove the
resulting wire.)
Step 4:

Return to the front panel and click Run. The display


should now update at a more leisurely rate.

Step 5:

Be sure to include a printout of your block diagram in


your lab writeup. You can do this with Print

Window ...

Step 6:

from the File menu.

Save your work in a persistant location (not the desktop).


We will make further enhancements next week.

You might also like