Configure Loop Timing in LabVIEW

Updated Dec 31, 2020



  • LabVIEW

Normally when a loop, such as a while loop, finishes executing one iteration, it immediately begins running the next. However, it is often beneficial to control how often a loop executes, or its frequency. If you wanted to acquire data in a loop, you would need a method to control the frequency of the data acquisition.
Timing a loop also allows the processor time to complete other tasks such as updating and responding to the user interface. In the following figures, the processor usage for a simple VI with a while loop running untimed and timed are shown. Timing a loop can drastically increase performance.
Untimed Loop                    Timed Loop Executing at 1000 Times a Second

You can control loop time in LabVIEW using Wait functions These functions control how long to wait before performing the next iteration of the loop. There are two basic wait functions in LabVIEW: Wait (ms) and Wait Until Next ms Multiple.
The Wait (ms) function forces the loop to wait for a user-specified amount of time, in milliseconds, before running the next iteration.
The Wait Until Next ms Multiple function watches the millisecond counter and waits for it to reach a multiple of the user-specified time, in milliseconds, before running the next iteration of the loop. You can use this VI to synchronize different activities. For example, you can configure multiple loops to execute at each multiple of 200 ms.

This tutorial explains how to implement timing structures in LabVIEW and helps you visualize how loop time affects number of iterations of a loop. Before completing this tutorial, it may be helpful to review information on LabVIEW For Loops and While Loops. This is a tutorial is suited for LabVIEW beginners. If you are looking for more resources on LabVIEW basics, look into the Introduction to LabVIEW getting started material.

Configure Front Panel Objects

  1. Launch LabVIEW and open a new VI from File >> New VI.
  2. Place a knob numeric control on the front panel by right-clicking on the front panel and navigating to Controls»Modern»Numeric»Knob.
    • We will later use this knob to set the wait time of the while loop.
  3. Double-click the name of the knob and change its name to Wait Time (ms).
  4. Change the knob’s limits to 1 and 1000 by double-clicking on the knob’s current limits and entering the new values. You will use the knob to control a while loop’s wait time.
    • You can also adjust the limits of the numeric control by right-clicking on the knob >> select Properties >> Scale tab >> set the Scale Range Minimum and Maximum.
  5. Place a numeric indicator on the front panel by right-clicking on the front panel and navigating to Controls»Modern»Numeric»Numeric Indicator.
    • This indicator will display the while loop iterations. 
  6. Double-click the name of the numeric indicator and change its name to Iteration.
  7. Change the Representation of the numeric indicator to I-32 (long integer) by right-clicking on the indicator and selecting Representation >> I32.
  8. Place a Stop Boolean control on the front panel. You can find this at Controls»Modern»Boolean»Stop Button.
    • We will use this Stop button to stop the while loop.
    • When complete, the front panel will resemble the image below.

Configure the Block Diagram

  1. View the block diagram by selecting Window»Show Block Diagram or pressing <ctr-E>.
  2. Add a Wait (ms) function by right-clicking on the block diagram and navigating to Functions»Programming»Timing.
  3. Wire the knob control Wait Time (ms) to the input of the Wait (ms) function.
    • The knob value specifies how long, in milliseconds, the loop waits before running the next iteration.
  4. On the block diagram, add a while loop around the front panel object icons.
    1. Right-click on the block diagram and navigate to Functions»Programming»Structures»While loop.
    2. After selecting the while loop, left-click and drag the loop around the icons.
      • If you miss an icon, you can add it into the while loop by clicking and dragging it into the loop.
  5. Wire the Stop control to the while loop’s stop conditional terminal. 
  6. Wire the numeric indicator to the while loop iteration terminal.

Test Loop Timing

  1. View the block diagram by selecting Window»Show Front Panel or pressing <ctr-E>.
  2. Move the knob value to around 500 ms.
  3. Run the VI.
    • Note that because the wait time is set to ~500 ms, the iterations indicator updates every half-second.
  4. As the VI runs, change the knob value by clicking and dragging the knob.
    • Note that as the speed of the loop increases, the iteration speed changes accordingly.

  5. Stop the VI using the Stop Boolean control.