Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Integrating Text-Based Programming

Certain tasks arise in NI LabVIEW software, such as scaling signals and solving mathematical equations, during which it can be beneficial to implement text-based programming in LabVIEW. LabVIEW provides the option to use C++ syntax with the Formula Node and .m file script syntax with the MathScript Node for signal processing, analysis, and math functions. This module explains how to get started with both.


Step-by-Step Procedure

Download the Step-by-Step Procedure PDF


To illustrate the needs for text-based integration in LabVIEW, consider an equation used to convert the resistance of a thermistor to the appropriate temperature in degrees Celsius.


Figure 1. Formula Used to Convert Resistance of a Thermistor to Temperature

You can see that this is a complex polynomial equation that requires many mathematical operations. While possible, this is something that can quickly become cluttered in LabVIEW and difficult to follow as seen in Figure 2:

Figure 2. Native LabVIEW Functions for Thermistor Formula

Figure 3 is an example of how the formula could be represented in a traditional text-based language such as C. This is a lot easier to understand, and it resembles closely the actual equation. Now look at how to implement this using the Formula Node and MathScript Node.


Figure 3. Text-Based Code Representation of Thermistor Formula

LabVIEW Formula Node

Before starting this procedure, you need to complete the exercise and use the solution in the Taking a Measurement module. You can also create a numeric control to simulate the data acquisition input.

Note: If you are not using a DAQ Assistant to acquire resistance data and convert to temperature, skip ahead to step 2 and use a numeric control in place of the DAQ Assistant.

  1. Configure the voltage input DAQ Assistant to the input resistance from the digital multimeter (DMM) terminals
    1. Double-click the existing DAQ Assistant to edit the configuration
    2. Right-click the Voltage channel in the Channel Settings list box and select Remove from Task
    3. Click the blue plus (+) sign to add a channel to the task and select Resistance
    4. Select Dev1 (NI myDAQ)»dmm
    5. Ensure Iex Source is set to Internal
    6. Ensure Acquisition Mode is set to 1 Sample (On Demand)
    7. Press OK to apply settings
  2. Place a Formula Node on the block diagram to convert resistance to degrees Celsius
    1. Right-click the block diagram and select Mathematics»Scripts & Formulas»Formula Node
    2. Paste the following equation into the Formula Node window and resize accordingly
  3. float64 M;
    M=ln(R/10000);
    T=(1/(A+B*M+C*M*M+D*M*M*M))-273.15;

  4. Add inputs to the Formula Node for all input variables in the equation
    1. Right-click the left border and select Add Input
    2. Enter R0 to relate the input to the R0 variable in the equation
    3. Repeat this procedure for A, B, C, and D
    4. Right-click inputs A, B, C, D, and R0 and select Create»Control
    5. A=0.0039083; B=5.775E-7; C=1.63214E-6; D=7.1922E-8; R0=1000
    6. Wire the Data output from the DAQ Assistant to the R input of the Formula Node
  5. Add output for Temperature (C) to display on the chart indicator
    1. Right-click the border and select Add Output
    2. Enter T to relate the output to the T variable in the equation
    3. Wire this output to the input of the graph indicator
  6. Add a While Loop to run the VI continuously
    1. Right-click the block diagram and select Programming»Structures»While Loop
    2. Left-click and drag to encompass all of the code
    3. Right-click the conditional terminal and select Create»Control
  7. Create the stop condition to include when the Stop button is pressed or an error is detected
    1. Unwire the Conditional Terminal from the Stopped output terminal of the analog input DAQ Assistant
    2. Right-click the block diagram and select Programming»Boolean»Or
    3. Wire the output from the Stop button control to the bottom input of the Or Boolean function
    4. Right-click the block diagram and select Programming»Cluster, Class, & Variant»Unbundle by Name and place this to the right of the analog input DAQ Assistant
    5. Wire the Error Out output terminal of the analog input DAQ Assistant to the input of the Unbundle by Name function and ensure Status is selected to be unbundled
    6. Wire the output of the Unbundle by Name function to the top input of the Or function
    7. Wire the output of the Or function to the input of the Conditional Terminal
  8. Run the VI and observe the result

Get more help with the Formula Node and obtain a list of Formula Node Functions.

MathScript Node

You can also use the MathScript Node to run preexisting .m file scripts as well as develop .m files in LabVIEW for signal processing.

Before starting this procedure, you need to complete the exercise and use the solution in the Taking a Measurement module. You can also create a numeric control to simulate the data acquisition input.

