PH415 Computer Interfacing 2013
OSU logo
Introduction to LabView

Table of Contents

LabVIEW Resources

  • John Essick - "Hands-On Introduction to LabVIEW for Scientists and Engineers" amazon link
  • National Instruments 622x PCI Board Specification Sheet link 1, link 2
  • San Diego State University Labview Tutorial link
  • CNX LabVIEW Graphical Programming Course link

LabVIEW by Example

  • Open Labview (Start -- Programs -- National Instruments LabVIEW 8.2.1)
  • Open Find Examples (lower right corner)
  • Search for and open Moonlanding.vi template - run and explore - under the window tab choose "Show Block Diagram" (ctrl-E) to show the code behind the front panel.
  • Open Temperature System Demo.vi template run and explore.
  • Open Power Spectrum Measurement.vi template run and explore.

While Loops

  • To program in Labview it is essential to understand their logical structures. The "While Loop" is one of the most useful logical structures and is used frequently.
  • Open a new blank vi and open both the front panel and block diagram. To make a while loop right click on the block diagram and choose "Structures" then "While Loop".
  • Notice that the while loop consists of two main features, a "stop sign" and an iterator, "boxed i".
  • When you press play While Loops execute the program (for one iteration) then checks the stop sign condition. If the stop sign logical condition is true then the code stops, if the logical condition is false then the code continues running.
  • Now lets create a sine wave and plot it using the while loop. To make the sine wave right click on the block diagram and choose "Mathematics" then "Elementary" then "Trigonometric" then sin. To plot it we need a graph, to do this right click on the front panel and choose "Graph" then "Waveform Chart".
  • Look back at the block diagram to see everything. To make the code run we need to connect the iteration terminal to the independent sine data terminal (x terminal on the left side), and we need to connect the output (right side of sine box) to the waveform chart. We need to turn the while loop on by setting the logical condition (stop sign) to false constant, which is in the "Boolean" menu.
  • Run your program. Does it work well? Can you make improvements? Discuss with lab partner.
  • One improvement we can make is to control the stopping of the while loop by not terminating it using the main program control. In general this is bad practice. You want to stop the while loop directly. To do this create a stop button by right clicking on the front panel and choosing the "Boolean" menu. Connect the stop button to the stop sign.
  • Another improvement we can make is to slow down the iteration rate from the processor rate. To do this right click and select "Timing" from the block diagram, then select "Wait(ms)". Connect a numerical constant, 100 for example, to set the number of milliseconds you want the iteration loop to pause on each cycle.
  • Any additional improvements? sine wave while loop

For Loops

  • There are two repetitive loop structures in Labview, the While Loop and the For Loop.
  • To make a for loop open up a block diagram right click and choose "Structures" then "For Loop". Notice that the for loop consists of an iterator "i", and a number of iterations "N" parameter.
  • A for loop works by doing the following: For i = 0 to i = N-1 execute the subprogram (inside the for loop).
  • Now lets make a sine wave using the for loop. First place the sine wave inside the for loop. Hook up the iterator into the input "x" sine port.
  • Create a numerical constant and place it outside the for loop. Choose the number of iterations, 100 for example.
  • To plot the data make a "Waveform Graph" in the front panel, and place it outside the for loop in the block diagram. Connect the output (sin(x)) data to the waveform graph. The waveform graph is outside of the for loop because data is created independently, then sent out of the for loop to be plotted.
  • How does your plot look? Can you make any improvements? How can you control the number of iterations from the front panel? What about the x-axes, how do we change it to radians instead of iterations?
  • Improvement suggestions are making an iteration division factor (smaller delta x). To fix the x-axes problem we need to bundle the output from the sine function using "Cluster and Variant" then "Bundle". Extend the bundle to three rows, for the top row connect a numerical value of zero, this is x0. For the middle row connect a value of 1/(Iteration Division Factor), this is delta X. Connect the bottom input row to the six(x) output data, and connect the output of the bundle to the waveform graph. sine wave for loop.

Measurement and Automation Explorer

  • You already have experience using the PCI board for data analysis, please briefly review the specification sheets if necessary.
  • Open a new vi. Before we get started programming lets access the measurement and automation explorer software which comes pre-installed in Labview. In your new vi file go to the "Tools" tab, then select "Measurement & Automation Explorer".
  • Find the PCI board in the Devices and Interfaces Menu. Run a self test to make sure that the card communication is working properly. Next check the device pinouts, which should look familiar.
  • Apply an analog signal to your PCI card and test it using the "Test Panels" tab. Change mode to "continuous". Next change the "samples to read" and the "rate", what happens? Discuss these parameters with your lab partner.

Analog Data Acquisition using the DAQ Assistant

  • Next we are going to take data using the built-in DAQ assistant in Labview. Note that the DAQ assistant has a lot of functionality, but some people prefer to brake it up and program individual data acquisition elements to gain more program control.
  • First create a while loop. Now place a DAQ assistant inside by right clicking in the block diagram and choosing "Measurement I/O" then "NI DAQmx" then "DAQ Assistant". Choose "Acquire Signals" then "Analog Input" then "Voltage". Check the connection diagram and setup the input voltage range.
  • It is a good idea to set up a time out for 100 ms inside the while loop again. Also connect a stop button to the while loop stop sign (logical condition), and the stop(T) connection of the DAQ assistant.
  • Create a waveform graph on the front panel, the data connection from the DAQ assistant will go into the waveform graph. Also create sample number and sampling frequency numerical indicators on the front panel and connect these to the "number of sample" and "rate" DAQ connections.
  • To start with I choose 100 samples, sampling frequency = 1kHz, time axes = 0-0.1 sec, and voltage values from -1V to 1V. As usual perturb these parameters and discuss the outcomes with your lab partner. What is the best strategy for collecting analog data.
  • Next we can save our data in a csv file by right clicking in the block diagram and choosing "File I/O" then "Write to Measurement File". The data from the DAQ assistant should be connected to the Signals of the write to measurement file.
  • Here I prefer to put a "case structure" around the measurement data file so I do not have to save files every time. To do this surround the measurement data file with a case structure and connect a boolean button to the case structure which allows you to push this button when you want to save.
  • Any additional improvements? DAQ assistant

Sample Variable Square Wave Code (Analog)

  • square wave. The duty factor is set by the relative temperature change from the target temperature.

Sample Temperature Controller Labview Code