# I don't understand Integral part of PID controller

I dont understand integral part of PID controller. Let's assume this pseudocode from Wikipedia:

previous_error = 0
integral = 0
start:
error = setpoint - measured_value
integral = integral + error*dt
derivative = (error - previous_error)/dt
output = Kp*error + Ki*integral + Kd*derivative
previous_error = error
wait(dt)
goto start


Integral is set to zero in the beginning. And then in the loop it's integrating the error over the time. When I make a (positive) change in setpoint, the error will become positive and integral will "eat" the values over the time (from the beginning). But what I dont understand is, when error stabilizes back to zero, the integral part will still have some value (integrated errors over time) and will still contribute to the output value of controller, but it should not, because if error is zero, output of PID should be zero as well, right?

Can somebody explain me that please?

The main purpose of the integral term is to eliminate the steady state error. In the normal case there is going to be a small steady state error and the integral is mainly used to eliminate this error. It's however true that when the error gets to 0 the integral will still be positive and will make you overshoot. Then after overshoot the integral will start to go down again. This is the negative effect of the integral term. So there is always the trade-off and you have to tune the PID controller to make sure that the overshoot is as small as possible and that the steady state error is minimized. Here is where the derivative term come into play. The derivative term helps to minimize the overshoot in the system.

• And a good example of steady state error is friction in a joint. Lets say your PD controller settles close to your target joint angle, but can't quite get there due to friction. The "I" term will slowly build up and eventually generate a large enough input to overcome friction.
– Ben
Nov 25, 2012 at 3:19
• Another example is bias in steering. If it turns out that there's a slight bias in the steering control, or, for tread-style robots, one tread turns slightly slower than the other despite the controller setting them to the same value, there'll be a bias. The integral term, set properly, corrects for that. Jan 24, 2013 at 4:00
• The correct asnwer has been provided by Ian. Dec 31, 2020 at 19:15

Imagine that you set up a PID controller on your own arm, so that you could hold a cup of coffee straight out in front of you.

• The proportional element would control your arm strength relative to your hand position being too high or too low.
• The derivative element would adjust that strength based on how quickly you were already moving, so that you don't overshoot your target.
• The integral element would compensate for the effects of gravity; without it, the cup would come to rest where the proportional force equaled the force of gravity.

It sounds like the part of the code you're stuck on is that the system must somehow measure the weight of the coffee, and one way to do that is to accumulate the position error over time. Most PID controllers have an additional term to specify a reasonable limit on the size that the integral element can be.

• +1. "if error is zero, output of PID should be zero as well, right?" As Ian explains, even when the coffee cup is in the perfect position and the error is zero, the output of the PID needs to have some upward force to hold that cup in position. Nov 26, 2012 at 18:55

But what I dont understand is, when error stabilizes back to zero, the integral part will still have some value (integrated errors over time) and will still contribute to the output value of controller, but it should not, because if error is zero, output of PID should be zero as well, right?

Not right--the output should be whatever it takes to hold the system at the setpoint. Consider the system of an electric space heater with a 0-11 knob that needs to be set at "5" to maintain the steady state temperature at the setpoint of 72°F.

If PID measures temperature and calculates every second, the error is in units of °F, the integrated error is in units of °F*s, and the derivative is in units °F/s.

At steady state, the error between setpoint and measurement is zero °F, so the proportional term can't contribute anything. At steady state the derivative of the error is zero °F/s so that term can't contribute anything. All that is left to hold the heater knob at the proper setting is the integral term, which converts the sum of the history of errors into heater knob units.

Supposing Kp = 1, Ki = 0.001, and Kd = 1, then for an output setting of 5 knob units at steady state the PID equation is:

5 knobUnits = 0°F * 1 knobUnit/°F
+ 0.001 knobUnit/(°F*sec) * integral
+ 0°F/s * 1 knobUnit/(°F/sec)


... where the proportional and derivative terms drop out, and the integrated sum of errors is

5 knobUnits / (0.001 knobUnit/(°F*sec))= integral
5000 °F*sec = integral


It is perfectly fine for the integral to be non-zero when the process is at setpoint--It is the only term providing output at steady state.

Here's a video that gives an "intuitive" understanding of PID loops. It has an explanation of the integral term, as well as the proportional and derivative terms.