digital PID controller
closing the loop with a PIC
The term ‘PID controller’ normally brings to mind an analogue controller circuit with a couple of opamps. This article shows how such a controller can be implemented in digital form. In this universal controller circuit, based on a PIC and a D/A converter, all control parameters can be entered via a keyboard to adapt the controller to the task at hand.
The digital PID controller described in this article, based on a PIC microcontroller, is on the one hand very well suited to studying how the controller operates with various parameter values, using simple concontroller type: PID trol loops. On the other parameter entry: hexadecimal keypad hand, it can of course parameter display: alphanumeric LCD also be used for specific adjustable parameters: - set value control tasks. A complete - initial control quantity value control loop requires an - upper limit appropriate actuator and - lower limit a suitable sensor, in addi- sampling interval (100 ms to 25 s) tion to the controller parameter storage: EEPROM itself. The core of the circuit A/D and D/A conversion: 8 bit is an inexpensive PIC (measured value, control quantity) 16C71 microcontroller, microcontroller: PIC 16C71 whose built-in A/D converter digitizes the measured value (actual value) and proDesign By D. Kohtz vides the result to the controller program. The program determines the control error relative to the set value (target value) for each actual value and, based on this, calculates the control value that is output to the external D/A converter. The control parameters, which are entered by a keypad, are stored in a non-volatile memory (EEPROM). In order to manage with the program memory of the PIC, which is only 1 kB, the parameters are entered as hexadecimal codes. They are however displayed on the LCD as text, which is easier to understand.

➤ ➤ ➤ ➤

➤ ➤ ➤


THEORY Control circuits are encountered very often in electronics. For instance, a voltage regulator is present in almost every circuit. More elaborate controllers for dealing with other physical quantities, such as rotational speed,
Elektor Electronics 9/99


