Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

PID Controller

Firmware for a PID (Proportional-Integral-Derivative) Controller is described for a PIC 16F876A. The PID controller is tested with a DHT-11 humidity/temperature sensor providing input and a PWM/Mosfet output to a air heater coil within an enclosed chamber.

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).


This project is still under active development.

Details about interface of sensors to PIC microcontrollers (i.e components currently being used in this project) can be found in the various pages in the Electronics Projects and Learning Electronics Sections (use menu at top-left hand side of this page).

Note: Image loading can be slow depending on server load.

  • PID Contoller SchematicPID Contoller Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    PID Contoller Schematic

This project is still under active development.

This project was only for a test of concept.

The circuit was only tested to the "bread board" stage.

See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
Resistors
2R1,R410k1/4W, 10% 
1R21001/4W, 10% 
1R32201/4W, 10% 
1R54.7k1/4W, 10% 
Capacitors
2C1,C222pFCeramic 
1C30.33uF 
1C40.1uF 
5C5-C91uF 
Integrated Circuits
1U1PIC16F876AMicrocontroller IC 
1U2LM7805Linear Voltage Regulator 
1U3CD4050Hex non-Inverting Buffer 
1U4LM1086IT 3.3vLinear Voltage Regulator 
1U5MAX232ERS232 Driver/Receiver 
Transistors
1Q1IRF540NPower MOSFET 
Diodes
1D21N-4004 
Miscellaeous
1J1CONN-H55-pin connector for ICSP
1P1CONN9-pin connector for RS232
1SW1SW-SPDT 
1SW2SW-SPST 
1X110MHzCrystal Oscillator
1Z1 Nokia 5110 LCD
1Z2DHT-11Humidity/Temperature Sensor
1Z3 Heater Nichrome Coil
1Z4 12v Fan

The "Background Section" also contains an explanation of how to calculate the necessary parameters to produce a desired frequency and duty cycle PWM. Some ramifications of these calculated values in terms of the hardware implementation within the PIC microcontroller, and how this relates to programming the values into the PIC IC are shown in various code snippets and tabulated calculated results.

This project is still under active development.

Project still under active development.

Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with Silver Membership and above can download.


Silver Membership is only $2, so join now!