How to Tune a PID Controller
2023-09-18 | By ShawnHymel
License: Attribution Drivers / Controllers Arduino
A PID controller is a mechanism used to control a process by combining the proportional, integral, and derivative values of the error between the process’s output and desired setpoint. They are used in a wide variety of industrial and automotive systems. You can read more about the theory behind PID controllers in this tutorial.
This guide will demonstrate how to manually tune a PID controller. If you would like to see an example of manually tuning a PID controller to operate an inverted pendulum, see this video:
Overview of PID Controllers
PID controllers have the following structure:
PID stands for “proportional, integral, derivative,” and they describe how the feedback component of the system is used to calculate an input value for the process (also known as a “plant”). The error term is computed by subtracting the feedback value from the desired setpoint value. The three separate terms are calculated and summed together to produce the controller output:
- Proportional: the error term is multiplied by a constant (Kp)
- Integral: the error term is accumulated over time. That accumulation is multiplied by a constant (Ki)
- Derivative: the slope of the error term is calculated and multiplied by a constant (Kd)
The math equation for a PID controller can be written as follows:
In that equation, u(t) is our input signal sent to the process and e(t) is the error signal as calculated by (output - setpoint), where the output can vary with time. The good news is that we do not need to solve this equation! Various methods exist to help us find values of Kp, Ki, and Kd to create a critically damped system.
PID Controller Tuning Overview
To turn the controller, we must manipulate the Kp, Ki, and Kd values until the controller behaves as desired. In the ideal case, we want the error value to approach 0 quickly with little or no overshoot. This is known as a “critically damped” response. If the error approaches 0 too slowly, this is known as an “overdamped” response, and if the error oscillates around 0, this is known as an “underdamped” response.
There are a number of ways to tune a PID controller. If you have a model of your process (“plant”), you can analytically solve for the best values of Kp, Ki, and Kd. However, this is often not feasible in real-world systems. So, we turn to other methods that involve some amount of trial and error:
- Manual tuning: experienced personnel try different values for the constants to get the system to respond as desired
- Ziegler-Nichols: Tune Kp and analytically solve for Ki and Kd
- Cohen-Coon: Use parameters from an open-loop transfer function experiment
- Åström-Hägglund: Auto-tuning method that is achieved by switching the input between two values, measuring the output, and solving for Kp, Ki, and Kd. Requires the system to oscillate during tuning.
In the rest of this guide, we will demonstrate simple manual tuning. We measure the output against the setpoint as we allow the system to operate from some known value (e.g. the system's output value goes from 0 to setpoint value). We can also introduce a perturbation to introduce a known deviation from the setpoint and measure how the system responds. We graph the system’s output response over time and compare it to the setpoint.
Step 1: Tune the Proportional Constant (Kp)
To begin, we first set Ki and Kd to 0. We then guess at some low value of Kp. Essentially, we are tuning just a P controller at this point. We slowly increase the value of Kp and measure the output of the system (magenta curve) compared to our desired setpoint (blue dashed line). Continue to adjust Kp until the output curve sits below the setpoint without oscillating. We want some steady-state offset, as we will correct it with the integral term.
Kp too small: the steady-state error (distance between steady-state value of the output versus the setpoint) is too large
Kp correct: the steady-state error is minimized with minimal oscillations
Kp too large: large oscillations with overshoot
Step 2: Tune the Integral Constant (Ki)
Once we have a P controller that offers a small amount of steady-state error with little to no oscillation, we can start increasing the Ki value to provide some amount of the integral term into our calculation. The integral term is intended to compensate for steady-state error over time.
A good Ki value will allow a fast rise in the output with a small amount of overshoot. If your system becomes unstable and oscillates out of control, your Ki value is too high.
Ki too small: the steady-state error is still present, which means your system will not reach the setpoint (or take too long to reach it).
Ki correct: steady-state error is removed with little or no oscillation. Some overshoot is acceptable here.
Ki too large: the system oscillates unacceptably or out of control
Step 3: Tune the Derivative Constant (Kd)
In many cases, you do not need to tune Kd; a PI controller is sufficient, especially if you are OK with some overshoot. However, if overshoot is unacceptable, then you should start increasing the Kd value to achieve a critically damped system. Such a system should approach the setpoint quickly with no overshoot and little to no oscillation.
Kd too small: overshoot is still present in the initial approach to the setpoint
Kd correct: response curve quickly approaches the setpoint with no overshoot and settles with little to no oscillation
Kd too large: the system oscillates at or near the setpoint
PID Controller Code
Many older PID controllers were pneumatic, mechanical, or built from analog electronics. Tuning them was a manual and painstaking process. Many modern PID controllers often rely on code running in microcontrollers or small computers. To help you get started, here is some pseudocode (Python-esque):
k_p = 1 # Tune this
k_i = 0 # Tune this
k_d = 0 # Tune this
interval = 0.001 # e.g. 1 ms
# Loop forever
error_prev = 0
integral = 0
while True:
# Get value from sensor (feedback)
val = read_from_sensor()
# Calculate the PID terms
error = setpoint – val
integral = integral + (error * interval)
derivative = (error – error_prev) / interval
output = (k_p * error) + (k_i * integral) + (k_d * derivative)
# Save value for next iteration
error_prev = error
# Wait for interval time
sleep(interval)
If you would like to try tuning a controller for an inverted pendulum, you can use the code here as a starting point. This code was used in the video shown above and operates the inverted pendulum kit from STMicroelectronics (STEVAL-EDUKIT01). The starting guide for the STEVAL-EDUKIT01 can be found here, and the educational materials (e.g. lab guide) can be found here.
Going Further
We hope this helps you get started tuning your own PID controllers! If you would like to dig deeper into PID controller theory and tuning, we recommend checking out the following guides:
- How to Control Output Voltage using a PID
- Practical PID tuning guide
- What is PID Tuning?
- Ziegler–Nichols method
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum