You are on page 1of 6


digital PID controller

closing the loop with a PIC
The term ‘PID con-
troller’ 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 con-
verter, all control para-
meters can be
entered via a key-
board to adapt the
controller to the task
at hand.

The digital PID controller described in gram. The program determines the
this article, based on a PIC microcon- control error relative to the set value
troller, is on the one hand very well (target value) for each actual value and,
suited to studying how based on this, calculates the control
the controller operates value that is output to the external D/A
Specifications: with various parameter converter. The control parameters,
➤ controller type: PID values, using simple con- which are entered by a keypad, are
➤ parameter entry: hexadecimal keypad trol loops. On the other stored in a non-volatile memory (EEP-
➤ parameter display: alphanumeric LCD hand, it can of course ROM).
also be used for specific In order to manage with the pro-
➤ adjustable parameters: - set value
control tasks. A complete gram memory of the PIC, which is only
- initial control quantity value control loop requires an 1 kB, the parameters are entered as
- upper limit appropriate actuator and hexadecimal codes. They are however
- lower limit a suitable sensor, in addi- displayed on the LCD as text, which is
- sampling interval (100 ms to 25 s) tion to the controller easier to understand.
➤ parameter storage: EEPROM itself.
➤ A/D and D/A conversion: 8 bit The core of the circuit BASIC THEORY
(measured value, control quantity) is an inexpensive PIC Control circuits are encountered very
➤ microcontroller: PIC 16C71 16C71 microcontroller, often in electronics. For instance, a
whose built-in A/D con- voltage regulator is present in almost
verter digitizes the mea- every circuit. More elaborate con-
sured value (actual value) and pro- trollers for dealing with other physical
Design By D. Kohtz vides the result to the controller pro- quantities, such as rotational speed,

Elektor Electronics 9/99

velocity, temperature, pressure, flow
and so on. Control technology deals interference value

with finding solutions for such prob-

1 z

lems. Let’s look at the basic principles

of this technology.
set value control actual
In control theory, the controlled (target) control error value value
activity is referred to as the process. w e u y
controller process
The job of the controller is to maintain
the value of the controlled quantity at
a set-point or target value, in spite of e=w-y

the effects of external interfer-

ence. In order to do this, the con-
troller must measure an actual value 990038 - 12

y, calculate the control error e by com-

paring the actual value to the target Figure 1. A closed
value w, and output a control loop is applied to con-
value u that depends on the value of The mathematical trol a process. q1 =
the control error and which achieves relationship between [1.5TG/(KpTu)](T0/2Tu –
the desired effect. (Note: the official the control error and Tu/T0 – 1)
term for the control value is ‘manipu- the control value is called the control q2 = 3TG/4KpT0
lated variable’.) algorithm. For a PID controller, this is
A characteristic feature of every a differential equation. The step response of the process must
control system is closed loop opera- The primary difference between a be measured experimentally to obtain
tion, as illustrated in Figure 1. It is nec- digital controller and an analogue con- these values. The parameters must
essary that a given input signal pro- troller is that with a digital controller also satisfy the following conditions:
duces a specific output signal, for both the actual value is not measured con-
the controller and the process. In gen- tinuously, but is instead periodically q0 > 0
eral, the relationship between the sampled at some fixed interval T0. In q1 < 0, |q1| > q0
input an output signals is time- the time between two successive sam- |q0 + q1| < q2 < q0
dependent. ples, the control error must be deter-
The step response is an essential mined and the control value calculated As a guideline for the value of the sam-
characteristic of both processes and using the control algorithm. pling interval T0, it should chosen to be
controllers. It reflects how the output The following equation can be used around one tenth of the time required
signal responds when the input signal as the control algorithm of a PID con- for the process step response to reach
u changes instantaneously from one troller (see the sidebar ‘Digital PID con- 95% of its final value.
level to another level. The waveform troller design’ for the derivation of this
shown in Figure 2 illustrates a step equation): HARDWARE
response that is typical of many AND SOF TWARE
processes. For example, if a given DC uk = u(k–1) + q0ek + q1e(k–1) + q2e(k–2) Figure 4 shows the circuit diagram of
voltage is applied to a resistive heating the controller. Its input is designed for
element, the graph of its temperature In this equation, the index k represents voltages between 0 and 5 V, which can
as a function of time will be similar to the running count of the individual come from any desired type of sensor.
this curve. measurements. This means that e(k–1) The input voltage is passed to the
A controller is also a transfer ele- is the control error for the previous internal A/D converter of the PIC
ment that produces a control value measurement. The control parameters 16C71 via opamp IC5a, which acts as a
(output signal) in response to a control q0 through q2 can be approximately buffer. The low-pass filter formed by
error (input signal). Depending on the determined from the characteristic R8 and C8 at the output of the opamp
way in which the controller responds, it parameters of the process step keeps high-frequency interference
can be classified as a proportional, inte- response using the following formulas: from reaching the A/D input (RA0 of
gral or derivative controller. In a pro- the PIC).
portional controller (P controller), the q0 = [1.5TG/(KpTu)](1 + Tu/2T0) The controller output provides a
transfer element is in principle nothing
more than an amplifier. Its output sig-
nal changes in proportion to its input Figure 2. Example of a
signal, with the gain being an
adjustable parameter. As the names
2 jump reply.

