Introduction to PID Controllers
2023-08-21 | By ShawnHymel
License: Attribution Drivers / Controllers Microcontrollers
PID controllers are mechanisms that operate on feedback to control a system, process, or plant. They work by measuring this feedback over time and adjusting the process’s input as needed to achieve a desired setpoint. They are extremely popular in industrial settings used to control, among other things: heaters, boilers, blowers, and robotic arms. You can also find them in everyday applications, such as cruise control systems in cars.
In the rest of this post, we will cover the basic theory behind how PID controllers operate. You can also view this theory in video form here:
Basic Control Theory
We will use a generic “cruise control” example for this description. A cruise control system attempts to maintain a vehicle’s speed without a user’s input (e.g. a driver operating the gas pedal).
The car’s engine is our “process.” In control theory, you will often see a process or piece of equipment referred to as a “plant.” This term comes from the industrial revolution in the late 1700s and describes something that has been “planted in the ground” for industrial purposes, such as machinery or tools.
Without cruise control, the user (or driver) must manually operate the accelerator (also known as a gas pedal). By pressing the gas pedal, the engine lets in more fuel and air, which generates more power to the wheels. This ultimately provides more acceleration and speed.
We want to design a system that can operate without user intervention. For our example, we’ll say that we want to automatically depress the accelerator by some amount (between 0 and 10 cm) to achieve a constant speed of 100 km/h.
Our first, naive pass at such a controller might be something like this:
We simply linearly map the desired speed to an accelerator position. For the sake of argument, we’ll say that when we depress the accelerator by 5 cm, we can get up to and maintain a speed of 100 km/h on a flat road. So, we simply multiply the set point by (1/20) to get 100 / 20 = 5.
That works, but only under very ideal conditions. If we go up or down a hill, our speed will change, and our controller has no way of knowing that something has changed. Additionally, if you’ve ever driven a car, you’ll know that you probably want to depress the accelerator past the desired set point to accelerate up to your speed. Then, you let off the pedal some to maintain the speed.
A system with no feedback as described above is known as an open-loop control system.
To help the controller understand the actual outcome, we introduce a feedback path.
The feedback mechanism produces a closed-loop control system. Usually, this feedback is provided by a sensor. In our case, a speed sensor attached to the wheels or drive shaft gives us the actual (or close enough) speed of the vehicle. The error is calculated as the difference between the set point and the feedback measurement:
error = set_point - feedback
So, if we are traveling at 30 km/h and our setpoint is 100 km/h, the error would be 100 - 30 = 70 km/h.
That error term is used as the input to the controller. The controller does whatever is needed to create an output signal that’s used to drive the process. In our example, the output signal is the position to set the gas pedal (in cm).
PID Controller Overview
There are many different types of controllers used in practice. However, a very popular one is the PID controller. PID stands for “Proportional, Integral, Derivative.” It describes how the controller operates.
Three separate terms are computed 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)
Each of these terms corrects problems found in the other terms. Together, they achieve a balanced approach that helps the plant meet its goal quickly with little or no overshoot.
Computing or adjusting the constants, Kp, Ki, Kd, is known as “tuning.” There are a number of ways to tune a PID controller, and it is something we will cover in a future post.
P Controller
A proportional controller (P controller) uses just the error term to affect the output; we simply multiply e(t) * Kp. In some cases, this is all we need. However, let’s take a look at how this might work in a cruise control system. We assume Kp = 0.1 and that a 5 cm pedal position allows us to cruise at 100 km/h on a flat road.
As we start from 0 km/h, the pedal is pressed to 10 cm, which allows us to accelerate quickly. However, as we approach 60 km/h, you can see that the pedal is kept at 4 cm, which is already less than the required 5 cm to cruise at our set point. Even if we hit 100 km/h, the error would be 0, which would equate to a pedal position of 0.1 * 0 = 0 cm (meaning no power from our engine). Our car will never reach its goal of 100 km/h! This offset (of 40 km/h) is known as the steady-state error.
PI Controller
To account for this steady-state error, we add another term to our P controller: the integral. This term keeps a running sum of the error over time and multiplies it by a separate constant, Ki.
If we are working in continuous time, then we would likely need some calculus to actually compute the integral to get the area under the error curve. However, most modern PID controllers are digital and operate on discrete time, which means we simply sample the error at a set interval. This makes the math much easier! We can just accumulate the error term and multiply it by the constant.
Note that for a true discrete-time integral controller, we would need to multiply the accumulated error by the interval (Δt). If we assume that Δt is constant, we can simply make this part of our Ki during tuning.
We sum the proportional and integral terms together to get the output. This combination is known as a “PI controller.” We’ll assume that Ki = 0.01 for this example.
Notice that as the proportional term begins to let off the pedal, the integral term starts to take over as the accumulated value increases. Even when the proportional term reaches its maximum and no longer contributes (i.e. producing a steady-state error), the integral term makes up for this problem to put the pedal in the correct position (e.g. 5 cm).
If the plant overshoots the goal, the error term, e(t) will be negative. The integral term will then decrease while the e(t) is negative to ultimately pull the plant's output in line with the set point.
In many cases, a PI controller works perfectly well. However, at higher Ki values, the system will often oscillate around the set point before settling. This is known as an underdamped system. Oscillations are often undesirable, as any overshoot might mean breaking the system (or breaking the law, in the case of our cruise control).
With lower values of Ki, you might find the system does not oscillate, but it takes a long time to reach the set point. This is known as an overdamped system.
In many cases, you want a critically damped system that quickly approaches the set point and settles without oscillations. With just proportional and integral terms, this is often hard to achieve.
PID Controller
In our final form, we add a new derivative term to the equation. This part of the controller finds the slope of the error term, multiplies it by a new constant, Kd, and adds that to the proportional and integral terms. If we are working in discrete time, we do not need to solve the actual derivative. We can estimate the slope by subtracting the previous error value, e(t-1), from the current error value, e(t). If we assume a constant Δt, we can roll that into Kd (like we did for the integral term).
As you can see from the example, if the error term is quickly approaching 0, the derivative term pulls back on the acceleration. In other words, it counteracts the effects of the proportional and integral terms to prevent overshoot.
Note that the above example is contrived: it demonstrates how the derivative term works against the proportional and integral terms at first before settling to 0 when the set point is reached. In reality, this example would likely have some overshoot, as I did not appropriately tune Kp, Ki, and Kd. With proper tuning, we should (in theory) see a critically damped response curve.
Code for a Digital PID Controller
The very first PID controllers (as analyzed by Nicolas Minorsky in the 1920s) were pneumatic. Eventually, analog electronics (e.g. using op-amps) were used to create such controllers. Most modern PID controllers are built using computers or microcontrollers (including programmable logic controllers, PLCs). As a result, PID control schemes are often written in code. To get you 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)
As you can see, we don’t actually need any calculus! We simply sum the error values over time for the integral term, and we find the difference between the current and previous error values for the derivative term.
In the example code, we don’t do anything with the output value–that’s left up to you to use for whatever you want to control. Additionally, we perform a division in the derivative term and an extra multiplication in the integral term to account for the interval values. In reality, if you can assure a constant interval value, you can avoid those extra compute steps to save some clock cycles. Just note that your Ki and Kd values will be different.
Going Further
If you’d like to learn more about PID controllers, check out the following materials:
- How to Control Output Voltage using a PID
- The PID Controller & Theory Explained
- Wikipedia: PID Controller
STMicroelectronics has a fantastic inverted pendulum kit to help you get started with control theory (including PID controllers). They have an entire manual and curriculum complete with videos, theory discussions, and hands-on lab exercises.
All the slides used in the video can be found here.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum