You are on page 1of 6

LabVIEW and myDAQ

National Instruments NIWeek 2010


Andrew Milluzzi

What is LabVIEW?
LabVIEW is a graphical programming language that implements dataflow programming. LabVIEW was first developed for virtual instrumentation. In other words, the software is the instrument. Each LabVIEW program is called a VI which is short for Virtual Instrument. A VI contains two windows, a Front Panel and a Block Diagram. The front panel is the user interface to the instrument. For example, imagine an oscilloscope, there is a graph, buttons, knobs, dials, etc. The front panel of a LabVIEW VI allows you to create these interfaces in software. The block diagram is like the wiring inside the oscilloscope. You can place blocks to add gain, process waveforms, etc. While LabVIEW was initially developed for instrumentation, it has been used in many different applications. For example, one of the most public applications of LabVIEW is the LEGO Mindstroms NXT. Additionally, LabVIEW has also been used in robots in the DARPA Challenges, to perform real time analysis of the Birds Nest Olympic Stadium in Beijing, and even control CERNs Large Hadron Collider. Today, however, we will be using LabVIEW for its intended purpose and interface with the new NI myDAQ.

Getting Started Calculator


Addition
Before we get to the myDAQ, lets learn some basics about LabVIEW. After you have installed LabVIEW and the myDAQ drivers, open LabVIEW 2010. When the software finishes loading, you should see the screen pictured right. For our experimentation, click Blank VI under the New category to launch a new VI. This will launch the two windows that make up a virtual instrument. On either window, click FileSave to save the VI. Save the program under the name Calculator. For this first program we will make a simple calculator that can add, subtract, multiply and divide. Ensure the window named Front Panel is open. We first need to make the Page | 2

Figure 1: LabVIEW Getting Started Window

interface for our program. For our calculator, we will have two controls and one indicator. A control allows the user to input data to the block diagram. Remember that the end user does not have access to the block diagram at runtime. An indicator will allow the block diagram to communicate data back to the user. Lets place two Numeric1 controls and one Numeric indicator as show in Figure 2. The controls and indicators can be found in the Numeric Palette. In Figure 2, I have named the controls and indicators. To change the name of a control or indicator, double click the existing name, and enter a new name. Also, should you wish to include a name or instructions, you can double click anywhere on the front panel and the text tool will allow you to create a text box. Now lets look at the block diagram. Notice that there are already three blocks on the diagram. These blocks represent the controls and indicators you placed on the front panel. Also note the colors of the blocks. LabVIEW defaults to the double precision data type2. You can select a different data type by right clicking the block and selecting Representation. For our demo, leave the values as double precision.
Figure 2: Calculator Front Panel

Lets wire up our first simple program that performs basic addition. To make LabVIEW perform an add, select ProgrammingNumericAdd from the functions palette. This will place an add block on the block diagram. Now hover your mouse over one of the terminals of the blocks. 3 Note the change in cursor. The wire coming off the spool is the new point of the cursor. Wire each control to a terminal on the add block. Then wire the output of the add block to your indicator as shown in Figure 3. Note how the data will flow from left to right on the block diagram. Congratulations, you have written your first LabVIEW program!

Numeric controls or indicators are number inputs and outputs. Other types include Boolean, String, Array, Cluster, and Graph. There are other data types, but this will be enough for this demo. 2 Double precision is a 64-bit floating point data type. In other words this allows decimal numbers to be returned to the user. Fixed point, integer (both signed and unsigned), and single point data types are also supported for numeric data. 3 When your mouse hovers over a terminal, the mouse cursor will switch to a spool of wire and a dot will appear on the block, representing the terminal.

Page | 3

Running a LabVIEW VI

Now that we have made our program, we need to run it. There are a few options when it comes to running a LabVIEW VI: Single Execution and Continuous Execution. Single Execution will run your program once, while Continuous Execution runs until the software encounters an error or you abort the program. In the upper left corner of both the block diagram and front panel, you will find a set of symbols as shown in Figure 4. These icons perform the following functions Figure 4: Execution from left to right: Single Execution, Continuous Execution, Buttons Abort, Pause, and Highlight Execution. Highlight Execution (sometimes called Debug mode) allows you to see an animation of the dataflow in your program. Little circles, representing the data, flow between blocks and green arrows appear on blocks that are currently running. This can be a great way to debug programs. Lets enter some numbers on the front panel and click the Single Execution arrow. Notice that the indicator now shows the sum of the two numbers you entered. Try Single Execution again, but before you click the arrow, click on the Highlight Execution light bulb. The program runs slower, but if you look at the block diagram during execution you can see the animation. This can be very useful! Now a calculator isnt very useful if we keep having to hit run each time. Try clicking Continuous Execution (you should turn off Highlight Execution). You should be able to change the values in the controls and watch the indicator change as well. This is great and all, but wouldnt it be cool to perform more than one function?

Figure 3: Calculator add Block Diagram

Case Statements and Subtraction


Software becomes smart when we allow it to make decisions. In most languages this is using a structure called an If or Switch statement. LabVIEW groups these concepts into its Case Structure. The case structure is located on the Functions Palette under ProgrammingStructuresCase Structure. Lets add one to the block diagram. Click the icon, and note how the cursor changes.4 Draw a box around the add block (and only the add block).5 You Figure 5: Case Structure can draw the box by clicking where you want the upper left corner of the box to start and drag to where you would like the lower right corner of the box to be. The case structure is shown in Figure 5. The case structure has a small block on the right edge. This is where you would wire the
4

There are no blocks for structures, rather they will change the cursor and allow the user to draw the size necessary for the application at hand. 5 Drawing the case structure around more than the add block will cause the indicators only to be read when running that case. This can cause excess controls and indicators and clutter the front panel.

Page | 4

boolean to control which case is executed: true or false. We will use a simple front panel control to pick add or subtract. Right click on the left terminal of the decision block and select CreateControl. This is a great shortcut when working on the block diagram. If you switch to the front panel, it should look similar to Figure 6. Notice that the run arrow is broken. This means our program is not complete. We need to add something to the false case of the case structure. To switch the structure, click the arrow pointing left or right by where it says True. Now in the same palette as the add block, drop in a subtract block and Figure 6: Boolean Control on Front wire up the inputs and outputs. Our calculator can now add Panel and subtract. Run your program setting the Boolean control to add or subtract.

Multiply and Divide


Adding the multiplication and division functions is similar to how we added the subtraction function. I had mentioned above that a case structure can act as a Switch statement. To use this functionality you need to wire up a numeric control instead of a Boolean control. To keep things simple, we can simply add a numeric control on the front panel and delete the Boolean control. Lets wire this control up to our case statement as seen in Figure 7. Notice how I set the representation of the control to U8. This means we are using an unsigned 8-bit integer. Also notice that the false case becomes 0 and also contains the term Default. In a switch statement, should a match not be found, we need to run some code and the default case is the one that is executed. We need to add 2 new cases to support multiply and divide. To add a new case, right click and select Add case after. This creates a new case in which to place the divide function. Repeat this action to create a case for the multiply function. Wire up the terminals to the input and outputs and try running your program. You have now implemented the 4 basic arithmetic

Figure 7: Case Structure with Numeric Input

functions. By selecting 0 through 3 on the switch statement control, you can control which function is performed. This is good and all, but what about looping?

Looping in LabVIEW
We have already seen how we can loop through our code via continuous execution; however sometimes we want control in our Page | 5

Figure 8: While Loop

code. Looping in the code allows us to have only sections of code loop. LabVIEW provides several types of loops. Like other programming languages, LabVIEW provides For and While loops. Loops are located on the functions palette in ProgrammingStructures. For our code, lets use a While Loop. Like the case structure, you will need to draw the box. This time, however, draw the box around the controls, indicator, and case structure. This ensures the latest values are processed. When you have completed these steps, your code should look like Figure 8. Notice that the run arrow is still broken. This is due to two new blocks in the While loop. The While loop adds both an iterator block (the i inside the blue box in the lower left corner of Figure 8). This counts the number of times the code has executed.6 The other icon in the lower right corner of the loop is the Stop if True icon. The loop will continue to iterate until a value of true is passed to this block. 7 Figure 9: Finished Calculator Block Diagram For our use we will wire a control to the stop condition. Your final block diagram should look like Figure 9.

The counter starts at 0. Thus your stop condition must be one less than the total number of iterations you would like. 7 Another possible symbol in this position is the Continue while True icon. As the name suggests, the loop will continue until a false is passed to the block.

Page | 6

You might also like