Short PID 101: PID = Proportional Integral Derivative Proportional control means that the correction is proportional to the error. Correction = -1 * P * Perror = -1 * P * (Setpoint - CurrentValue) Integral control means that the correction depends on the present and past error. More exactly, the Integral of the error, which can be represented as the sum of the errors so far. If Ierror0 is the error at the previous measurement time and error is the Ierror at the present time then: Ierror = Ierror + (Setpoint - CurrentValue) Correction = -1 * I * Ierror Derivative control means that the correction depends on the change rate of the error. Therefore if Derror was the previous error: Derror = Derror - (Setpoint - CurrentValue) Correction = -1 * D * Derror Derror = (Setpoint - CurrentValue) By putting it all together and everything in the middle of a loop that repeats at fixed time intervals: Ierror = 0 // initial error integral is zero do // Read input value CurrentValue = Read(Measure Feedback Value) // PID process Error = (Setpoint - CurrentValue) // the error // Integral term if(Error != 0) Ierror += Error // integral else Ierror = 0; // reset integrator if the we have reached the setpoint // Limit integral value if(Ierror > MAXIERROR) // limit integrator Ierror = MAXIERROR; if(Ierror < -1 * MAXIERROR) Ierror = -1 * MAXIERROR; // Derivative error term Derror -= Error // derivative (differential) // Limit it if(Derror > MAXDERROR) Derror = MAXDERROR; if(Derror < -1 * MAXDERROR) Derror = -1 * MAXDERROR // Compute PID correction Correction = -1 * ( P * Error + I * Ierror + D * Derror ) Derror = Error // store the 'previous' derivative for the next pass // Output correction Write(Correction) while forever Note: All the variables are signed. Note2: The integral term will cause a lot of trouble. It is customary to zero it whenever Error is 0 and to limit its growth both in the high direction and in the low direction, to reduce trouble. The Derror is also limited sometimes in the high and low direction to avoid giant kicks when the input Setpoint changes by a large value. Note3: PID tuning is a course by itself. It requires that the user understand the mechanical equivalents of inertial mass and friction losses. Note4: If PID is applied to a mechanism that has backlash then the loop will oscillate. Typical example: model servos. The integral model used here is not very accurate (squares = Riemann sum). Precision PID would use a better integration method. Peter -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics