Proportional Integral Derivative (PID) Loop Control and Tuning

What is a PID loop, what do you use it for, and how do you tune it for best efficiency?

 

Download this technical note as a pdf

In this article

​This article defines the elements of a PID loop, reviews how PIDs are configured for Opto 22 devices, and introduces a free online tuner you can use for many loop controllers (not just Opto's).

In this article we give you background information on PIDs but don't discuss details of the theory or methods for tuning your loop. That's all available online. (For example, you can search on Ziegler-Nichols PID tuning to find information from reliable sources.)

What is a PID control loop?

A PID control loop is a mathematical formula that monitors a process variable (typically a sensor input), compares it to a setpoint, and calculates an output to keep the variable as close as possible to the setpoint.

The sensor input is scanned at a set rate (for example, once per second). At each scan the loop controller compares the input to the setpoint and notes the error (the difference between them). Then it plugs the error into a mathematical formula with the proportional, integral, and derivative values, and calculates an output to put the process back on track. The loop repeats at the next scan.

It looks like this:

Two common examples of a PID control loop are a car’s cruise control and a room thermostat. 

Cruise control—When you click the Set button on the car’s cruise control, you're setting the current vehicle speed to be the setpoint. The car’s PID loop controller measures the vehicle road speed and sees how quickly it is approaching or departing from that setpoint. Then it calculates the acceleration required to maintain this speed, and sets the accelerator pedal output. 

Room thermostat—For a room thermostat, the loop controller measures the current temperature and compares it to the desired setpoint. Depending on the sign of the error, the output activates either heating or cooling.


Cruise control and room temperature control are two examples of PID loops.

   

PID Terminology

Before we dive into tuning the loop, let’s review a few of its key terms.

Proportional

The proportional component of the PID calculation (sometimes called gain or error term) looks only at the difference between the setpoint and the process variable (input). The output signal is a proportional response to this error. Depending on the formula used in the PID controller, increasing the proportional value speeds up the response of the PID loop. Excessive values put the process into oscillation; even larger values could cause the system to oscillate out of control.

Integral

The integral component sums the output error over time and is linked to the scan rate of the PID loop. The integral value's function is to reduce the steady state error to zero. An effect called integral windup can occur when the value of the integral saturates the PID controller, meaning the controller output reaches its maximum or minimum limit, and the error signal still cannot be driven to zero.

Some PID formulas use an inverted integral. 

Derivative

The derivative component looks at the rate of change in the output and reduces that rate of change to prevent output overshoot and decrease settling time if the input (process variable) moves too fast. Increasing this value causes the output to react in very small increments; if increments are too small, the derivative can cause the loop to become sensitive to noise. Often loops can be tuned satisfactorily with no derivative at all. 

Direction of output 

Depending on the nature of the process, the direction of output may be the same as or opposite to the direction of the input. You’ll need to make sure the output direction is correct for your application.

  • In some applications the direction of output is the same as the direction of input (increment/increment, or decrement/decrement).
  • In others, the direction of output is the opposite of the direction of input (increment/decrement, or vice versa). This is called a reverse acting PID control loop.

For our car cruise control, the direction of output is opposite. We need to make sure that when the vehicle approaches a hill and its speed drops below the setpoint, the PID loop directs the accelerator to apply more power to the engine. In this case, a decrement in the input (road speed) causes an increment to the output (accelerator).

For our heating/cooling system, it depends. The thermostat provides the input as temperature. There are two PID loop controllers, one for the heating valve and one for the cooling valve. Each has a different setpoint, to ensure that you do not have both valves open at the same time. For heating, an increase in the room temperature means the PID needs to close the heating valve. Less heating water flows through the valve, so the room cools down. This is an opposite direction of output. But for cooling, the same increase in room temperature needs to cause an increase in the cooling water valve; this is the same direction of output. 

PID controllers have different ways of changing the direction of output. Check your PID loop manual or formula to determine the correct way to set the output direction. 

Loop dead time

The amount of time it takes the process to start reacting to a change in the input is called the loop dead time. This time delay is a combination of factors, starting with the process itself, the location of the sensors, and any filtering (hardware or software) that may be installed. 

For example, moving the temperature sensor physically closer to the output actuator may reduce the loop dead time, but you must take care that the sensor still measures the process accurately. You have to find a balance between the loop dead time and accuracy.

Scan rate and scan time

  • How often the PID calculation is performed—how many times per second—is called the scan rate
  • The amount of time between scans is called the scan time (or scan interval). For example, a scan rate of 4 times per second equals a scan time of 0.25 seconds. 

Scan rate is a very important aspect of configuring the PID loop. If the calculation is done too seldom, you won’t be able to control the process; if too often, the loop might be hard to tune.  When setting the scan rate, at all times keep in mind the speed limits, if any, of your input and output devices, both sensors and actuators. There’s no point in scanning faster than they can update.

Since the PID calculation is only performed at the scan time (scan interval), be aware that the output will remain static until the next scan. This is why it’s important to correctly set it, so the output will react to the input in a timely manner. 

   

Generally, set the PID scan rate so that the loop scans between four and 10 times during the loop dead time interval. The shorter the loop dead time, the shorter your scan time should be.

For example, if the loop dead time is 3 seconds, a possible PID scan time could be somewhere between 0.75 seconds and 0.3 seconds:

  • 3 seconds/4 = 0.75 s.
  • 3 seconds/10 = 0.3 s.


The scan rate (scan time) determines how often the PID calculation is performed.

Tuning a PID loop

To be most effective, each PID loop must be tuned. Tuning makes sure that the controlled output moves the process input to the desired setpoint as quickly as possible with minimum overshoot, and then holds that setpoint steady with minimal changes in the output.

In the case of the car’s cruise control, we want to make sure the speed doesn't vary too much, because if it does, people riding in the car could get motion sickness as it constantly speeds up and slows down. In the case of the room thermostat, we don't want the room temperature to swing around the setpoint and leave people feeling alternately too cold or too hot. Tuning solves these problems.

PID tuning methods

 

P and PI tuning

P tuning—It is possible to “tune” a process using just the proportional value. The problem is that there will be a steady state offset between the process variable and the setpoint. The process will simply never reach the setpoint.

PI tuning—Some PID loop controllers offer only PI (proportional and integral) tuning. The addition of the integral value removes the steady state offset and ensures that the process settles on the setpoint (once the controller is tuned correctly).

PID tuning

Complete PID tuning is the best way and the usual offering of most PID controllers. The addition of the derivative ensures that the loop is not too reactive during any sudden process or setpoint changes.

There are several methods for complete PID tuning. Arguably the most popular are trial and error, reaction curve closed loop (sometimes called infinite oscillation), and reaction curve open loop (sometimes called step response).

Trial and error—For obvious reasons we don’t recommend this method. Your time is too valuable for that!

Reaction curve closed-loop method—This method is a possibility, but it requires putting the process into a state of oscillation. If not done with a great deal of care, it can easily result in the process being damaged by the oscillations. 

Reaction curve open-loop method—The advantage with this method is that it can be trial-run several times, starting with very small steps in output, thus keeping the process under control while observing the changes in the steps. For most processes, this is a safer method of determining the required tuning parameters.

Using the free online tuner

Whether you use Opto 22 controllers or a different PID loop controller, you can use our free online graphical PID tuner. It's a huge help if you use PID control loops only now and then, or if you are looking for a less-math-more-visual way to tune your PID loop. It uses the reaction curve open-loop method, specifically the Ziegler-Nichols Reaction Curve (Ziegler-Nichols Open-Loop) tuning method and is optimized for Opto 22’s Velocity Type C and similar algorithms. 

NOTE: This method works well to tune most temperature and some pressure processes. It isn't as useful on slower flow or very dynamic processes, for example a process where the dead loop and response curve are similar in length. Likewise it's less effective if you have a very long loop dead time, for example HVAC in a large conference room or hall. 

The online tuner calculates the values for PID control loop elements (P, I, D, and scan time) based on data you enter. It’s an easy way to get very close to functional tuning for your real-world process PID, giving you a good baseline for your PID parameters.

Here are a few requirements for using the online tuner:

  • The process response time for your control loop should be around twice the loop dead time.
  • The process should self-stabilize for a given step change in the controlled variable (the output).
  • Your PID loop controller must be able to graph the PID loop (see image below).
  • You'll need a metric ruler to take precise decimal measurements and enter them into the online tuner.
  • You should have access to either a printer or screen capture software so you can take graph measurements at the end.
  • Depending on your PID loop controller formula, you may need to invert the integral value that is calculated for you. Check your formula or manual for that information.

Your PID loop controller must be able to produce a graph like this one.
 
   

Steps to use the online tuner

Start by opening the free online PID tuner

Free online PID tuner

In the tuner

The buttons at the top of the tuner explain PID loops and walk you through the process (the first three are explained in more detail in this article):

Tuning procedure

If you already have some rough tuning values for your PID loop, use them. If not, don’t worry; we start with the loop in manual. In either case the procedure is the same: we step the process from one steady state to another steady state, and capture the curve of the process in a graph that can be printed out on paper.

Use Step 1 in the tuner as a guide for the response curve graph that you need. If the curve of the process overruns the time scale of your graph the first time, reset the process, extend the time scale, and step through the process again. Or if the process is very responsive and the resulting curve is too compact to measure, reset the process and speed up the graph.

You may need to take a few trial runs at generating a nice clean curve on paper, but as you gain experience in using this tuning method, you’ll be faster at getting it right the first time. 

Follow the steps

Step 1: Determine high and low values for the control variable (PID output).

Put the PID loop into manual and set the control variable (CV) PID output to a nonzero value. You may need to test this a few times to see what a good low output value is. Try to use a value of at least 10%. For example, with HVAC try a value around 20%. 

Wait for the process to settle into a steady state. Then step the output to a higher value, typically 20–40% higher than the minimum. Allow the process to stabilize again. Use the image in the tuner as a guide of how the curve should look, and print the resulting graph.

In the tuner, enter the control variable higher (CV high) and lower (CV low) values—that is, the output value that you stepped to and the value you stepped from. 

Step 2: Enter high and low values of your process variable (PID input).

In the tuner, enter the process variable high (PV high) and low (PV low) values for your PID input.

Step 3: Enter distance between steady states.

On your printout, measure the distance between the lower steady state of the control variable and the upper steady state (typically it’s easier to use millimeters rather than decimal inches as the distance units). 

In the tuner, enter the distance between the two states. The amount of change in the control variable as a result of the step change is used to calculate the P term. 

Step 4: Calculate the 63.2% point on the curve. 

In the tuner, press the Calculate 63.2% change button. The tuner calculates the change and gives you the answer in a height from the lower steady state. Measure this height on your printout and mark the point on the curve. Drop a vertical line down from this point on the curve. 

Step 5: Enter distance for the change. 

Measure on your printout how long it took the process to change from its lower steady state to the 63.2% point on the curve. In the tuner, enter the distance you measured. 

This distance will be converted to time in the next step so that we can determine the loop time constant. This is the amount of time that it took the input to move from its lower steady state to the 63.2% point of the input curve. This time tells us how dynamic the process is and is an important part of the Integral value calculation.

Step 6: Enter the time scale values. 

On your graph, measure the distance between two known points of time. In the tuner, enter the Time scale in time units (the scale you used, in seconds) and the Time scale on paper (the distance you measured, in millimeters). 

Step 7: Determine the loop dead time. 

On your graph, measure the distance between the step change in the output and the beginning of change in the input. In the tuner, enter this process loop Dead time, which is the amount of time it takes the process to begin to react to a change. 

Dead time is an important part of understanding how frequently you should set the PID controller to perform its loop calculation. The online tuner calculates a PID scan rate that sets the loop to scan four times during the loop dead time interval you enter.

Step 8: Get the final calculations. 

The tuner now has all the data it needs to calculate the P, I and D values for you and to suggest a scan time. Press the Calculate tuning button and write down the results. This provides a starting point for tuning your PID loop.

If you want to understand the process that the tuner uses to compute those numbers, check the Show calculation box.

Here's an example of the result:
Example: results of tuning a PID loop with the free online tuner

 

For each PID loop, we suggest using the free online tuner to get a rough baseline for P, I, D, and scan time.

Need help using the PID online tuner? Contact Opto 22 Product Support or talk to an engineer—it's free!

   

Configuring PIDs on Opto 22 devices

Several Opto 22 devices offer built-in PID loop control. For the latest groov EPIC edge controllers and our older SNAP-PAC-R1 rack-mounted controllers, you can configure up to 96 PID loops per device; for groov RIO universal edge I/O, up to 4. These PIDs are individually configured (including a choice of algorithm: Velocity, ISA, Parallel, or Interacting) and individually tuned. 

If you want, you can cascade PIDs by simply using the output channel of one PID loop as the input channel for another.

Once you have your baseline values from the free online tuner, plug those values into your PID loop configuration and fine-tune as necessary. Here are some general instructions; see details below for your device:

  • For all Opto 22 devices running a control program, configure PIDs in that program—PAC Control™, CODESYS®:, or a custom program through secure shell access (SSH). Configurations or changes are written to the I/O when the control program is downloaded and run. PAC Control includes a graphical tuner; in CODESYS, use PID function blocks.
  • For Opto 22 groov RIO units that are not running a control program, configure PIDs in groov Manage when you configure I/O. For SNAP PACs not running a control program, configure PIDs in PAC Manager. With either groov Manage or PAC Manager, remember to save configurations to flash memory.

NOTE: In all Opto 22 devices, the derivative is applied only to the process variable (the input) and not to the setpoint. This means you can change the setpoint without causing spikes in the derivative term. These PIDs also prevent integral windup by back calculating the integral without the derivative term. The feed forward term (bias) is added before output clamping and has a tuning factor.

For both groov and SNAP PAC products, you’re using reliable Opto 22 I/O for connections to field devices. Analog and discrete I/O modules come in a wide variety of signal types, with I/O channels individually configurable for different signals. Each module is tested—twice—before it leaves our factory and headquarters in Temecula, California. Most I/O modules are guaranteed for life

And product support on all Opto 22 products is free and provided by experienced engineers in our California headquarters.

groov EPICgroov EPIC 

For groov EPIC, configure and tune up to 96 PIDs in your control program (PAC Control, CODESYS, or a custom program using SSH). Each PID operates at the I/O level, independently of the controller. Once configured and initialized, a PID runs until the I/O unit loses power, even if the control program running on the processor stops. 

If you're using CODESYS or SSH:

  • If you don't already know the needed PID parameters, use the free online tuner to get started. Then configure and tune them in your control program.
  • Don't configure PIDs in your control program and also configure or change them in groov Manage. The groov Manage configurations will be overwritten or lost when you download, stop, or restart your program. 
  • CODESYS and custom control programs through SSH use OptoMMP memory map addresses to access PID parameters and data.

If you're using PAC Control:

  • If you don't already have an idea of the needed PID parameters, use the free online tuner to get started. Then configure and tune them in PAC Control, which includes a graphical tuner.
  • Don't configure PIDs in PAC Control and also configure or change them in groov Manage. The groov Manage configurations will be overwritten or lost when you download, stop, or restart your strategy. 
  • You'll find our free online PID training classes a good resource. Part of our OptoU series of free online training, these classes show you step-by-step how to configure and tune PIDs in PAC Control.

Reference guides for groov EPIC:

groov RIO

On groov RIO Universal I/O modules not running a control program, you can still use the free online tuner to get started. Then use groov Manage to configure and tune up to four PID loops when you're configuring I/O. See the PID chapter in the groov RIO Universal I/O User's Guide for steps, and remember to save your configuration to flash memory.

Choose from any of the five analog input types (V, mA, ICTD, TC/mV, thermistor/resistor) and two analog output types (V, mA) to design your PID loop. These loops run locally and independently on groov RIO—no PLC, PAC, or PC required.

Unlike standard loop controllers, with groov RIO you can analyze, combine, and share PID-related process data using other embedded tools, like Node-RED and MQTT/Sparkplug B

Reference guide for groov RIO: groov RIO Universal I/O User's Guide (form 2324)

SNAP PAC

For SNAP PAC R-series controllers, you can also get started with the free online tuner. Then configure and tune up to 96 PIDs in PAC Control, which includes a graphical tuner.

  • Configurations and changes are written to the I/O unit when the strategy is downloaded and run. If you later download a different strategy to the control engine, you’ll receive an error message (-700) reminding you that a PID loop is still running and that it may conflict with the new strategy. 
  • Make sure you don't configure PIDs in PAC Control and also configure or change them in PAC Manager. The PAC Manager configurations will be overwritten or lost when you download, stop, or restart your strategy.

The PID operates at the I/O unit, independently of the controller. Once configured and initialized, a PID runs until the I/O unit loses power, even if the control program running on the controller stops. 

You'll find our free online PID training classes a good resource. Part of our OptoU series of free online training, these classes show you step-by-step how to configure and tune PIDs in PAC Control.

NOTE: If you are not using PAC Control with your SNAP PAC, you can tune PIDs in PAC Manager or by using the OptoMMP Protocol. Be sure you save PID configuration to flash memory following instructions for the I/O unit.

Reference guides for SNAP PAC:

   

More resources for PIDs

Learn more about groov EPIC edge controllers.
Learn more about groov RIO edge I/O.

Get groov EPIC Training: free online training or hands-on, instructor-led premium factory training.

Explore your options. Talk to an Opto 22 engineer.

   


About Opto 22 products

Opto 22 industrial automation products include the latest groov EPIC edge programmable industrial controllers and groov RIO edge I/O, as well as field-proven legacy products like SNAP PAC and solid state relays (SSRs).

With groov products, you can collect, process, view, and exchange data where it's produced—at the edge of the network—and securely share data among databases, cloud platforms, web services, PLC systems, and other systems and equipment. groov products give you built-in cybersecurity and multiple control programming options including CODESYS, plus Node-RED, Inductive Automation's Ignition Edge, and MQTT communications.

Opto 22 products are ideal for OEMs and machine designers, process control, machine monitoring, power and energy monitoring, predictive maintenance, remote installations like water-wastewater and oil & gas, SCADA, and industrial internet of things (IIoT) applications. 

Join us at industry events.


Built for the future, with experience from the past

For over 50 years customers like you have counted on Opto 22's reliable, open standards-based products for industrial automation, remote monitoring, and data acquisition. We offer a unique product philosophy: Here Today, Here Tomorrow. That means:

  • We design new products for the long term, with features you’ll need tomorrow.
  • We provide upgrade paths, so your existing systems will work with new ones. 
  • We keep building older products as long as we can find required components. 
  • And we support all our products for FREE as long as you have them.
Learn more about Opto 22
See what customers say about Opto 22
   
   
   

Questions? Contact an Opto 22 engineer. 

  • No bots, just real people
  • ​Call us at 800-321-0PTO
  • Chat now or complete a contact form

 

Order your groov products now!