<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE article  PUBLIC "-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN" "http://dtd.nlm.nih.gov/publishing/3.0/journalpublishing3.dtd"><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="3.0" xml:lang="en" article-type="research article"><front><journal-meta><journal-id journal-id-type="publisher-id">ENG</journal-id><journal-title-group><journal-title>Engineering</journal-title></journal-title-group><issn pub-type="epub">1947-3931</issn><publisher><publisher-name>Scientific Research Publishing</publisher-name></publisher></journal-meta><article-meta><article-id pub-id-type="doi">10.4236/eng.2020.129044</article-id><article-id pub-id-type="publisher-id">ENG-102829</article-id><article-categories><subj-group subj-group-type="heading"><subject>Articles</subject></subj-group><subj-group subj-group-type="Discipline-v2"><subject>Engineering</subject></subj-group></article-categories><title-group><article-title>
 
 
  Implementation of Control Algorithms in Small Embedded Systems
 
</article-title></title-group><contrib-group><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Lars</surname><given-names>Bengtsson</given-names></name><xref ref-type="aff" rid="aff1"><sub>1</sub></xref><xref ref-type="corresp" rid="cor1"><sup>*</sup></xref></contrib></contrib-group><aff id="aff1"><label>1</label><addr-line>Department of Physics, University of Gothenburg, Gothenburg, Sweden</addr-line></aff><pub-date pub-type="epub"><day>07</day><month>09</month><year>2020</year></pub-date><volume>12</volume><issue>09</issue><fpage>623</fpage><lpage>639</lpage><history><date date-type="received"><day>18,</day>	<month>August</month>	<year>2020</year></date><date date-type="rev-recd"><day>11,</day>	<month>September</month>	<year>2020</year>	</date><date date-type="accepted"><day>14,</day>	<month>September</month>	<year>2020</year></date></history><permissions><copyright-statement>&#169; Copyright  2014 by authors and Scientific Research Publishing Inc. </copyright-statement><copyright-year>2014</copyright-year><license><license-p>This work is licensed under the Creative Commons Attribution International License (CC BY). http://creativecommons.org/licenses/by/4.0/</license-p></license></permissions><abstract><p>
 
 
  This work describes how a control algorithm can be implemented in a small (8-bit) microcontroller for the main purpose of merging embedded systems and control theory in electrical engineering undergraduate classes. Two different methods for discretizing the control expression are compared: Euler transformation and bilinear transformation. The sampling rate’s impact on the algorithm is discussed and theoretical results are verified by an application to a temperature control system in a heating plant. Four control algorithms are compared: PID and PI algorithms discretized with Euler and bilinear transformation, respectively. It is shown that for the heating plant used in this work, a bilinear PI algorithm implemented in a small 8-bit microcontroller outperforms a commercial controller from Panasonic. It is also demonstrated that all the derived algorithms can be implemented using integer calculations only, obviating the need for expensive and time-consuming floating-point calculations. This work bridges the gap between control theory equations and the implementation of control systems in small embedded systems with no inherent floating-point processing power.
 
