Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

13 August 2011
Author: Giorgos Lazaridis
PID Theory

A PID controller example explained in simple words

After 2 pages of theory and mathematical hocus-pocus, there will be people that still are not able to write software for a PID controller. For those people, i have 2 good news: First is that i found the best documentation for making a PID controller with a PIC microcontroller. It is the Application Note 937 from Microchip (who else):

 Implementing a PID Controller Using a PIC18 MCU [Microchip AN937]

Second good news: A PID controller example explained in simple words

I understand that not all people feel comfortable with mathematics, but many would like to make a PID controller for number of reasons. The integral term is by far the hardest to decipher. Instead of just explaining the integral term, i will explain a complete (and simple) PID controller with an example. I bet that after this, everyone will be able to make one (as long as he know how to program a microcontroller of course).

So, it goes like this: We want to control the temperature of a water heater. The heater resistor is controlled by a TRIAC, and the microcontroller controls the gate of that TRIAC (it could operate as a dimmer for example). A temperature sensor is placed in the water and gives feedback to the microcontroller in degrees Celsius (oC). The system has these parameters which you must define as RAM variables, and also it is good to save them in an EEPROM memory because you do not want to define them every time the system restarts:

• SP - The Set-Point of the system, the temperature that we want to achieve
• P_Gain - The gain for the Proportional term
• I_Gain - The gain for the Integral term
• D_Gain - The gain for the Derivative term
• I_Time - Reset time, this is the Ti parameter that we saw on previous pages
• D_Time - Derivative time, this is the Td parameter that we saw on previous pages

• You will also need to define these additional RAM variables:

• P_Term - The Proportional term to be added to the output.
• I_Term - The Integral term to be added to the output.
• D_Term - The Derivative term to be added to the output.
• PV - The Process Variable, this is the temperature that the sensor reads.
• Err - This will hold the current error (SP-PV).
• Acc_Err - This is the accumulated error to help calculate the Integral term
• D_Err - This is the derivative error to help calculate the Derivative term
• Ti_Counter - A countdown counter for the Integral accumulation routine
• Td_Counter - A countdown counter for the Derivative error calculation
• P_Out - The output power calculated by the PID

• This is the simplest PID controller. The output of this system is described by this function (do not be scared if you know little about mathematics)

The microcontroller will run an infinite loop, during which it will calculate the PID terms and control the output. I call this routine "MainLoop". During start-up, the microcontroller will run the "Initialization" routine. Here are the flow chart of the Initialization and the MainLoop routines: (Click to enlarge)

 Initialization (Click to enlarge) MainLoop (Click to enlarge)

Finally, there are 3 more subroutines, Calculate_P_Term, Calculate_I_Term and Calculate_D_Term. Here are the flow charts for those:

 Calculate_P_Term(Click to enlarge) Calculate_I_Term(Click to enlarge) Calculate_D_Term(Click to enlarge)

How it works then?

First of all, i want you to agree that, behind the mask of the hard math, there is only a simple program with additions and subtractions (ok, and some multiplications and divisions maybe). I assume that you do know how to read a flowchart and i will not go into details. Regarding the symbols that i use: An ellipse indicates the start and the name of the (sub)routine that follows, a rectangle has the parts of a routine that have to be translated directly into code, a parallelogram defines a call to another sub-routine and a diamond defines an IF case, with a YES or NO branch.

Looking into the MainLoop, you can see that the system reads the temperature and calculates the error in every loop. The error is calculated by subtracting the PV from the SP (Err=SP-PV). If for example we want the water to have 80oC temperature (SP) and the current temperature is 25oC (PV), the error (Err) is 80-25=55oC. Using this error, the program calculates the P_Term (Calculate_P_Term subroutine), simply by multiplying the error by the P_Gain (P_Term=Err*P_Gain). Until now, this is the typical operation of a simple P-controller.

Now to add the I term. Looking again into the MainLoop, we see that the Calculate_I_Term subroutine is NOT called in every loop. Instead, a counter is used (Ti_Counter). The initial value of this counter is taken from the I_Time system parameter (Ti). The counter is decreased on every loop. When the counter becomes zero, the Calculate_I_Term subroutine is called. So, this is where the Ti parameter is used. Unlike the P_Term, the I_Term is calculated only once every Ti loop cycles. To calculate the I_Term the system adds the current error (Err) to a variable named Acc_Err. This variable accumulates all past errors and therefore is called Accumulated Error. The I_Term is then calculated by multiplying this variable by the I_Gain (I_Term=Acc_Err*I_Gain). Now you understand the purpose of the Ti parameter. If the system did not have this parameter, it would accumulate the current errors constantly, and therefore the I_Term would become huge and it would overshadow the P_Term and D_Term.

Finally, the D_Term. Similar to the I_Term, the D_Term has a different countdown timer (Td_Counter), so the Calculate_D_Term subroutine is called once every D_Time (Td) cycles. To calculate the D_Term, the system must know the previous error. The previous error is saved in the D_Err variable. So, the system can calculate the error difference by subtracting the previous error from the current error (Err - D_Err). Finally, the D_Term is calculated by multiplying the error difference by the D_Gain (D_Term = (Err - D_Err) * D_Gain).

The output of the system is calculated in every loop, and it is the sum of the 3 terms:

P_Out = P_Term + I_Term + D_Term

ATTENTION

There are some problems that may come out from the following example. First of all, you have to decide if you will use 8-bit registers of longer for the variables. This is something that you need to decide before you start designing the system. You have to take into account the system's range and accuracy. For example, a temperature controller that is used for a system which measures temperatures from 0 to 100 degrees with 1oC accuracy can be implemented with 8-bit registers, But if the system measures from 0 to 600oC then the 8-bits are just not enough.

Moreover, extreme caution must be taken with the P_Out and Acc_Err registers. Both of them are subject to overflow when big numbers are added. If for example an 8-bit registered is selected for the Acc_Err variable, but an error of 100 degrees is added 3 times, then the register will overflow and the result will be wrong. Same can happen to the P_out register if the result of the P and D term addition is larger than the register can handle. Therefore, make sure that you put some limits to these registers and check them every time to see if they exceed these limits.