Acquire and Analyze Signals With LabVIEW Real-Time

Publish Date: Jun 08, 2017 | 3 Ratings | 4.67 out of 5 | Print | Submit your review


Learn how to deterministically acquire data and analyze it with ready-to-run libraries built into LabVIEW. Follow the tutorial below or open and walk through the attached sample solution.

Download LabVIEW Evaluation Download the Sample Code

Starting a New Project in LabVIEW and Adding a Real-Time Target


Begin by creating a new project in LabVIEW, where you manage your code and hardware resources.

1. Create a new project in LabVIEW by selecting File»Create Project. Then select Blank Project.

2.  The default project tree includes My Computer, which is where you can write code that runs on the Windows development machine. Remember that a real-time target has a processor running a real-time OS, so it’s effectively another computer. To write code that runs on that computer, you need to add another target to your project. To add a real-time system to the project, right-click on the Project item at the top of the tree and select New»Targets and Devices…


3. With this dialog, you can discover existing systems on your network or add a new system. Since you are just evaluating the software, select New target or device. LabVIEW lists available hardware corresponding with the drivers you have installed. Since you installed the NI-RIO driver with your evaluation software, select and expand Real-Time CompactRIO and select cRIO-9068.


4. If you had an existing system, LabVIEW would attempt to detect the I/O present in your system and automatically add it to the LabVIEW project. Since you are adding a new system, you need to manually add a C Series I/O module. To do this, right-click on the Chassis project item, named for the backplane of a CompactRIO, and select New»C Series Modules…

5. In the same manner as the target, since you do not have physical hardware, select New target or device and C Series Module and then click OK.

6. In the dialog box that appears, select an NI 9211 thermocouple module for Mod1 and then click OK.

7. You are now ready to start programming and accessing I/O on your real-time target. Navigate to File»Save to save your project as Real-Time Evaluation.lvproj. Click OK. Note that you communicate with your target by entering its IP address once it’s properly configured on the same network as your development computer.

Developing a LabVIEW Real-Time VI


This section walks you through creating a basic real-time control application on CompactRIO. You should now have a new LabVIEW project that contains your CompactRIO system and C Series I/O module. This tutorial uses an NI 9211 thermocouple input module; however, you can follow this process for almost any analog input module.

1. This project contains only one VI, which is the LabVIEW Real-Time application that runs embedded on the real-time processor. Create this VI by right-clicking on the CompactRIO real-time controller in the project and selecting New»VI. Save the VI as

2. Double-click on to open it. A VI is composed of two windows, the gray front panel, which serves as the user interface, and the white block diagram, where code is developed.

3. The basic operation of this application includes initialization and then two parallel tasks. A flat sequence structure is an easy way to make sure initialization happens first in data flow. In the white block diagram window, right-click in the blank space to bring up the Functions palette and then select Programming»Structures»Flat Sequence Structure. 

4. Now add a Timed Loop (right-click and select Programming»Structures»Timed Structures»Timed Loop) to the right of the sequence structure. You can deterministically synchronize the code inside Timed Loops to various timebases and run it continuously until a stop condition is met.


Drag with your mouse the rectangle where you want the loop to appear and then release it.

5. To configure the Timed Loop, double-click on the clock icon on the left input node.


6. Select Synchronize to Scan Engine as the Loop Timing Source. Click OK. This causes the code in the Timed Loop to execute once, immediately after each I/O scan, to ensure that any I/O values used in this Timed Loop are the most recent values. With the NI Scan Engine, you can access I/O modules directly in LabVIEW Real-Time on a CompactRIO target without programming LabVIEW FPGA. Leave all other options as default.

7. The previous step configured the Timed Loop to run synchronized to the scan engine. Now configure the rate of the scan engine itself by right-clicking on the top-level RT CompactRIOTarget in the LabVIEW project and selecting Properties.

8. Select Scan Engine from the categories on the left and enter 100 ms as the Scan Period. This causes all of the I/O in the CompactRIO system to be updated every 100 ms (10 Hz). You also can set the Network Publishing Period from this page, which controls how often the I/O values are published to the network for remote monitoring and debugging. Click OK.

9. Now that you have configured the I/O scan rate, it is time to add the I/O reads to your application for control. When using CompactRIO Scan Mode, you can simply drag and drop the I/O variables from the LabVIEW project to the RT block diagram. Expand the CompactRIO chassis and I/O module. Select AI0, click on it, and then drag and drop it into the Timed Loop on your diagram as shown below. Also drag AI1 into the Timed Loop.

10. Next add analysis that runs on the target to the program. Right-click the block diagram to open the Functions palette and click on the Search button in the top right of the palette.


11. Search for “Mean,” select Mean in the Signal Processing palette, drag two instances to your block diagram in the Timed Loop, and wire your I/O as shown in the image below. Create a constant by right-clicking on the sample length input terminal and type in a value of 100.