Note: If you are not using a DAQ Assistant to acquire resistance data and convert to temperature, skip ahead to step 2 and use a numeric control in place of the DAQ assistant.

  1. Configure the voltage input DAQ Assistant to the input resistance from the DMM terminals
    1. Double-click the existing DAQ Assistant to edit the configuration
    2. Right-click the Voltage channel in the Channel Settings list box and select Remove from Task
    3. Click the blue plus (+) sign to add a channel to the task and select Resistance
    4. Select Dev1 (NI myDAQ)»dmm
    5. Ensure Iex Source is set to Internal
    6. Ensure Acquisition Mode is set to 1 Sample (On Demand)
    7. Press OK to apply settings
  2. Place a MathScript Node on the block diagram to convert resistance to degrees Celsius
    1. Right-click the block diagram and select Mathematics»Scripts & Formulas»MathScript Node
    2. Paste the following script into the MathScript Node window and resize accordingly

double M;
M=log(R/10000);
T=(1/(A+B*M*M+C*M*M+D*M*M*M));
T=T-273.15;

  1. Add inputs to the Formula Node for all input variables in the equation
    1. Right-click the left border and select Add Input
    2. Enter R to relate the input to the R variable in the equation
    3. Wire the Data output from the DAQ Assistant to the R input of the MathScript Node
    4. Repeat for variables A, B, C, and D
    5. Right-click inputs A, B, C, D, and R0 and select Create»Control
    6. A=0.0039083; B=5.775E-7; C=1.63214E-6; D=7.1922E-8; R0=1000
    7. Wire the Data output from the DAQ Assistant to the R input of the MathScript Node
  2. Add output for Temperature (C) to display on the chart indicator
    1. Right-click the border and select Add Output
    2. Enter T to relate the output to the T variable in the equation
    3. Wire this output to the input of the graph indicator
  3. Add a While Loop to run the VI continuously
    1. Right-click the block diagram and select Programming»Structures»While Loop
    2. Left-click and drag to encompass all of the code
    3. Right-click the conditional terminal and select Create»Control
  4. Create the stop condition to include when the Stop button is pressed or an error is detected
    1. Unwire the Conditional Terminal from the Stopped output terminal of the analog input DAQ Assistant
    2. Right-click the block diagram and select Programming»Boolean»Or
    3. Wire the output from the Stop button control to the bottom input of the Or Boolean function
    4. Right-click the block diagram and select Programming»Cluster, Class, & Variant»Unbundle by Name and place this to the right of the analog input DAQ Assistant
    5. Wire the Error Out output terminal of the analog input DAQ Assistant to the input of the Unbundle by Name function and ensure Status is selected to be unbundled
    6. Wire the output of the Unbundle by Name function to the top input of the Or function
    7. Wire the output of the Or function to the input of the Conditional Terminal
  5. Run the VI and observe the result

Get more help with the LabVIEW MathScript RT Module.

You can convert your DAQ Assistant Express VIs into low-level NI-DAQmx functions by right-clicking the DAQ Assistant and selecting Generate NI-DAQmx Code. The low-level NI-DAQmx API exposes more functionality and customization options for programming.

 

LabVIEW Example Code

Native LabVIEW Functions Snippet

Formula Node Snippet

MathScript Node Snippet

Right-click on the above VI snippet and select Save Image As... Locate the file on your hard disk and click and drag the file icon onto your LabVIEW block diagram. LabVIEW automatically generates the code from the VI snippet. For more information, read VI Snippets.


Module Exercise: Integrating Text-Based Programming

Download the Module Exercise PDF

Exercise Goal
With the LabVIEW Example Codes above, create an overall solution that you can use to select one of the three methods of conversion from resistance to temperature when acquiring the resistance of a thermistor using the NI myDAQ device. Before starting this exercise, you need to complete the exercise and use the solution in the Taking a Measurement module.

Required Hardware
1- NI myDAQ or NI ELVIS II
1- 10 kΩ thermistor
or
Simulate the resistance input with a numeric control

Things to Consider

  • What is the execution structure used to run a piece of code based on a condition?
  • What type of control could be used to determine the case if there are more than two cases in the Case structure? Would a string allow for this? Would a numeric allow for this? Is there a combination of both?
  • Does the temperature increase when the resistance increases or decreases? Try placing a numeric indicator for the resistance value to determine this.
  • Use alligator clips with your DMM wires to help affix the thermistor.

View the exercise solution
(Don’t cheat!).

Cumulative Exam:
LabVIEW and DAQ

Evaluate your understanding of LabVIEW and the basics of DAQ. This exam is recommended after you complete all of the modules for LabVIEW Basic Concepts and LabVIEW Basic Tasks. There are 26 multiple choice questions on the exam.

Take the exam


Cumulative Project:
LabVIEW and DAQ

Assess your working knowledge of building a complete system by acquiring, processing, and displaying data with LabVIEW and a DAQ device. This project is recommended after you complete all of the modules for LabVIEW Basic Concepts and LabVIEW Basic Tasks.

Start the project