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

previous_error = 0
integral = 0 
  error = setpoint - measured_value
  integral = integral + error*dt
  derivative = (error - previous_error)/dt
  output = Kp*error + Ki*integral + Kd*derivative
  previous_error = error
  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.

  • 8
    $\begingroup$ 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. $\endgroup$
    – Ben
    Nov 25 '12 at 3:19
  • 2
    $\begingroup$ 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. $\endgroup$
    – ViennaMike
    Jan 24 '13 at 4:00
  • $\begingroup$ The correct asnwer has been provided by Ian. $\endgroup$ Dec 31 '20 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.

  • 3
    $\begingroup$ +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. $\endgroup$
    – David Cary
    Nov 26 '12 at 18:55

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.


There's a followup video that explains the math and shows how to derive the code from the general formula.


enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.