</p></abstract><kwd-group><kwd>Bilinear Transformation</kwd><kwd> Euler Transformation</kwd><kwd> Microcontroller</kwd><kwd> Control System</kwd><kwd> PID Algorithm</kwd><kwd> Temperature Sensor</kwd><kwd> Set Value</kwd><kwd> Process Value</kwd><kwd> Plant</kwd></kwd-group></article-meta></front><body><sec id="s1"><title>1. Introduction</title><p>Control theory and embedded systems are typically treated separately in electrical engineering programs and the implementation of control algorithms in digital computing targets is only “indicated” on a block diagram level. This work presents the details of the implementation of control algorithms in a small microcontroller which at the same time offers a challenging application for an embedded systems class.</p><p>Control systems are one of the most frequently occurring electro-mechanical systems in process industry [<xref ref-type="bibr" rid="scirp.102829-ref1">1</xref>]. Temperature, gas and liquid flow, liquid levels and rotation speeds (revolutions) are only some of the physical quantities that are frequently controlled. “Controlled” refers to the fact that the current process value is measured (by a sensor) and fed back and compared with the intended value (the set value) and the control system’s task is to eliminate the difference between the set value and the process value. This is illustrated in <xref ref-type="fig" rid="fig1">Figure 1</xref>.</p><p>From <xref ref-type="fig" rid="fig1">Figure 1</xref>, we can see that the input to the control algorithm is the error signale (t), i.e. the difference between the set value and the process value. The control algorithm is typically a PI- or a PID-system. P is the proportional band; the smaller P is, the greater is the amplification K<sub>P</sub> (of e(t)). I represents the integrating part of the control algorithm. This part is necessary in order to eliminate “remaining errors” (otherwise the set and process values may differ even in steady state). Finally, D represents the differential part of the algorithm. This part has two objectives; it makes the system more agile to abrupt changes in the set value or perturbations, but most of all, it helps to stabilize the system (all feedback systems run the risk of being instable).</p><p>The transfer functions of a PI and a PID system are represented by Equation (1) and Equation (2), respectively.</p><p>G ( s ) = K P &#215; ( 1 + 1 T I s ) (1)</p><p>G ( s ) = K P &#215; ( 1 + 1 T I s + T D s ) (2)</p><p>In Equation (1) and Equation (2), s is the Laplace variable (s = α + jω), K<sub>P</sub> is the amplification, T<sub>I</sub> is the integration time and T<sub>D</sub> is the derivation time. The {K<sub>P</sub>, T<sub>I</sub>, T<sub>D</sub>} set is referred to as the PID parameters and they need to be determined first; they depend on the physical properties of the process.</p><p>The “process” can be very simple or extremely complex, but most systems are approximated with either a first or a second order system and these models are necessary in order to determine the control parameters. This work is not primarily concerned with finding the PID parameters; the primary concern of this work is how to implement expressions such as Equation (1) and Equation (2) into a small embedded system. Having said that, control parameters are typically derived either by rule of thumb [<xref ref-type="bibr" rid="scirp.102829-ref2">2</xref>], by examining the step response [<xref ref-type="bibr" rid="scirp.102829-ref3">3</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref4">4</xref>] or by more advanced system identification methods [<xref ref-type="bibr" rid="scirp.102829-ref5">5</xref>].</p><p>In this work, Ziegler-Nichols’ step response method was applied in order to derive the PID parameters [<xref ref-type="bibr" rid="scirp.102829-ref6">6</xref>]. First, the open-loop step response of the process is registered, see <xref ref-type="fig" rid="fig2">Figure 2</xref>.</p><p>In a digital solution, y is the output from an n<sub>y</sub>-bit ADC (Analog-to-Digital Converter), i.e. an integer in the range 0 and 2 n y − 1 . Correspondingly, the input step signal u is the output from an n<sub>u</sub>-bit DAC (Digital-to-Analog Converter):</p><p>an integer in the range 0 ⋯ 2 n u − 1 . If a stepwise input change from u<sub>0</sub> to u<sub>∞</sub> results in an output change from y<sub>0</sub> to y<sub>∞</sub>, the open-loop gain is</p><p>K = y ∞ − y 0 u ∞ − u 0 (3)</p><p>L and T in <xref ref-type="fig" rid="fig2">Figure 2</xref> are the inherent deadtime and the time constant of the process, respectively. These parameters must be measured in a step response experiment and the parameter set {K, L, T} is used to find the appropriate PID parameters according to <xref ref-type="table" rid="table1">Table 1</xref> [<xref ref-type="bibr" rid="scirp.102829-ref6">6</xref>].</p>Review of Previous/Related Works<p>Implementation of control algorithms in single-chip microcontrollers has been reported ever since they were introduced on the market. All ranges and brands have been used. For example, 8-bit microcontrollers were used in [<xref ref-type="bibr" rid="scirp.102829-ref7">7</xref>] - [<xref ref-type="bibr" rid="scirp.102829-ref12">12</xref>], Aslam et al. used a 16-bit PIC controller [<xref ref-type="bibr" rid="scirp.102829-ref13">13</xref>] and Krivic et al. [<xref ref-type="bibr" rid="scirp.102829-ref14">14</xref>] and Arzak et al. [<xref ref-type="bibr" rid="scirp.102829-ref15">15</xref>] used 32-bit ARM controllers. A lot of PID implementations have been reported in FPGAs, either using soft CPUs like Xlinix’s Pico Blaze [<xref ref-type="bibr" rid="scirp.102829-ref16">16</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref17">17</xref>] or Altera’s Nios processor [<xref ref-type="bibr" rid="scirp.102829-ref18">18</xref>]. Direct implementations in embedded hardware, such as FPGAs [<xref ref-type="bibr" rid="scirp.102829-ref18">18</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref19">19</xref>] and FPAAs [<xref ref-type="bibr" rid="scirp.102829-ref20">20</xref>] have also been reported.</p><p>As expected, the most common control implementations are concerned with temperature [<xref ref-type="bibr" rid="scirp.102829-ref7">7</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref18">18</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref21">21</xref>] and DC engines [<xref ref-type="bibr" rid="scirp.102829-ref8">8</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref9">9</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref10">10</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref13">13</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref16">16</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref17">17</xref>], but water level [<xref ref-type="bibr" rid="scirp.102829-ref14">14</xref>], DC-DC output voltage [<xref ref-type="bibr" rid="scirp.102829-ref12">12</xref>] and position control of magnetically levitated balls [<xref ref-type="bibr" rid="scirp.102829-ref11">11</xref>] have been reported. One of the most salient works is the implementation of a microcontroller PID system for controlling the movement of a wheelchair by EEG waves (for quadriplegic patients) [<xref ref-type="bibr" rid="scirp.102829-ref15">15</xref>].</p><table-wrap id="table1" ><label><xref ref-type="table" rid="table1">Table 1</xref></label><caption><title> PID parameters from step response</title></caption><table><tbody><thead><tr><th align="center" valign="middle"  rowspan="2"  >Controller</th><th align="center" valign="middle"  colspan="3"  >PID parameters</th></tr></thead><tr><td align="center" valign="middle" >K<sub>P</sub></td><td align="center" valign="middle" >T<sub>I</sub></td><td align="center" valign="middle" >T<sub>D</sub></td></tr><tr><td align="center" valign="middle" >PI</td><td align="center" valign="middle" >0.9T/KL</td><td align="center" valign="middle" >3L</td><td align="center" valign="middle" ></td></tr><tr><td align="center" valign="middle" >PID</td><td align="center" valign="middle" >1.2T/KL</td><td align="center" valign="middle" >2L</td><td align="center" valign="middle" >L/2</td></tr></tbody></table></table-wrap><p>In all reported works, about half of them implemented a straightforward Euler-transformed PID (Equation (9) below) [<xref ref-type="bibr" rid="scirp.102829-ref7">7</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref10">10</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref13">13</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref14">14</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref18">18</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref19">19</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref21">21</xref>] and some used Fuzzy logic algorithms [<xref ref-type="bibr" rid="scirp.102829-ref8">8</xref>] [<xref ref-type="bibr" rid="scirp.102829-ref14">14</xref>]. Kheriji suggested a Model Predicted Control algorithm which is an extension to the Euler-transformed PID [<xref ref-type="bibr" rid="scirp.102829-ref22">22</xref>].</p><p>In spite of an extensive data base search, no works have been found that utilize bilinear transformation of control algorithms; Euler transformation is apparently prevalent. This work remediates this gap and demonstrates the advantages of bilinear transformation. There is also a paucity of implementation details in previous works; exactly how are control equations translated into C code (without floating-point support) and exactly how is the hardware details designed. For example, how are the algorithm details affected by analogue-to-digital and digital-to-analogue converters? This work will cover all these implementation details.</p></sec><sec id="s2"><title>2. Method and Material</title><sec id="s2_1"><title>2.1. Theory</title><p>The problem addressed in this work is how control equations, such as those represented by Equation (1) and Equation (2), are best implemented in a small, 8-bit microcontroller (non-digital signal-processing chip). Two methods are proposed and compared: Euler transformation and bilinear transformation.</p><sec id="s2_1_1"><title>2.1.1. Euler Transformation</title><p>In Equation (1) and Equation (2), G(s) represents the transfer function from e(t) to u(t) in <xref ref-type="fig" rid="fig1">Figure 1</xref> (i.e. U(s)/E(s)). Taking the inverse Laplace transform of expressions (1) and (2) gives us the time domain signals:</p><p>u ( t ) = K P &#215; ( e ( t ) + 1 T I ∫ e ( t ) d t ) (4)</p><p>u ( t ) = K P &#215; ( e ( t ) + 1 T I ∫ e ( t ) d t + T D &#215; e ′ ( t ) ) (5)</p><p>In <xref ref-type="fig" rid="fig3">Figure 3</xref>, the error signal e(t) has been sampled with a sampling time T<sub>S</sub>.</p><p>From <xref ref-type="fig" rid="fig3">Figure 3</xref>, it is obvious that the integral of e(t) can be approximated by the sum of the indicated rectangles, i.e.</p><p>∫ e ( t ) d t ≈ ∑ i e ( i ) &#215; T S = T S ∑ i e ( i ) . (6)</p><p>Correspondingly, in <xref ref-type="fig" rid="fig4">Figure 4</xref>, we can see that the derivative of e(t) at some point may be approximated by the gradient of the straight line between two adjacent samples, i.e.</p><p>e ′ ( t ) ≈ e ′ ( n ) = e ( n ) − e ( n − 1 ) T S . (7)</p><p>Hence, when we discretize the control algorithms (4) and (5) using Euler transformations, we get the following expressions:</p><p>u ( n ) = K P &#215; ( e ( n ) + T S T I ∑ i = 0 n e ( i ) ) (8)</p><p>u ( n ) = K P &#215; ( e ( n ) + T S T I ∑ i = 0 n e ( i ) + T D T S ( e ( n ) − e ( n − 1 ) ) ) (9)</p><p>Expressions (8) and (9) are “computer-friendly” expressions that can be readily implemented in an embedded system. Notice how the sampling time becomes a crucial design parameter.</p></sec><sec id="s2_1_2"><title>2.1.2. Bilinear Transformation</title><p>In the Euler transform, we first apply the inverse (Laplace) transform and then discretize the time. In the bilinear transformation, we do exactly the opposite; we first transform the continuous-frequency expressions (1) and (2) to the discrete-frequency domain and then apply the inverse transformation to get the time-expressions for the computer algorithm.</p><p>The discrete-time correspondence to the Laplace transform is the z transform [<xref ref-type="bibr" rid="scirp.102829-ref23">23</xref>]. In the z transform, the frequency variable is the normalized frequency variable Ω (normalized to the sampling rate):</p><p>Ω = ω f S = ω &#215; T S (10)</p><p>Since ω is limited to frequencies &lt; 2π &#215; f<sub>S</sub>/2, due to the sampling theorem [<xref ref-type="bibr" rid="scirp.102829-ref24">24</xref>], Ω is limited to &#177;π (z transforms are periodic with period 2π to represent the periodicity of sampled signals in frequency space).</p><p>Hence, a transformation from continuous-time to discrete-time becomes cumbersome, since only a limited number of frequencies can be carried over to a discrete-time representation. To overcome this, we have to “squeeze” in all frequencies in the continuous time-domain, from –∞ to +∞, into the &#177;π interval in the z domain. The following expression accomplishes this:</p><p>Ω = 2 &#215; arctan ( k ω ) (11)</p><p>Equation (11) maps ω to Ω as:</p><p>ω = − ∞ → Ω = − π ;</p><p>ω = 0 → Ω = 0 ;</p><p>ω = + ∞ → Ω = + π .</p><p>This is illustrated in <xref ref-type="fig" rid="fig5">Figure 5</xref>.</p><p>In Equation (11), we solve for ω (and multiply both sides by the imaginary number j):</p><p>j ω = j 1 k tan ( Ω 2 ) = 1 k &#215; j &#215; sin Ω 2 cos Ω 2 = 1 k &#215; 1 2 ( e j Ω / 2 − e − j Ω / 2 ) 1 2 ( e j Ω / 2 + e − j Ω / 2 ) = 1 k &#215; e − j Ω / 2 ( e j Ω − 1 ) e − j Ω / 2 ( e j Ω + 1 ) = 1 k &#215; e j Ω − 1 e j Ω + 1 (12)</p><p>For α = 0, s = jω and we can write Equation (12) as</p><p>s = 1 k &#215; z − 1 z + 1 (13)</p><p>where z = e s T s = e ( α + j ω ) T s = e j Ω if α = 0. Equation (13) represents the classic bilinear transformation that transfers a Laplace transform expression into the corresponding z transform expression.</p><p>However, we are not quite done yet; we need to determine the constant k in Equation (13). We determine it so that we get a good mapping for the lowest frequencies; if Ω is “small” then, tan (Ω/2) ≈ Ω/2 and expression (12) is now</p><p>ω = 1 k &#215; Ω 2 (14)</p><p>Solving for k, and remembering that Ω is the normalized frequency variable, we get:</p><p>k = Ω 2 ω = ω T S 2 ω = T S 2 (15)</p><p>Combining Equation (13) and Equation (15), we finally get our transformation expression:</p><p>s = 2 T S &#215; z − 1 z + 1 (16)</p><p>This is the substitution we need to do in expressions (1) and (2) to translate the control frequency functions to discrete-time (sampled) space. We do Equation (1) first:</p><p>G ( z ) = K P &#215; ( 1 + 1 2 T I T S z − 1 z + 1 ) = K P ( 1 + T S 2 T I z + 1 z − 1 ) = K P ( 2 T I 2 T I z − 1 z − 1 + T S 2 T I z + 1 z − 1 ) = K P 2 T I ( 2 T I ( z − 1 ) + T S ( z + 1 ) ) &#215; 1 z − 1 = K P 2 T I ( z ( 2 T I + T S ) − ( 2 T I − T S ) ) &#215; 1 z − 1 = K P 2 T I ( ( 2 T I + T S ) − ( 2 T I − T S ) z − 1 ) &#215; 1 1 − z − 1 (17)</p><p>Equation (1) represents the transfer function from e(t) to u(t) in <xref ref-type="fig" rid="fig1">Figure 1</xref>. Hence,</p><p>G ( z ) = U ( z ) E ( z ) (18)</p><p>K P 2 T I E ( z ) ( 2 T I + T S − ( 2 T I − T S ) z − 1 ) = U ( z ) ( 1 − z − 1 ) (19)</p><p>Taking the inverse z transform of Equation (19), and remembering that in the z transform, z<sup>−m</sup> represents a time delay of m sample times ( [<xref ref-type="bibr" rid="scirp.102829-ref25">25</xref>], p. 345), we get the difference equation we are looking for:</p><p>u n = u n − 1 + K P 2 T I ( ( 2 T I + T S ) e n − ( 2 T I − T S ) e n − 1 ) (20)</p><p>Notice an important difference between the Euler expression (8) and the bilinear expression (20); in Equation (20) we need old samples of the control signal (u<sub>n</sub><sub>−1</sub>). This implies poles in the z plane, i.e. the system could become instable (if the poles are outside the unit circle in the z plane). With the Euler transform, we always get inherently stable systems (no poles).</p><p>Next, we need to do the same substitution in Equation (2). This will give us the following expression:</p><p>u n = u n − 2 + K P 2 T I T S { ( 2 T I T S + T S 2 + 4 T I T D ) e n + ( 2 T S 2 − 8 T I T D ) e n − 1     + ( 4 T I T D + T S 2 − 2 T I T S ) e n − 2 } (21)</p></sec></sec><sec id="s2_2"><title>2.2. Hardware</title><p>The hardware setup is illustrated in <xref ref-type="fig" rid="fig6">Figure 6</xref>. The heart of the control system is a microcontroller from Microchip; the 20-pin, 8-bit PIC16F1769 mid-range controller [<xref ref-type="bibr" rid="scirp.102829-ref26">26</xref>] (list price is $1.92). The process to control (the “plant”) is the temperature of a power resistor. Its temperature is measured by a temperature sensor, LM35 [<xref ref-type="bibr" rid="scirp.102829-ref27">27</xref>], and fed back to the control algorithm. The sensor’s sensitivity is 10 mV/˚C and its signal is amplified 10 times by a non-inverting amplifier (taking advantage of an op amp embedded into the microcontroller). The output</p><p>is connected to one of the analog input channels; this represents the process value and since the maximum allowed input is +5 volts, the temperature range is 0..50˚C.</p><p>The output from the control algorithm is sent to a pulse width modulated (PWM) output (period = 4.4 seconds). This output is connected to a semiconductor relay [<xref ref-type="bibr" rid="scirp.102829-ref28">28</xref>] that controls the dc power source (V<sub>p</sub>) that heats the plant resistor. The V<sub>P</sub> voltage was adjusted so that a PWM duty cycle of 100% would correspond to a plant temperature of +50˚C; V<sub>P</sub> = 8.0 volts. The set value is adjusted by a 10-turn potentiometer connected to one of the analog inputs (AN2) and the plant temperature is registered on analog channel AN3.</p></sec><sec id="s2_3"><title>2.3. Software</title><p>The PWM duty cycle value can only be updated at the end of each period and hence it makes no sense to read the set and plant temperatures more often than the PWM period (i.e. the sampling time equals the PWM period). The software was designed to generate an interrupt at the end of each PWM period and the control algorithms were implemented in the ISR (interrupt service routine; the software is background/foreground oriented ( [<xref ref-type="bibr" rid="scirp.102829-ref29">29</xref>], pp. 83-84)) and the ISR is illustrated in <xref ref-type="fig" rid="fig7">Figure 7</xref>.</p><p>The heart of the algorithm is the calculation of the new output value and the update of the old samples. This part of the C code is illustrated below.</p><p>u_temp = u2 + 5892*e−7154*e1 + 2172*e2; //calculate new</p><p>if (u_temp &gt; 65535) //limit the output to</p><p>u_temp = 65535; //a 16-bit range</p><p>else if (u_temp &lt; 0)</p><p>u_temp = 0;</p><p>u = (unsigned int)u_temp; //type conversion</p><p>PWM6_DutyCycleSet(u);</p><p>PWM6_LoadBufferSet(); //Update output</p><p>e2 = e1; //Update old samples</p><p>e1 = e;</p><p>u2 = u1;</p><p>u1 = u;</p><p>The program was developed in Microchip’s MPLABX<sup>&#210;</sup> IDE using the XC8 C compiler. The microcontroller ran at 16 MHz and the first C code line above (calculating the new output value), was timed in the simulator to take 52 μs.</p></sec><sec id="s2_4"><title>2.4. Step Response</title><p>The open-loop step response was measured using two analog channels on a Tektronix MSO2012B oscilloscope. Since the relay that controls the plant is controlled by a PWM signal, an input step corresponds to a step change in the PWM signal’s duty cycle. The open-loop step response was recorded (on pin 3/14 in <xref ref-type="fig" rid="fig6">Figure 6</xref>) by the oscilloscope and data was transferred to MATLAB via a USB</p><p>memory stick. Graphs were plotted in MATLAB to facilitate a convenient estimation of the K, T and L parameters in <xref ref-type="fig" rid="fig2">Figure 2</xref> and <xref ref-type="table" rid="table1">Table 1</xref>. The input step change was generated by changing the PWM duty cycle from 20% to 60%.</p></sec><sec id="s2_5"><title>2.5. Performance/Verification</title><p>The performance of the derived control algorithms was evaluated by registering the control system’s response to 1) a positive step change in the set value (from 30˚C to 40˚C), 2) a negative step change in the set value (from 40˚C to 30˚C) and finally, 3) a perturbation test where the plant was subjected to an instant cooling perturbation (using cold spray). All responses were observed for 17 minutes.</p><p>Also, in order to benchmark our control algorithms, their performance was compared to that of a commercial temperature controller from Panasonic (KT4) [<xref ref-type="bibr" rid="scirp.102829-ref30">30</xref>]. The Panasonic controller controlled the temperature of the same power resistor. The only difference in the setup was that the Panasonic controller only accepts thermocouples or Pt-100 temperature sensors as input (a type K thermocouple was used); the thermocouple was attached to the power resistor (but the LM35 resistor was not removed, it was used to register the temperature on the oscilloscope). Other than that, the exact same setup was used except that the relay in <xref ref-type="fig" rid="fig6">Figure 6</xref> was removed; the Panasonic controller also has a PWM output with an internal relay. The Panasonic controller was first set to “autotuning mode” where it evaluates the connected systems and finds its own PID parameters (by some built-in rules of thumb).</p></sec></sec><sec id="s3"><title>3. Results</title><sec id="s3_1"><title>3.1. Control Parameters</title><p><xref ref-type="fig" rid="fig8">Figure 8</xref> illustrates the open-loop step response when the input PWM duty cycle (u(t)) changes instantly from 20% to 60%. (In <xref ref-type="fig" rid="fig8">Figure 8</xref>, the step signal is the output from an auxiliary I/O pin that only marks the start of the step change in the PWM signal; its amplitude has been manipulated in MATLAB to fit the graph).</p><p>From <xref ref-type="fig" rid="fig8">Figure 8</xref>, we can see that the output changes from 3.03 volts to 3.95 volts. For a 10-bit ADC, with a reference voltage of 5.0 volts, that corresponds to integers</p><p>3.03 &#215; 2 10 5.0 = 621 and 3.95 &#215; 2 10 5.0 = 809</p><p>The PWM input signal has a resolution of 16 bits and hence a duty cycle change from 20% to 60% corresponds to integers</p><p>0.2 &#215; 2 16 = 13107 and 0.6 &#215; 2 16 = 39322</p><p>From Equation (3), we get the open-loop amplification:</p><p>K = 809 − 621 39322 − 13107 = 0.0071</p><p>Also, from close inspection of <xref ref-type="fig" rid="fig8">Figure 8</xref>, we get L = 9 seconds and T = 100 seconds. Finally, combining these parameters with <xref ref-type="table" rid="table1">Table 1</xref> and expressions (8), (9), (20) and (21), we get four different control algorithms according to <xref ref-type="table" rid="table2">Table 2</xref>. (Notice that <xref ref-type="table" rid="table2">Table 2</xref> indicates that only integer operations are required).</p><table-wrap id="table2" ><label><xref ref-type="table" rid="table2">Table 2</xref></label><caption><title> Suggested control algorithms</title></caption><table><tbody><thead><tr><th align="center" valign="middle" >Controller</th><th align="center" valign="middle" >u<sub>n</sub></th></tr></thead><tr><td align="center" valign="middle" >Euler, PI</td><td align="center" valign="middle" >1395 e n + 277 &#215; ∑ i = 0 n e i</td></tr><tr><td align="center" valign="middle" >Euler, PID</td><td align="center" valign="middle" >1860 e n + 455 &#215; ∑ i = 0 n e i + 1902 &#215; ( e n − e n − 1 )</td></tr><tr><td align="center" valign="middle" >Bilinear, PI</td><td align="center" valign="middle" >u n − 1 + 1509 e n − 1281 e n − 1</td></tr><tr><td align="center" valign="middle" >Bilinear, PID</td><td align="center" valign="middle" >u n − 2 + 5892 e n − 7154 e n − 1 + 2172 e n − 2</td></tr></tbody></table></table-wrap></sec><sec id="s3_2"><title>3.2. Performance, Microcontroller</title><p>Figures 9(a)-(d) illustrate the (positive) step responses for each one of the four controllers presented in <xref ref-type="table" rid="table2">Table 2</xref>.</p><p>In the Euler case, the PID algorithm appears to be slightly better than the PI algorithm; both oscillate a little in the steady state, but the oscillation amplitude is slightly less in the PID case (≈1˚C).</p><p>For the algorithms based on bilinear transformation, the PID algorithm is outperformed by the PI algorithm. Overall, the bilinear PI algorithm exhibits the best performance; it has the same steady-state oscillation as the Euler PID algorithm, but much smaller initial overshoot.</p><p><xref ref-type="fig" rid="fig1">Figure 1</xref>0 below illustrates the negative step response of the bilinear PI algorithm and <xref ref-type="fig" rid="fig1">Figure 1</xref>1 illustrates the perturbation response.</p></sec><sec id="s3_3"><title>3.3. Performance, Panasonic Controller</title><p>After the autotuning was completed, the Panasonic PID parameters were set to P = 22.2˚C, I = 34 seconds and D = 8 seconds. Figures 12-14 below illustrate the positive and negative step responses and the perturbation response of the Panasonic controller.</p><p>Figures 12-14 should primarily be compared to <xref ref-type="fig" rid="fig9">Figure 9</xref>(c), <xref ref-type="fig" rid="fig1">Figure 1</xref>0 and <xref ref-type="fig" rid="fig1">Figure 1</xref>1, which illustrate the corresponding performance of the bilinear PI algorithm.</p></sec></sec><sec id="s4"><title>4. Discussion</title><p>From Figures 9-11, it must be concluded that the bilinear PI algorithm exhibits the overall best performance of all the four suggestions in <xref ref-type="table" rid="table2">Table 2</xref>. From Figures 12-14,</p><p>it is also clear that it performs even better than a commercial temperature controller from Panasonic.</p><p>In all controllers, the plant temperature oscillates a little in steady state. In <xref ref-type="fig" rid="fig9">Figure 9</xref>(c), the amplitude of the oscillations seems to increase with time. For that reason, the output of this controller was carefully monitored for two hours and the oscillations petered out almost completely.</p><p>The first version of this design did not work as expected; the microcontroller was not able to control the temperature. After extensive debugging, the error was found in the software; the delay between the sampling of AN2 and AN3 was inserted. According to the datasheet ( [<xref ref-type="bibr" rid="scirp.102829-ref26">26</xref>], pp. 185-186), there is only one ADC; all channels use the same sample-and-hold circuit and an analog multiplexer forwards the analog channels to the ADC. Hence, the shared sample-and-hold capacitor must be allowed time to charge/de-charge between channel switching and for that reason a delay needs to be inserted between readings of different channels. (10 ms is an overkill but compared to the system’s time constant it is negligible).</p></sec><sec id="s5"><title>5. Conclusion</title><p>This work has presented detailed instructions on how to implement PI and PID algorithms into small embedded systems and it has been proven that a bilinear transformation of the control expressions yields a better controller performance than the general modus operandi of Euler transformations. The reason why a bilinear transformation has better performance than the Euler transformation lies probably in the fact that the bilinear transformation produces algorithms with feedback; they utilize previous samples of the output signal. Herein lies also the weakness; control algorithms derived by bilinear transformation may become inherently unstable (which cannot happen with Euler algorithms). This work has also demonstrated that a complete controller can be implemented in an inexpensive, analog/digital hybrid 8-bit microcontroller, costing less than $2. That will make it less expensive than any other implementations in ARM- or FPGA-based solution and in terms of performance, it has been demonstrated that it can outperform a commercial desktop controller. This also makes it readily available for undergraduate classes in electrical engineering. Here, the application was a temperature plant, but the same system has been successfully implemented also on a water level plant and the fact that the arithmetic is based on integer calculations indicate that it should not be a problem to apply it also to DC motor control. The fact that only integer calculations are required (see <xref ref-type="table" rid="table2">Table 2</xref>) eliminates the need for expensive floating-point processors.</p></sec><sec id="s6"><title>Conflicts of Interest</title><p>The author has no conflicts of interest to report.</p></sec><sec id="s7"><title>Cite this paper</title><p>Bengtsson, L. (2020) Implementation of Control Algorithms in Small Embedded Systems. Engineering, 12, 623-639. https://doi.org/10.4236/eng.2020.129044</p></sec></body><back><ref-list><title>References</title><ref id="scirp.102829-ref1"><label>1</label><mixed-citation publication-type="other" xlink:type="simple">Harris, T., Seppala, C. and Desborough, L. (1999) A Review of Performance Monitoring and Assessment Techniques for Univariate and Multivariate Control Systems. Journal of Process Control, 9, 1-17.  
https://doi.org/10.1016/S0959-1524(98)00031-6</mixed-citation></ref><ref id="scirp.102829-ref2"><label>2</label><mixed-citation publication-type="other" xlink:type="simple">Ziegler, J. and Nichols, N. (1942) Optical Settings for Automatic Controllers. Transactions of the American Society of Mechanical Engineers, 64, 759-768.</mixed-citation></ref><ref id="scirp.102829-ref3"><label>3</label><mixed-citation publication-type="other" xlink:type="simple">Ahmed, S., Huang, B. and Shah, S. (2007) Novel Identification Method from Step Response. Control Engineering Practice, 15, 545-556.  
https://doi.org/10.1016/j.conengprac.2006.10.005</mixed-citation></ref><ref id="scirp.102829-ref4"><label>4</label><mixed-citation publication-type="other" xlink:type="simple">Bi, Q., Cai, W.-J., Lee, E.-L., Wang, Q.-G., Hang, C.-C. and Zhang, Y. (1999) Robust Identification of First-Order plus Dead-Time Model from Step Response. Control Engineering Practice, 7, 71-77. https://doi.org/10.1016/S0967-0661(98)00166-X</mixed-citation></ref><ref id="scirp.102829-ref5"><label>5</label><mixed-citation publication-type="other" xlink:type="simple">Ljung, L. (1999) System Identification. Prentice-Hell, New Jersey. 
https://doi.org/10.1002/047134608X.W1046</mixed-citation></ref><ref id="scirp.102829-ref6"><label>6</label><mixed-citation publication-type="other" xlink:type="simple">Basilo, J. and Matos, S. (2002) Design of PI and PID Controllers with Transient Performance Specification. IEEE Transactions on Education, 45, 364-370. 
https://doi.org/10.1109/TE.2002.804399</mixed-citation></ref><ref id="scirp.102829-ref7"><label>7</label><mixed-citation publication-type="other" xlink:type="simple">Chesaru, V., Dan, C. and Bodea, M. (2006) PID Algorithm for Controller Processors. 2006 International Semiconductor Conference, Sinaia, 27-29 September 2006, 429-432. https://doi.org/10.1109/SMICND.2006.284037</mixed-citation></ref><ref id="scirp.102829-ref8"><label>8</label><mixed-citation publication-type="other" xlink:type="simple">Sarin, S., Hindersah, H. and Prihatmanto, A. (2012) Fuzzy PID Controllers Using 8-Bit Microcontroller for U-Board Speed Control. 2012 International Conference on System Engineering and Technology, Bandung, 11-12 September 2012, 1-6.  
https://doi.org/10.1109/ICSEngT.2012.6339355</mixed-citation></ref><ref id="scirp.102829-ref9"><label>9</label><mixed-citation publication-type="other" xlink:type="simple">Uzunovic, T., Zunic, E., Badnjevic, I., Miokovic, I. and Konjicia, S. (2010) Implementation of Digital PID Controller. The 33rd International Convention MIPRO, Opatija, 24-28 May 2010, 1357-1361.</mixed-citation></ref><ref id="scirp.102829-ref10"><label>10</label><mixed-citation publication-type="other" xlink:type="simple">Xu, C., Huang, D.G., Huang, Y.P. and Gong, S.G. (2008) Digital PID Controller for Brushless DC Motor Based on AVR Microcontroller. 2008 IEEE International Conference on Mechatronics and Automation, Takamatsu, 5-8 August 2008, 247-252.</mixed-citation></ref><ref id="scirp.102829-ref11"><label>11</label><mixed-citation publication-type="other" xlink:type="simple">Kalúz, M., Klauco, M. and Kvasnica, M. (2015) Real-Time Implementation of a Reference Governor on the Arduino Microcontroller. 2015 20th International Conference on Process Control, Strbske Pleso, 9-12 June 2015, 350-356.  
https://doi.org/10.1109/PC.2015.7169988</mixed-citation></ref><ref id="scirp.102829-ref12"><label>12</label><mixed-citation publication-type="other" xlink:type="simple">Boudreaux, R.R., Nelms, R.M. and Hung, J.Y. (1997) Simulation and Modeling of a DC-DC Converter by an 8-Bit Microcontroller. Proceedings of APEC 97—Applied Power Electronics Conference, Atlanta, 27 February 1997, 963-969.</mixed-citation></ref><ref id="scirp.102829-ref13"><label>13</label><mixed-citation publication-type="other" xlink:type="simple">Aslam, S., Hannan, S., Sajjad, U. and Zafar, W. (2016) Implementation of PID on PIC24F Series Microcontroller for Speed Control of a DC Motor Using MPLAB and Proteus. Advances in Science and Technology Research Journal, 10, 40-50.  
https://doi.org/10.12913/22998624/64060</mixed-citation></ref><ref id="scirp.102829-ref14"><label>14</label><mixed-citation publication-type="other" xlink:type="simple">Krivic, S., Hujdur, M., Mrzic, A. and Konjicija, S. (2012) Design and Implementation of Fuzzy Controller on Embedded Computer for Water Level Control. 2012 Proceedings of the 35th Convention MIPRO, Opatija, 21-25 May 2012, 1747-1751.</mixed-citation></ref><ref id="scirp.102829-ref15"><label>15</label><mixed-citation publication-type="other" xlink:type="simple">Arzak, M., Sunarya, U. and Hadiyoso, S. (2016) Design and Implementation of Wheelchair Controller Based on Electroencephalogram Sinal Using Micronctroller. International Journal of Electrical and Computer Engineering, 6, 2878-2886.</mixed-citation></ref><ref id="scirp.102829-ref16"><label>16</label><mixed-citation publication-type="other" xlink:type="simple">Das, A. and Banerjee, K. (2009) Fast Prototyping of a Digital PID Controller on a FPGA Based Soft-Core Microcontroller for Precision Control of a Brushed DC Servo Motor. 2009 35th Annual Conference on IEEE Industrial Electronics, Porto, 3-5 November 2009, 2825-2830. https://doi.org/10.1109/IECON.2009.5415406</mixed-citation></ref><ref id="scirp.102829-ref17"><label>17</label><mixed-citation publication-type="other" xlink:type="simple">Youness, H., Moness, M. and Khaled, M. (2014) MPSoCs and Multicore Microcontrollers for Embedded PID Control: A Detailed Study. IEEE Transactions on Industrial Informatics, 10, 2122-2133. https://doi.org/10.1109/TII.2014.2355036</mixed-citation></ref><ref id="scirp.102829-ref18"><label>18</label><mixed-citation publication-type="other" xlink:type="simple">Chan, Y., Moallem, M. and Wang, W. (2007) Design and Implementation of Modular FPGA-Based PID Controllers. IEEE Transactions on Industrial Electronics, 54, 1898-1906. https://doi.org/10.1109/TIE.2007.898283</mixed-citation></ref><ref id="scirp.102829-ref19"><label>19</label><mixed-citation publication-type="other" xlink:type="simple">Gosh, S., Barai, R., Bhattarcharya, S., Bhattarcharya, P., Rudra, S., Dutta, A. and Pyne, R. (2013) An FPGA Based Implementation of a Flexible Digital PID Controller for Motion Control System. 2013 International Conference on Computer Communication and Informatics, Coimbatore, 4-6 January 2013, 1-6.  
https://doi.org/10.1109/ICCCI.2013.6466277</mixed-citation></ref><ref id="scirp.102829-ref20"><label>20</label><mixed-citation publication-type="other" xlink:type="simple">Lita, I., Visan, A. and Cioc, I. (2009) FPAA Based PID Controller with Application in the Nuclear Domain. 2009 32nd International Spring Seminar of Electronics Technology, Brno, 13-17 May 2009, 1-4. 
https://doi.org/10.1109/ISSE.2009.5206979</mixed-citation></ref><ref id="scirp.102829-ref21"><label>21</label><mixed-citation publication-type="other" xlink:type="simple">Moallem, M. (2004) A Laboratory Testbed for Embedded Computer Control. IEEE Transactions on Education, 47, 340-347. https://doi.org/10.1109/TE.2004.825054</mixed-citation></ref><ref id="scirp.102829-ref22"><label>22</label><mixed-citation publication-type="other" xlink:type="simple">Kheriji, A., Bouani, F., Ksouri, M. and Ahmed, M. (2011) A Microcontroller Implementation of Model Predictive Control. International Journal of Electrical, Computer, Energetic, Electronic and Communication Engineering, 5, 600-606.</mixed-citation></ref><ref id="scirp.102829-ref23"><label>23</label><mixed-citation publication-type="other" xlink:type="simple">Bohner, M. and Peterson, A. (2002) Laplace Transform and Z-Transform: Unification and Extension. Methods and Applications of Analysis, 9, 151-158.  
https://doi.org/10.4310/MAA.2002.v9.n1.a6</mixed-citation></ref><ref id="scirp.102829-ref24"><label>24</label><mixed-citation publication-type="other" xlink:type="simple">Shannon, C. (1949) Communication in the Presence of Noise. Proceedings of the Institute of Radio Engineers, 37, 10-21. 
https://doi.org/10.1109/JRPROC.1949.232969</mixed-citation></ref><ref id="scirp.102829-ref25"><label>25</label><mixed-citation publication-type="other" xlink:type="simple">Proakis, J. and Manoakis, D. (2004) Digital Signal Processing. 2nd Edition, Macmillan Publishing Company, New York.</mixed-citation></ref><ref id="scirp.102829-ref26"><label>26</label><mixed-citation publication-type="other" xlink:type="simple">Microchip Technology Inc. (2018) PIC16(L)F1764/5/8/9. Microchip Tech. Inc., Tucson.</mixed-citation></ref><ref id="scirp.102829-ref27"><label>27</label><mixed-citation publication-type="other" xlink:type="simple">Analog Devices (2015) TMP35/TMP36/TMP37 Low Voltage Temperature Sensors. Rev. H ed., Norwood, Maine.</mixed-citation></ref><ref id="scirp.102829-ref28"><label>28</label><mixed-citation publication-type="other" xlink:type="simple">Panasonic, S. (2017) Relays—Polarized Power Relays. ASCTB207E ed., Osaka.</mixed-citation></ref><ref id="scirp.102829-ref29"><label>29</label><mixed-citation publication-type="other" xlink:type="simple">Laplante, P. (2004) Real-Time Systems Design and Analysis. John Wiley &amp; Sons, Inc., Hoboken. https://doi.org/10.1002/0471648299</mixed-citation></ref><ref id="scirp.102829-ref30"><label>30</label><mixed-citation publication-type="other" xlink:type="simple">Panasonic (2020) Panasonic Industrial Automation.  
https://www3.panasonic.biz/ac/na_download/fasys/component/temperature_controller/manual/kt4_manual_e.pdf?f_cd=3614</mixed-citation></ref></ref-list></back></article>