Various sensor input is typically used to control output devices such as motors and heater coils etc to achieve a desired "set point". The simplest approach is the so-called "bang-bang controller" in which the output is fully "on" until the set-point is reached, after which the output is fully "off". Obviously this sort of approach will provide over-shoot and under-shoot (oscillation) depending upon the characteristics of the system.
Although bang-bang controllers can be optimal controls in some cases (1), the oscillation produced is often undesirable and not best use of the system input (energy etc). Further, it may be required to have the set-point achieved and the control value remain within a certain tolerance (maximum and minimum value around the set-point). A PID (Proportional-Integral-Derivative) Controller attempts to use to the measured system value (eg temperatue) and adjust the controlled output (eg heater power) to minimise the fluctuation/difference compared to the set-point (eg desired temperature). While working on the fruit/vegetable dehydrator, there was the need to control the air temperature within the drying container, and therefore the use of a PID Controller was explored.
The test prototype involved controlling the air temperature within an enclosed container by applying power to a heater coil via a PWM/mosfet. A PIC 16F876A was used as the system controller, with PID firmware controlling the PWM output from the PIC (using interrupts from Timer1 as the time step). The PIC 16F876A was interfaced to a DHT-11 humidity/temperature sensor for measuring the enclosure air-temperature. Collected data from the test system was sent to PC for analysis/graphing via RS232 while a Nokia LCD was used to display system status/data during test runs.
PID (Proportional-Integral-Derivative) Controller Theory
Wikipedia provides (as usual) a good introduction to the background and use of PID Controllers (2). However, "PID with a PhD" by Tim Wescott should be probably a "must read" (3). The link to this article from Tim Wescott's page wasn't particularly helpful, but the article is available elsewhere (4). Finally, the "Improving the Beginner's PID" site explaining/modifying an Arduino PID library code is also probably "essential" (and the code used in this project was based on this example) (5).
The previously mentioned references explain PID controllers, so I don't provide much detail here, but since web sites "come and go", and as per the usual rationale to document "things" for "myself", the following are what I see as the main points."
Crucial terms are "process variable", "set point" and "error" (as well as what "proportion", "integral" and "derivative" mean).
- Process Variable: This is the "feedback" signal from the system, i.e., the measurement of the variable/parameter of interest (e.g. the temperature of a container, the speed of a motor, current being consumed etc).
- Set Point: This is the desired value (or target) for the variable/parameter of interest (e.g. the temperature a container must be kept, required speed of the motor under differing load, etc)
- Error: This is the difference between the "set point" and the "feedback" or actual measured value. The PID controller attempts to minimise this "error" as quickly as possible.
- Proportional (term/gain/factor): Kp, This is a factor or "gain" that simply is used by multiplying the value of the calculated "error". The proportional factor changes the output according to the current measured "error". This means if the "error" is large (i.e. the current measured value of the signal of interest is a long way from the set point) the output will also be large (Kp * error). Conversely, if the "error" is small the output will be small. The higher the value of Kp the faster the system will reach the set point. However, high values of Kp can also lead to the system being unstable and wildly oscillating around the set point. Too low a value for Kp can result in the system stabilising but with a value to low and below the set-point.
- Integral (term/gain/factor): Ki, This factor enables the system to change the output in response to the observed previous recorded "error" values. This means the "integral" is the sum of the previous "errors". Again, the term is used by simplying multiplying Ki by the sum of the previous errors. This helps to avoid the situation of the system stablising below the set point.
- Derivative (term/gain/factor): Kd, This term enables calculating how quickly the "error" is being eliminated. If the value of the calculated "error" is changing slowly, then the Kd factor enables changing the output quicker and vice versa. Again, the Kd factor is used by multiplication, but this time not the current "error" but the difference between the current "error" and the previous observed value of the "error".
- PID equation: The calculated output of the PID controller is simply PIDoutput = (Kp * error) + (Ki * (sum of previous error)) + (Kd * (error - previous error))
There are a number of other "practical" considerations such as the "form" of the value being measured (e.g. is the "feedback" signal a voltage, temperature (in degrees celsius/farenheit), speed (RPM) etc), how is the calculated PIDoutput used then control the system (e.g. how can the PIDoutput then affect the actual voltage, speed etc of the equipment/circuit in question), how often is the system measured, what happens if the "sum of the previous errors" gets very large (e.g. integral windup), how do you determine the values of Kp, Ki and Kd, etc.
These "practical" issues are dealt with during the discussion of the actual test circuit and the firmware code in the sections below (and (5) steps through these problems in detail).