suggest, the transfer element of an

y (t)
integral controller (I controller) is an
integrator, and the transfer element of a
derivative controller (D controller) is a
differentiator. With the latter types of
controllers, both the time constant and
the gain are adjustable parameters. Tu = delay
A controller that combines all three TG = transition period
types of response, called a PID con- Kp = amplification
troller, has proven its worth as a sort of
‘universal’ controller. As shown in Fig-
ure 3, the control value consists of the
sum of the outputs of the three types
of controllers, which can be assigned Tu TG
individual weighting factors according t=0 990038 - 13
to the desired controller response.

Elektor Electronics 9/99 15

The alarm LED D1 is driven directly
from the RA4 port output, and the D/A
3 P
converter chip and the LCD module
are also connected directly to the out-
put port pins of the PIC. In the input
mode, the same port pins are used for
control value
scanning the contacts of the parameter-
control error
e I u input switches. Except for S2, these are
controller arranged as a 4×4 matrix, with four
port connections on each side. S2 is
used as the SET pushbutton for con-
firming input values; it connects RB0
to earth when actuated.
Figure 3. Block dia- D The power supply is just as simple
gram of a PID con- controller as the rest of the circuit. It consists of a
troller. 990038 - 15
5 V regulator, two electrolytic capaci-
tors and a reverse-polarity protection
diode at the input connector for an
voltage that also ranges from 0 to 5 V. used for the D/A converter. It can be external 9 V mains adapter.
The output current of opamp IC5b is operated from a single 5 V supply and As far as the software is con-
certainly adequate for use with the pre- does not need an external reference. Its cerned, the tables for the predefined
viously-mentioned circuit that simu- external circuitry is limited to C7, display text and the instructions
lates the process to be controlled. For which is necessary to decouple the needed to control the serial EEPROM
controlling a real process, you will nat- supply voltage. In addition, pull-up both take up quite a bit of storage
urally have to provide a suitable power resistors R2 and R3 are connected to space. The mathematical routines for
stage that can be driven by the maxi- the clock and data pins of the EEP- calculating the control quantities also
mum load current (5 mA) of the con- ROM IC (24C01). take up a certain amount of space.
troller output. Furthermore, only a small amount The actual controller program is
IC5b matches the voltage range of of extra hardware is needed around modest by comparison.
the D/A converter to that of the A/D the PIC itself. In addition to the clock The program is protected by a
converter of the PIC. Its gain is set to circuitry with a 4 MHz crystal, there is watchdog timer, which in case of a fail-
1.935 by R10 and R11. a reset circuit which requires only a ure triggers a reset and an alarm (blink-
An 8-bit few passive components. The power- ing LED D1) and also sets the control
Figure 4. Circuit dia- DAC IC (Ana- on reset is provided by R5 and C5, quantity to zero. If this happens, the
gram of the PIC-based log Devices while a manual reset is provided by R6 controller must be restarted, but it is
digital PID controller. AD577) is and S1. not necessary to re-enter the control

5V 5V
4 D1 C4
100n 1%
R5 R1 100n
14 6


11 R7
IC5b 10Ω
2 6 RB0 RB0 1 16 5
1 17 7 RB1 RB1 2 15
R12 IC5a 1k RA0 RB1 D1 SA