12. The next logical step would be to write the data to the user interface; however, it’s a best practice to separate high-priority acquisition from presenting or logging the data. This minimizes jitter and allows your Timed Loop to run with “real-time” performance, meaning that it will always finish on time. By running tasks in separate loops at distinct rates, you also maximize CPU efficiency by executing each task only as often as necessary.

Transfer the latest result of the mean calculation from the high-priority Timed Loop to a While Loop using a real-time FIFO. This provides a buffer between the two loops. The Timed Loop runs, synchronized to the I/O scan, and writes the latest result of the mean calculation to the buffer each time. The While Loop reads the data out of the buffer and writes it to the user interface to display temperature data at run time. Place down a While Loop beneath the Timed Loop (Structures»While Loop).

13. Next, create the shared variables to transfer values between the high-priority Timed Loop and low-priority While Loop. To create a shared variable, right-click your RT CompactRIO Target in the LabVIEW project and select New»Library. Right-click on the new library and select Save»Save. Rename your library something intuitive like “RTComm” to be saved on disk and click OK.


14. Now right-click your new library and select New»Variable. This opens the Shared Variable Properties window. Name your variable Data Buffer and select Single Process for the variable type in the Variable Type drop-down box as shown in the image below. Choose Array of Double as the Data Type. This creates a locally scoped variable (no network publishing) that contains an array of double-precision floating-point numbers.

15. Select the RT FIFO option from the menu on the left and click the Enable RT FIFO check box. Then select Single Element for the FIFO Type and enter 2 for the Number of elements (this varies depending on the number of I/O channels monitored). This configures the variable to operate as a global variable that can maintain the timing requirements while serving as a data buffer between the high-priority and low-priority tasks. Click OK.

16. Create another single process shared variable in the library you just created. This variable is for the user interface Stop control you create that stops both loops of the program when directed. This new variable should have all the following settings:

  • Name: Stop
  • Variable Type: Single Process
  • Data Type: Boolean
  • RT FIFO: Enabled and Single Element FIFO Type

17. Once finished, your project should look similar to the image below.

18. Now return to the sequence structure to initialize these variables and an array for the data. Drag an instance of both variables from the project into the structure.

19. Right-click on each variable and select Access Mode»Write.

20. Right-click on each variable’s input terminal and select Create»Constant. Leave the default False and array of zeros values.

21. Place an Initialize in the structure (search the Functions palette or navigate to Programming»Array»Initialize Array). Right-click the element terminal and Create»Constant. Set the value to 0. Do the same for the dimension size input terminal and set the constant value to 2 because you need to write two channels of temperature data to the array. Then wire the initialized array output through the sequence structure to the border of the Timed Loop as shown below.

22. Now insert the mean values into the array. Place two instances of the Insert Into (Programming»Array»Insert Into Array) in the Timed Loop and wire the mean output into the new element terminal as shown below.

Place one Insert Into after the other to build up the array as you wire the output array into the array input. The first AI0 mean data point is placed in index 0 by default. Create a constant of 1 for the index input of the second Insert Into to place the AI1 mean data point in the next index.

23. Drag an instance of both the Data Buffer and Stop variables from the project into the Timed Loop. Wire the output array into the Data Buffer FIFO variable to transfer data to the While Loop. Then wire the Stop variable to the Loop Condition terminal as shown below.

24. Complete the application by creating a user interface to see temperature data and stop the application at run time. Right-click on the front panel to bring up the Controls palette and select Silver»Graph»Waveform Chart. Then select Silver»Boolean»Stop Button. Arrange the user interface as shown below.

25. Now finish wiring the low-priority While Loop. Drag an instance of both the Data Buffer and Stop variables into the loop. Wire the Data Buffer output into the Waveform Chart, and the Stop Button control value into the Stop variable, to ensure both loops stop at the same time. Also wire this stop value to the Loop Condition.

26. Place down a Wait (ms).vi to enforce timing for this task. Right-click on the input terminal, select Create»Constant, and enter a value of 200 to run slower than the high-priority While Loop. Finally, double-click on the Waveform Chart label to select it and rename it something meaningful like Temperature.

27. Your basic acquisition and analysis application is now complete. Once you connect the CompactRIO hardware, you can run the code on the target.


Continue your Evaluation of LabVIEW with the LabVIEW RIO Evaluation Kit

Using the evaluation kit, develop an embedded system with the LabVIEW reconfigurable I/O (RIO) architecture. Use LabVIEW software and C/C++ to program NI RIO hardware, which includes a real-time processor, FPGA, and I/O. CompactRIO uses this same architecture for prototyping through deployment with a flexible array of configuration, expansion, and C Series module I/O options.


Additional Resources

Back to Top

Bookmark & Share


Rate this document

Answered Your Question?
Yes No