How to tune a PID controller? best practice

P = Proportional gain

P  Background
 Proportional gain should get you most of the way to the finish line  this is likely the largest win of the 3.
 Proportional looks at how far you are away from the setpoint and applies force in the opposite direction to push you toward setpoint.

P  How to tune the Proportional gain?
 Step 1 Zero out the other gains and focus on tuning the P first.
 Step 2 There is some P gain value above which the loop goes unstable  stay well below that point. Too high proportional gain and you get uncontrollable osscillations.
 Step 3 Slow reaction to stimulus? Increase proportional gain.
 Step 4 You’re done setting the P when you’ve got a steady state offset error. This is expected and fine.


I = Integral gain

I  Background
 If you have a steady state offset from the proportional, you fix that with a little bit of integral.
 The integral portion of the control loop looks at how far you are away from setpoint for how long (that extra time component is the key difference between P and I).
 It pushes a little bit if you’re away from setpoint for a little bit of time and pushes ever harder the longer you stay away from setpoint.

I  How to tune:
 Step 5 Start the I at 0 (zero) and bring it up.
 Step 6 You know you’ve overdone the I gain when you get a steady state oscillation.
 Step 7 Often it is typical to end up with something like I=P/20, but your case may of course be different.


D = Derivative gain

D  Background
 Derivative gain helps predict future errors.
 It acts as a dampening force, slowing down the system’s response as it approaches the setpoint to minimize overshoot.
 So it’s basically telling your controller how big the lag is between its action and the actual response of the system. A big D means telling your controller there’s a big lag so it can anticipate that.

D  How to tune?
 Step 8 Begin with the D gain set to zero and only adjust it after tuning P and I.
 Step 9 Increase D gain until the loop responds quickly to changes without overshooting the setpoint.
 Step 10 If the system starts to oscillate or becomes too “jittery,” bring down the D gain.
 Step 11 Noise affects the performance of the D gain, so use a filter if necessary to remove highfrequency fluctuations.


Important remarks for practitioners

Theory tells you:
 There’s math you can do to theoretically calculate the gains.
 Most of literature says those of us who tune by feel are dumb and should at least feel bad for doing so.

Practice teaches us:
 In reality the math depends highly on the application, but of course research the math behind it if it interests you.
 Step 12 Put multiple layers of software and mechanical safeties in place so if the loop were to go unstable, nothing catastrophic would occur.

Source:
This graph is adapted from a comment by @afterburn in response to this youtube video.