Archived:LabVIEW Statechart Module Tutorial

Updated Oct 12, 2020

National Instruments does not actively maintain this document.

This content provides support for older products and technology, so you may notice outdated links or obsolete information about operating systems or other relevant products.



  • LabVIEW
  • LabVIEW Statechart Module

This NI LabVIEW Statechart Module tutorial is designed for the LabVIEW online evaluation environment, so it does not depend on any hardware. It covers the basics of creating a statechart-based application.

Introduction to Applications Built with Statecharts

With an existing statechart-based application, this section illustrates how you can use statecharts. The application is a ceiling fan that has variable speeds and a light that can be toggled on/off. Additionally, this application makes use of a synchronous statechart architecture. Synchronous architectures periodically read inputs and write to outputs and are not dependent on an external trigger to cause the statechart to evaluate conditions. Synchronous applications are best suited for real-time, field-programmable gate array (FPGA), and embedded applications.

  1. Open Ceiling Fan (sync).lvproj. Expand LV Statechart.lvsc.
  1. Statecharts consist of triggers, inputs, outputs, state data, and a diagram. Open Edit Triggers and Groups. This section is where you can add external triggers that can cause a state transition. Because you are dealing with a synchronous statechart, no triggers have been created. Click OK.

  1. Open Inputs.ctl. Here you can see four different input values – light, fan, switch, and stop buttons.

  1. Open Outputs.ctl. This has two indicators – a light status LED and fan status numeric indicator.

  1. Also included inside a statechart is StateData.ctl. State data is a control that can act as a set of temporary variables that are accessible only within the statechart.

  1. Open This is now the statechart logic for the ceiling fan. Statecharts are primarily made of regions, states, and transitions. A region is a container for one or more states, a state is the condition that the statechart is in, and a transition is a means to move between states.
  2. Double-click the transition that goes between Fan Off and Fan On.

  1. This brings up the Configure Transition dialog window, where the LabVIEW code that defines a transition is located. The tabs Guard and Action define where the LabVIEW code is written. A guard is a conditional that is evaluated to determine whether a transition should be executed, while the action tab specifies a behavior of the system when a transition is taken. Click OK.

  1. Double-click the Fan On state.
  2. This brings up the Configure State dialog for the fan.

  1. States have several types of actions. Entry actions are executed every time the state is entered, and exit actions are executed when the state is exited. You can make additional actions depending on the behavior in the system. Click OK.
  2. Looking at the statechart, the behavior is described as follows: initially, the statechart is in the Switch Off state. Once the switch is turned on, you enter the Switch On superstate. In this state, you contain two orthogonal regions that act independently of each other. Inputs can change the light from the Light Off state to Light On and likewise for the Fan Off and Fan On states. Ultimately, you can terminate the statechart by the Stop input.
  3. Switching back to the Project Explorer window, open the FanLightDemo –
  4. Bring up the block diagram by typing <Ctrl+E>. The statechart is executed by the Run

  1. Switch back to the front panel. Run the VI.

  1. Experiment with the buttons and observe how this impacts the statechart.

Creating a Statechart Application

This section covers developing a statechart and integrating it into your application, which simulates the heating and cooling of a water tank. It consists of a tank being heated to a user-defined temperature, after which the tank is cooled to another user-defined temperature. The system has an emergency switch that stops the process at any point.

First you need to create your project.

  1. From the LabVIEW Getting Started screen, select Empty Project.

  1. Right-click My Computer and select New >> Statechart.

  1. Save the statechart as Temperature Monitor.
  2. You have created the statechart file structure in the Project Explorer window.

You now have created the framework for your statechart. The following section outlines the steps required to build your statechart-based application – inputs, outputs, diagram, code, and integration.

The main statechart creation steps are defining your system inputs, determining the system outputs, creating a diagram representing your statechart, implementing the behavior in LabVIEW code, and then integrating the statechart into your LabVIEW application.

Defining Inputs


During this phase, create the input cluster to your statechart.

  1. From the Project Explorer window, open up Inputs.ctl.

  1. Rename the button as “Stop.”
  2. Add three numeric controls for High TemperatureLow Temperature, and Current Temperature. The completed input cluster should look like the image below.

  1. Open StateData.ctl. You can use this control for temporary values and internal state data. In this example, maintain a copy of the current temperature. Rename the control to Temperature.