3 18 8 RB2 RB2 3 14
10k RA1 IC2 RB2 D2 SB R11
1 9 RB3 RB3 4
RA2 RB3 D3
C8 2 PIC 10 RB4 RB4 5 1%
R13 RA3 RB4 D4

16C71 8V4
3 11 RB5 RB5 6 AD557
RA4 RB5 D5 R9 5V
10n 12 RB6 RB6 7 10
RB6 D6 CS 10k
4 13 RB7 RB7 8 9
8 C11
12 13 IC5 = TLC272 IC5

16 15 5
4 100n

C5 R4
C6 X1
P1 S1 4MHz
K1 10µ
10µ 10V
1 10V 10k C2 C3




3 22p 22p S15 S16 S17 S18
5 C D E F
6 5V 5V
E S11 S12 S13 S14
7 RB0
D0 C1
8 RB1
D1 8 9 A B
9 RB2 +8V4 5V 100n R2 R3 RB2


10 RB3 S7 S8 S9 S10
D3 IC4 8
11 RB4
D4 7805
12 RB5 +9V D2 +5V 1
A0 4 5 6 7
D5 2 5 RB7 RB1
13 RB6 A1 IC1 SDA
D6 1N4001 3 6 RB6 S3 S4 S5 S6
14 RB7 A0 SCL
D7 C9 C10
7 24C01
WP 0 1 2 3
LM16A211 100µ 25V 10µ 10V RB0

990038 - 11

16 Elektor Electronics 9/99

5 Resistors:
R1 = 1kΩ5
R2,R3,R5,R9,R12 = 10kΩ
R4 = 270Ω
R6 = 100Ω
R7 = 10Ω
R8 = 1kΩ
R10 = 9kΩ53 1%
R11 = 10kΩ 1%
K1 P1 R13 = 1MΩ


C1 D1
C6 R7 Capacitors:
C4 C5
C11 C1,C4,C7,C11 = 100nF ceramic
C2,C3 = 22pF ceramic

IC2 R1

R8 C5,C10 = 10µF radial


T C6 = 10µF 10V
C8 C8 = 10nF MKT (Siemens)
R4 T
IC3 C3 R13 C9 = 100µF 26V radial
X1 R12

T Semiconductors:
D1 = LED, red, high efficiency
S15 S16 S17 S18 D2 = 1N4001
0 IC1 = 24C01
C D E F IC4 IC2 = PIC16C71, order code

S11 S12 S13 S14 +5V IC3 = AD557JN (Analog Devices)
IC4 = 7805
8 9 A B IC5 = TL272CP

S7 S8 S9 S10 0
4 X1 = 4MHz quartz crystal
5 6 7 D2
+9V K1 = LM16A211 LCD module,2 x 16
characters (Sharp)
S3 S4 S5 S6 S2 S1 S1-S18 = pushbutton, PCB mount,
Set Reset ITC type D6-R-RD
1 2 3
0 PCB, order code 990038-1


