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

Implementing Closed-Loop Control

A closed-loop control system is necessary to automate the monitoring and maintaining of a process variable. An example of a closed-loop control system is cruise control, a standard feature on most vehicles on the road. The driver provides the set point, or desired speed, and the control system monitors the actual speed and adjusts the engine throttle accordingly. For example, the logic of the control system dictates how quickly the car gets back up to speed after encountering a hill, and tuning this relationship optimizes engine performance and fuel efficiency. This module illustrates how to implement a PID control algorithm in NI LabVIEW software.


Step-by-Step Procedure

Download the Step-by-Step Procedure PDF

This procedure requires a transducer/sensor to be connected to pins ai0+ and ai0- and an actuator/motor/fan connected to pins ao0+ and AGND. A simple example of this is shown in the video. Another option is a thermostat setup consisting of a thermometer, heating lamp, solid state relay, and fan to cool the system. The control system is used to hold the system at a near steady temperature. Below is a generic setup procedure that you can modify for your control system setup and parameters.

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.

  1. Connect the USB DAQ device to your PC
  2. Connect the sensor being monitored: Analog Input 0 (ai0)
    1. You can locate the device pinouts for a DAQ device by searching for your device online at ni.com
    2. The user guide and specifications contain this diagram
    3. Right-click your device in Measurement & Automation Explorer (MAX) configuration software and select Device Pinouts
    4. Connect the actuator/motor being controlled: Analog Output 0 (ao0)
  3. Place a DAQ Assistant on the block diagram for analog output
    1. Right-click on the block diagram and select Express»Output»DAQ Assistant
    2. Place the DAQ Assistant to the right of the existing DAQ Assistant on the block diagram by left-clicking
  4. Configure DAQ Assistant type
    1. Select Generate Signals»Analog Output»Voltage
    2. Select Dev1 (or the name of your device if not Dev1)
    3. Select ao0 
  5. Configure generation settings
    1. Select 1 Sample (On Demand) for Generation Mode
    2. Enter 5 for the Max Signal Output Range
    3. Enter 0 for the Min Signal Output Range
    4. Select OK to apply these settings
  6. Place a PID Control VI to control the analog output value
    1. Right-click the block diagram and select Control Design & Simulation»PID» PID
    2. Left-click to place the Simulate PID VI in between the DAQ Assistants
  7. Crate the inputs for the PID VI
    1. Right-click the Voltage Output Range input terminal and select Create»Control
    2. Right-click the PID Gains input terminal and select Create»Control
    3. Right-click the Set Point input terminal and select Create»Control
    4. Navigate to the front panel by pressing Ctrl-E
    5. Enter 5 for the Output High
    6. Enter 0 for the Output Low
    7. Enter 1 for Proportional Gain
    8. Enter 0.01 for Integral Time
    9. Enter 0 for Derivative Time

Note: These values vary from system to system and must be tuned accordingly. Use the PID Autotuning VI to have LabVIEW determine the gains for you.

  1. Wire the Data output of the analog input DAQ Assistant to the Process Variable input of the PID VI
  2. Wire the Output of the PID VI to the Data input of the analog output DAQ Assistant
  3. Run the VI to observe the signal being output on the waveform graph
  4. Merge the set point and output from the PID VI to one signal
    1. Right-click the block diagram and select Express»Signal Manipulation»Merge Signals
    2. Wire the Set Point to the top input terminal
    3. Wire the Data output from the Analog Input DAQ Assistant to the bottom terminal
  5. Create a waveform chart to indicate the set point and analog input data
    1. Navigate to the front panel by pressing Ctrl-E
    2. Right-click the front panel and select Express»Graph Indicators»Waveform Chart
    3. Left-click to place the chart
    4. Navigate to the block diagram by pressing Ctrl-E
    5. Ensure the waveform chart terminal is within the While Loop
    6. Wire output of Merge Signals to the input of the waveform chart
  6. Run the VI to observe the output
    1. Tune the PID gains if necessary
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

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.

Don’t have NI-DAQmx installed on your PC?
Download the latest version of NI-DAQmx.

Related Links
Use NI myDAQ to Turn on a Computer Fan Using a Solid State Relay

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