pressure. As shown in Figure 3. flow and so on. the controller must measure an actual value y. has proven its worth as a sort of ‘universal’ controller. which can come from any desired type of sensor. It is necessary that a given input signal produces a specific output signal. For a PID controller. The controller output provides a 2 y (t) Figure 2. the control value consists of the sum of the outputs of the three types of controllers. as illustrated in Figure 1. Tu = delay TG = transition period Kp = amplification Tu t=0 TG t 990038 . it can be classified as a proportional. the transfer element is in principle nothing more than an amplifier. Example of a jump reply. A controller that combines all three types of response. As the names suggest. HARDWARE AND SOF TWARE Figure 4 shows the circuit diagram of the controller. A closed loop is applied to control a process.velocity. The primary difference between a digital controller and an analogue controller is that with a digital controller the actual value is not measured continuously. The job of the controller is to maintain the value of the controlled quantity at a set-point or target value. for both the controller and the process. In order to do this. |q1| > q0 |q0 + q1| < q2 < q0 As a guideline for the value of the sampling interval T0. both the time constant and the gain are adjustable parameters. if a given DC voltage is applied to a resistive heating element. In general. In the time between two successive samples. it should chosen to be around one tenth of the time required for the process step response to reach 95% of its final value. The parameters must also satisfy the following conditions: q0 > 0 q1 < 0. with the gain being an adjustable parameter. integral or derivative controller.5TG/(KpTu)](1 + Tu/2T0) Figure 1. the controlled activity is referred to as the process. The low-pass filter formed by R8 and C8 at the output of the opamp keeps high-frequency interference from reaching the A/D input (RA0 of the PIC). The input voltage is passed to the internal A/D converter of the PIC 16C71 via opamp IC5a. Depending on the way in which the controller responds. A controller is also a transfer element that produces a control value (output signal) in response to a control error (input signal). the index k represents the running count of the individual measurements. For example. The following equation can be used as the control algorithm of a PID controller (see the sidebar ‘Digital PID controller design’ for the derivation of this equation): uk = u(k–1) + q0ek + q1e(k–1) + q2e(k–2) In this equation. calculate the control error e by comparing the actual value to the target value w. In a proportional controller (P controller). Its input is designed for voltages between 0 and 5 V. q1 = [1. and output a control value u that depends on the value of the control error and which achieves the desired effect. Its output signal changes in proportion to its input signal. Elektor Electronics 9/99 1 set value (target) w control error e control value u interference value z controller process actual value y e=w-y 990038 . In control theory. and the transfer element of a derivative controller (D controller) is a differentiator. the control error must be determined and the control value calculated using the control algorithm. called a PID controller. temperature.13 15 .) A characteristic feature of every control system is closed loop operation. The control parameters q0 through q2 can be approximately determined from the characteristic parameters of the process step response using the following formulas: q0 = [1. the transfer element of an integral controller (I controller) is an integrator. The waveform shown in Figure 2 illustrates a step response that is typical of many processes.12 The mathematical relationship between the control error and the control value is called the control algorithm. the relationship between the input an output signals is timedependent. (Note: the official term for the control value is ‘manipulated variable’. this is a differential equation. This means that e(k–1) is the control error for the previous measurement.5TG/(KpTu)](T0/2Tu – Tu/T0 – 1) q2 = 3TG/4KpT0 The step response of the process must be measured experimentally to obtain these values. the graph of its temperature as a function of time will be similar to this curve. The step response is an essential characteristic of both processes and controllers. which acts as a buffer. It reflects how the output signal responds when the input signal u changes instantaneously from one level to another level. in spite of the effects of external interference. but is instead periodically sampled at some fixed interval T0. Control technology deals with finding solutions for such problems. With the latter types of controllers. which can be assigned individual weighting factors according to the desired controller response. Let’s look at the basic principles of this technology.

Its gain is set to 1. the same port pins are used for scanning the contacts of the parameterinput switches. used for the D/A converter. two electrolytic capacitors and a reverse-polarity protection diode at the input connector for an external 9 V mains adapter.11 16 Elektor Electronics RB7 10µ 10V 10k C2 C3 9/99 . it connects RB0 to earth when actuated. Circuit dialog Devices gram of the PIC-based AD577) is digital PID controller. Block diagram of a PID controller. In the input mode.3 P controller control error e I controller control value u Figure 3. The program is protected by a watchdog timer. you will naturally have to provide a suitable power stage that can be driven by the maximum load current (5 mA) of the controller output. The mathematical routines for calculating the control quantities also take up a certain amount of space. The power supply is just as simple as the rest of the circuit. IC5b matches the voltage range of the D/A converter to that of the A/D converter of the PIC. Its external circuitry is limited to C7. which in case of a failure triggers a reset and an alarm (blinking LED D1) and also sets the control quantity to zero. It can be operated from a single 5 V supply and does not need an external reference. It consists of a 5 V regulator. pull-up resistors R2 and R3 are connected to the clock and data pins of the EEPROM IC (24C01). The output current of opamp IC5b is certainly adequate for use with the previously-mentioned circuit that simulates the process to be controlled. If this happens. with four port connections on each side. Furthermore. The actual controller program is modest by comparison.935 by R10 and R11. Except for S2. while a manual reset is provided by R6 and S1. but it is not necessary to re-enter the control 4 2 R12 10k R8 1k 5V D1 C4 100n R5 10k R1 1k5 14 5V C7 100n 11 RB0 17 18 1 RA0 RA1 RA2 RA3 RA4 RB1 6 7 8 9 10 11 12 13 RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 1 2 3 4 5 6 7 8 D0 D1 D2 D3 D4 D5 D6 D7 5V R10 9k53 1% 6 R7 10Ω IC5b VOUT 16 15 14 10k0 R11 5 7 IC5a 3 1 IC3 SA SB IC2 PIC 16C71 RB2 RB3 RB4 RB5 RB6 1M R13 C8 10n 2 3 1% AD557 CS CE 10 9 8V4 R9 10k 8 5V 4 MCLR OSC1 16 OSC2 15 270Ω R4 RB7 5V R6 100Ω C11 100n 5 12 13 IC5 = TLC272 IC5 4 C6 C5 10µ 10V P1 S1 X1 4MHz K1 VSS VDD VO RS R/W E D0 D1 D2 D3 D4 D5 D6 D7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 RB4 RB5 RB6 RESET 22p 22p S15 S16 S17 S18 5V RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 +9V D2 +8V4 C1 5V RB3 C S11 D S12 E S13 F S14 5V IC4 7805 +5V 1 2 100n 8 A0 A1 A0 WP R2 10k R3 10k RB2 8 S7 S8 9 S9 A B S10 1N4001 C9 100µ 25V C10 10µ 10V IC1 SDA SCL 5 6 RB7 RB6 RB1 4 S3 S4 5 S5 6 S6 7 3 7 24C01 RB0 4 0 1 S2 2 3 LM16A211 SET 990038 . there is a reset circuit which requires only a few passive components. and the D/A converter chip and the LCD module are also connected directly to the output port pins of the PIC. In addition. which is necessary to decouple the supply voltage. the controller must be restarted. The poweron reset is provided by R5 and C5. the tables for the predefined display text and the instructions needed to control the serial EEPROM both take up quite a bit of storage space. only a small amount of extra hardware is needed around the PIC itself. The alarm LED D1 is driven directly from the RA4 port output. these are arranged as a 4×4 matrix.15 voltage that also ranges from 0 to 5 V. For controlling a real process. D controller 990038 . An 8-bit DAC IC (AnaFigure 4. S2 is used as the SET pushbutton for confirming input values. As far as the software is concerned. In addition to the clock circuitry with a 4 MHz crystal.

due to the internal connections. order code 990038-1 Disk.C10 = 10µF radial C6 = 10µF 10V C8 = 10nF MKT (Siemens) C9 = 100µF 26V radial Semiconductors: D1 = LED.R3. shown in Figure 7. A simple circuit.C7.R12 = 10kΩ R4 = 270Ω R6 = 100Ω R7 = 10Ω R8 = 1kΩ R10 = 9kΩ53 1% R11 = 10kΩ 1% R13 = 1MΩ R5 D1 C5 R1 IC5 R10 R8 C7 R4 IC3 C3 C2 X1 C8 C11 R7 OUT H2 H1 K1 P1 C1 IC1 IC2 R9 C6 C4 R13 R12 R11 Capacitors: C1.R5. order code 996003-1 S15 S16 S17 S18 0 R6 C S11 D S12 E S13 F S14 IC4 +5V 8 R3 R2 S7 9 S8 A S9 B C9 S10 0 C10 4 S3 5 S4 6 S5 7 S6 S2 D2 S1 +9V 0 H4 1 2 3 Set Reset H3 990038-1 ROTKELE )C( 1-830099 Figure 5. The keypad module specified in the list of components must be used for the parameter input pushbuttons. you will find the source code on a diskette available through our Readers Services. The only disadvanElektor Electronics 9/99 T T 990038-1 (C) ELEKTOR T tage is that the registers that are used more than once do not have meaningful names. This circuit simulates the process to be controlled. PCB track parameters. When setting up the board. calculations are never used during some registers must be used for more parameter input.C4. PCB mount.C11 = 100nF ceramic C2. Do not overlook them when stuffing the board.2 x 16 characters (Sharp) S1-S18 = pushbutton. a few jumpers are unavoidable. this is not a mounting plan (board ously stored in the EEPproblem. In layout and component values that were previprinciple. since (for available ready-made).R9. order code 996504-1 IC3 = AD557JN (Analog Devices) IC4 = 7805 IC5 = TL272CP Miscellaneous: X1 = 4MHz quartz crystal K1 = LM16A211 LCD module. contains PIC source code. If you want to carefully study or modify the program.C3 = 22pF ceramic C5. The step response of the process is determined by the time constants of three RC low-pass circuits 17 . first adjust P1 to set the contrast of the display for optimum readability. red. since the than one purpose. example) the registers Since the program used for performing needs a very large number of registers. has been designed for testing the controller and experimenting with it. ITC type D6-R-RD PCB. Since the circuit board is single-sided. as long as it can supply at least 200 mA. high efficiency D2 = 1N4001 IC1 = 24C01 IC2 = PIC16C71.5 COMPONENTS LIST Resistors: R1 = 1kΩ5 R2. ROM can be used again. CONSTRUCTION AND OPERATION Constructing the circuit on the printed circuit board does not require any unusual skills. An unregulated 9 V mains adapter can be used for the power supply.

The stored values are displayed for each parameter in turn. The bits after the decimal point are weighted according to 1/2n.5D = 0D80H q0 = q1 = –18.14 100k 7 1k 4k7 10k Since the electrolytic capacitors in the simulation circuit have quite large tolerances. tion ‘INTERVAL’) is determined by multiplying a basic increment of 0. If the controller is started anew by pressing Reset.6 For 8-bit signed numbers. the control value (CONTROL VAL) is output at a level equal to its entered initial value until the entered target value (SETVALUE) is reached. which corresponds to a voltage range of 0 to 5 V. with one byte (two hex characters) before the decimal point and one byte after the decimal point. An example is: 13D = inverted: +1: 0000 1101B = 0DH 1111 0010B = F2H 1111 0011B = F3H 100µ 100µ 100µ 990038 . The parameters q0. the parameter entry process must be run through again by pressing SET after each parameter value is displayed. and can be modified if necessary. and interference can be easily simulated by loading the output with the switchable resistor. Negative 8-bit numbers can be recognized by the fact that the MSB is a 1. The target value and the initial value of the control quantity can be set between 00H and FFH. The values of CONTROL VAL. UPPER LIMIT. If either the upper or lower limit value (UPPER LIMIT or LOWER LIMIT) is exceeded. A hex value of 80 after the decimal point thus corresponds to 1/2 (0. The controller output goes to zero if the upper limit is exceeded.4 s (enter ‘04’) target value = 80H (approx. During the control process. After parameter entry has been completed by pressing SET. No new values are displayed during this interval. with the last saved value. The length of the Figure 7. A negative binary number is formed by bitwise inverting a positive number and adding one to the result.1 s (fixed in software) by the entered hexadecimal value. and to FFH (5 V) if the lower limit is exceeded. This little cirsampling interval cuit simulates a (display indica‘process’ for testing the controller. In this case the values of q0 through q2 should be empirically adjusted. The time constants of this ‘electronic process’ can thus be easily changed. the actual control process starts up and. The output signal of the controller is set to 0 V during parameter entry. where n represents the bit position (MSB = 1. Two characters will be displayed after two keys have been pressed in turn. LSB = 8). are entered as fixed-point numbers. Finished prototype of the digital controller. (990038-1) 18 Elektor Electronics 9/99 . this results in a range of values from +127D (7FH) to –128D (80H).5 decimal).5D = 0680H sampling interval = 0. q1 and q2. connected in series. depending on the selected control parameter values. Q0HI and Q0LO are entered by pressing two keys for each byte. 2. If you use the simulation circuit shown in Figure 7. connect its input to the output of the controller and its output to the input of the controller. The upper and lower limit values can also be set between 00H and FFH. Control parameter values can be entered only immediately after the circuit is switched on or directly following a reset. even in the presence of interference. Since q1 is always negative. The SET button must be pressed for the entered value to be accepted and stored in EEPROM. To use the simulation circuit. a short explanation of the representation of negative binary numbers is in order.1 V) Figure 6. with SET pressed between the first and second pair of value-entry keys). more or less exactly maintains the controlled variable at the set value. the current levels of the control value (CONTROL VAL) and the actual value (ACT. LOWER LIMIT and INTERVAL follow in turn.5D = ED80H q2 = 6. They are entered in hexadecimal form by means of the keypad. you can achieve satisfactory results with the following parameter values: 13. 3.VAL) are continuously displayed. it is possible that the controller will not operate optimally with the above values. the alarm lamp (LED D1) blinks for the duration of out-of-limits condition. After this.5 V) initial control value = A0H (approx. The integer and fractional bytes of q0 through q2 must be entered separately (for example. which are important for the control algorithm. each appearing after SET has been pressed. SETVALUE appears first on the display.

This equation can also be represented in the frequency domain by applying the Laplace transform. the result is the following equation. The gains of the three parts of the controller (often referred to as the proportional. KDR and KIR represent the (adjustable) gains of the three controllers (P D and I con. the index k is the running count of the individual measurements. This means that e(k-2) is the control error for two sample intervals prior to the present measurement. as follows: ∫ f( τ) = T[f(a +T)+ f(a +2T)+ . + f(b)] a u(s) = K PR ⋅e(s)+ K DR ⋅s⋅e(s)+(K ID / s)e(s) where it is assumed that the initial conditions are all zero. The finite sum is defined as the discrete-time equivalent of the continuous-time integral of a function.Digital PID controller design The easiest approach is to start with an analogue controller designed according to the block diagram shown in Figure 3. The backwards difference is defined as the discrete-time equivalent of the continuous-time derivative of a function. integral and derivative coefficients of the controller) must be set by the designer according to the desired control response. . whose control algorithm is given by the following differential equation: ∆f (t ) = f (t )− f (t − T ) / T in which T is the period of the sampling signal (the sampling interval). which is shown in the article as the control algorithm of a digital PID controller: u k = u(k−1) + q 0⋅e k + q1⋅e(k−1) + q 2⋅e(k−2 ) Once again. It is given by the formula b [ ] u(t ) = K PR ⋅e(t )+ K DR ⋅de(t )/ dt + K IR ⋅ ∫e( τ)dτ 0 t In this equation. It is given by the formula If the relationships shown in formulas 3 and 4 are applied to the first two equations. trollers) shown in Figure 3. . KPR. . The next step is to find a discrete equivalent for the controller described by equations 1 and 2.

Sign up to vote on this title
UsefulNot useful