Disk, contains PIC source code,
990038-1 ROTKELE )C(
order code 996003-1

990038-1 (C) ELEKTOR

tage is that the registers that are used

more than once do not have meaning-
ful names.
If you want to carefully study or
modify the program, you will find the
source code on a diskette available
through our Readers Services.

Constructing the circuit on the printed
circuit board does not require any
unusual skills. Since the circuit board is
single-sided, a few jumpers are
unavoidable. Do not overlook them
when stuffing the board.
The keypad module specified in the
list of components must be used for the
parameter input pushbuttons, due to
the internal connections. An unregu-
lated 9 V mains adapter can be used for
the power supply, as long as it can sup-
ply at least 200 mA.
When setting up the board, first
adjust P1 to set the contrast of the dis-
Figure 5. PCB track play for optimum readability.
parameters, since the layout and component than one purpose. In A simple circuit, shown in Figure 7,
values that were previ- mounting plan (board principle, this is not a has been designed for testing the con-
ously stored in the EEP- available ready-made). problem, since (for troller and experimenting with it. This
ROM can be used again. example) the registers circuit simulates the process to be con-
Since the program used for performing trolled. The step response of the
needs a very large number of registers, calculations are never used during process is determined by the time con-
some registers must be used for more parameter input. The only disadvan- stants of three RC low-pass circuits

Elektor Electronics 9/99 17

For 8-bit signed numbers, this results in
a range of values from +127D (7FH) to
6 –128D (80H). Negative 8-bit numbers
can be recognized by the fact that the
MSB is a 1.
The integer and fractional bytes of
q0 through q2 must be entered sepa-
rately (for example, Q0HI and Q0LO
are entered by pressing two keys for
each byte, with SET pressed between
the first and second pair of value-entry
If you use the simulation circuit
shown in Figure 7, you can achieve sat-
isfactory results with the following
parameter values:

q0 = 13.5D = 0D80H
q1 = –18.5D = ED80H
q2 = 6.5D = 0680H
sampling interval = 0.4 s (enter ‘04’)
target value = 80H (approx. 2.5 V)
initial control value = A0H (approx.
3.1 V)
Figure 6. Finished pro-
totype of the digital Since the electrolytic capacitors in the
controller. simulation circuit have quite large tol-
erances, it is possible that the controller
will not operate optimally with the
above values. In this case the values of
q0 through q2 should be empirically
connected in series. The time constants tion ‘INTERVAL’) is determined by The output signal of the controller
of this ‘electronic process’ can thus be multiplying a basic increment of 0.1 s is set to 0 V during parameter entry.
easily changed, and interference can be (fixed in software) by the entered hexa- After parameter entry has been com-
easily simulated by loading the output decimal value. pleted by pressing SET, the control
with the switchable resistor. To use the The parameters q0, q1 and q2, which value (CONTROL VAL) is output at a
simulation circuit, connect its input to are important for the control algo- level equal to its entered initial value
the output of the controller and its out- rithm, are entered as fixed-point num- until the entered target value (SET-
put to the input of the controller. bers, with one byte (two hex charac- VALUE) is reached. After this, the
Control parameter values can be ters) before the decimal point and one actual control process starts up and,
entered only immediately after the cir- byte after the decimal point. The bits depending on the selected control
cuit is switched on or directly follow- after the decimal point are weighted parameter values, more or less exactly
ing a reset. They are entered in hexa- according to 1/2n, where n represents maintains the controlled variable at the
decimal form by means of the keypad. the bit position (MSB = 1, LSB = 8). A set value, even in the presence of inter-
Two characters will be displayed after hex value of 80 after the decimal point ference. During the control process, the
two keys have been pressed in turn. thus corresponds to 1/2 (0.5 decimal). current levels of the control value
The SET button must be pressed for Since q1 is always negative, a short (CONTROL VAL) and the actual value
the entered value to be accepted and explanation of the representation of (ACT.VAL) are continuously displayed.
stored in EEPROM. The target value negative binary numbers is in order. A If either the upper or lower limit
and the initial value of the control negative binary number is formed by value (UPPER LIMIT or LOWER
quantity can be set between 00H and bitwise inverting a positive number LIMIT) is exceeded, the alarm lamp
FFH, which corresponds to a voltage and adding one to the result. An exam- (LED D1) blinks for the duration of
range of 0 to 5 V. The upper and lower ple is: out-of-limits condition. No new values
limit values can also be set between are displayed during this interval. The
00H and FFH. The 13D = 0000 1101B = 0DH controller output goes to zero if the
Figure 7. This little cir- length of the inverted: 1111 0010B = F2H upper limit is exceeded, and to FFH
cuit simulates a sampling interval +1: 1111 0011B = F3H (5 V) if the lower limit is exceeded.
‘process’ for testing (display indica- If the controller is started anew by
the controller. pressing Reset, the parameter entry
process must be run through again by
pressing SET after each parameter
value is displayed. SETVALUE appears
1k 4k7 10k
first on the display, with the last saved
7 value. The values of CONTROL VAL,


INTERVAL follow in turn, each
appearing after SET has been pressed.
100µ 100µ 100µ
The stored values are displayed for
each parameter in turn, and can be
modified if necessary.
990038 - 14

18 Elektor Electronics 9/99

Digital PID controller design
The easiest approach is to start with an analogue controller
designed according to the block diagram shown in Fig- [ ]
∆f (t ) = f (t )− f (t − T ) / T
ure 3, whose control algorithm is given by the following dif-
ferential equation:
in which T is the period of the sampling signal (the sam-

∫e( τ)dτ
pling interval).
u(t ) = K PR ⋅e(t )+ K DR ⋅de(t )/ dt + K IR ⋅ The finite sum is defined as the discrete-time equivalent of
the continuous-time integral of a function. It is given by the
In this equation, KPR, KDR and KIR represent the
(adjustable) gains of the three controllers (P, D and I con-
trollers) shown in Figure 3. This equation can also be rep- b
resented in the frequency domain by applying the Laplace
transform, as follows: ∫ f( τ) = T[f(a +T)+ f(a +2T)+ . . . + f(b)]

u(s) = K PR ⋅e(s)+ K DR ⋅s⋅e(s)+(K ID / s)e(s)

If the relationships shown in formulas 3 and 4 are applied
to the first two equations, the result is the following equation,
where it is assumed that the initial conditions are all zero. which is shown in the article as the control algorithm of a
The gains of the three parts of the controller (often referred digital PID controller:
to as the proportional, integral and derivative coefficients
of the controller) must be set by the designer according to
the desired control response. u k = u(k−1) + q 0⋅e k + q1⋅e(k−1) + q 2⋅e(k−2 )
The next step is to find a discrete equivalent for the con-
troller described by equations 1 and 2. The backwards dif-
ference is defined as the discrete-time equivalent of the Once again, the index k is the running count of the indi-
continuous-time derivative of a function. It is given by the vidual measurements. This means that e(k-2) is the control
formula error for two sample intervals prior to the present mea-


With the passing of the years, duction of his voice. Thus was born the phonograph or, as it is bet-
developments and inventions in ter known in Europe, the gramophone.
the electrical field succeeded each Although the reproduction was not of very good quality, the prin-
other in increasing tempo. In ciple had been established. Ten years later, Emile Berliner
1876, Alexander Graham Bell (1851–1929), another American inventor, produced an improved
(1847–1922) developed the tele- version of the phonograph, which he called ‘gramophone’. In contrast
phone. The sound to be transmit- to Edison’s instrument, which used a variable-resistance transmit-
ted via the telephone line was ter, that of Berliner used a flat disk instead of a rotating cylinder.
picked up by a funnel-shaped The flat disk is, of course, the forerunner of the modern gramo-
horn. The horn was terminated by phone record. In the Berliner system, the grooves on the disk are
a thin metal diaphragm immedi- modulated laterally, whereas those on the Edison cylinder are
ately behind which was a small amplitude-modulated.
coil wound on a bar magnet. In 1879, in America, Edison, and in England, the British physi-
Alexander Graham Bell
When sound entered the horn, the cist and chemist Sir Joseph Wilson Swan (1828–1914) simultane-
diaphragm vibrated and thereby ously introduced the first practical incandescent lamp. It was, per-
varied the distance between the diaphragm and the coil. This caused haps, not so much the improvement of the filament that made the
a voltage to be generated in the coil that alternated in rhythm with incandescent lamp a success as the evacuation of the bulb (in which
the sound signal. When two such devices were linked by a long wire, Swan was aided by Charles Stearn, an expert in the production of
the speaker at one end could bring the diaphragm at the other end high vacua). Edison and his team perfected the filament: after exam-
into vibration. The same device was thus used for speaking and lis- ination of thousands of alternatives, they devised the cotton-thread
tening by alternately bringing it in front of the mouth and against filament which, when joined with the bulb patents of Swan and
the ear. This setup was improved fairly quickly and provided, among Stearn, made the Edison lamp a commercial success.
others, with a different kind of earpiece. Within a few years – in 1878 in 1890, a young Dutch mechanical engineer, Gerard Philips
– the first public telephone network was opened in the United States. (1858–1942), decided to set up
Within a year after the introduction of the telephone, Thomas his own company to produce
Alva Edison (1847-1931) had devised a contraption that consisted incandescent lamps. This
of a wooden cylinder around which was wound a sheet of tinfoil, an resulted in 1891 in the Philips
arm that enabled a needle to be brought into contact with the tin- Gloeilampenfabriek in Eind-
foil, and a crank to rotate the cylinder. Edison set the needle in con- hoven, which soon became
tacts with the tinfoil, rotated the cylinder with the aid of the crank, famous all over the world and is
and into a horn-shaped mouthpiece attached to the needle he sang (or still an important multinational
shouted): “Mary had a little lamb …”. When he turned the cylinder company today.
again, the horn of the instrument produced a recognizable repro- [995073]
Antique phonograph

Elektor Electronics 9/99 19