Determining Outputs

Now that you have completed the inputs to your system, you can create the output of your statechart.

  1. From the Project Explorer window, open Outputs.ctl.

  1. Rename the numeric indicator as Temperature. The completed output cluster should look like the image below.

Creating Diagrams


Now that the input and outputs are complete, you can create the diagram that represents your statechart.

  1. Open up During this part of the exercise, you use only the Statechart Development palette.

  1. Create a state named Controller. Within this state, create a region called Temperature Control. Additionally, the diagram requires an Initial pseudostate and a transition. The diagram should look like this:

  1. Within the Temperature Control region, add two additional states called Heating and Cooling. These states need to be connected by a transition. Within this region, you need to add initial pseudostate and a transition. The diagram should now look like this:

  1. Outside the controller state, add a Terminal pseudostate. Add a transition from the controller state to the terminal and a transition from the cooling state to the terminal block. The diagram should look like the image below. This is the completed statechart diagram for your application.

Implementing Code

With your diagram complete, you now can implement your LabVIEW code into the statechart.

  1. Right-click the transition entering the Heating State. Select Configure Transition.
  2. In the Configure State dialog, under the Action tab, wire Inputs.Current Temperature to StateData.Temperature.

  1. Right-click the Heating state. Select Configure State.
  2. In the Configure State dialog, create a static reaction by pressing the Create button in the bottom left of the window. Select the newly created static reaction and navigate to the Action Tab. Within the action tab, add the value of 1 to the StateData.Temperature. Wire this value to StateData.Temperature and Output.Temperature. Click OK

  1. Now add code to the transitions between Heating and Cooling. This code determines when the transition between heating and cooling occurs. Right-click the transition and select “Configure Transition…
  2. Select the Guard tab. Change the inputs to be both StateData.Temperature and Inputs.High Temp. Determine whether the state data temperature is greater or equal to high temperature. Wire the output of the comparison to the Execute? terminal. If the current temperature is greater than or equal to the high temperature, it transitions to the cooling state. Click OK

  1. Configure the Cooling state. Right-click the cooling state and select Configure State… Within the Configure State dialog, select StateData.Temperature. Subtract 1 from the value and wire to StateData.Temperature and Output.Temperature terminals.

  1. Configure the transition leaving the cooling state. Right-click the transition and select Configure Transition. In the guard code, determine whether StateData.Temperature is less than or equal to Input.Low Temp. If this is true, the transition should execute. Wire the output of this comparison to the Execute? terminal.

  1. Configure the transition leaving the Controller state and ending at the terminal state. In the guard code, wire Input.Stop to the Execute? terminal.

  1. Now the statechart diagram is complete with the functionality that you need. The final step is to generate the code for the statechart by clicking the Generate LabVIEW Code for this Statechart button.

Integrating the Statechart into Your Application

You have completed your statechart. Next, call and execute your statechart from your LabVIEW application.

  1. From the Project Explorer window, create a new VI.
  1. On the block diagram, from the Statechart >> Statechart Communication palette, select Run
  1. Right-click the Run and select Link to Statechart. Link to the temperature monitor statechart that we just created.
  2. Right-click the input terminal to the Run and create a control.

  1. Switch to the front panel and add a Waveform Chart to the user interface.

  1. Switch back to the block diagram. Right-click the Run output terminal and select Cluster, Class, & Variant Palette >> Unbundle by Name.

  1. Wire the output from Unbundle by Name to the Waveform Chart. Place a timed loop around the code. Wire the Terminated? terminal from the Run to the stop of the while loop.

  1. Right-click the Run and select Configure… From the configure dialog, under Statechart Diagram Display, check the Show Terminal? check box.

  1.  Create an indicator for the Diagram Display terminal of the Run The completed code should look as follows:

  1. Enter values for current temperature, high temperature, and low temperature and run the VI. The completed front panel should look as follows.

More Information and Examples

These are a few bundled examples of applications built with the LabVIEW Statechart Module:

  • Fan Demo (Asynchronous) – The ceiling fan demo using asynchronous architectures.
  • SPI Statechart Demo – SPI protocol developed using statecharts.
  • PackML – Package Machine Language machine model.

For more shipping examples, in LabVIEW, go to the menu option Help>>Find Examples… to open the Example Finder.
Browse under Toolkits and Modules >> Statechart to find additional statechart examples.