Digital Control of Power Converters Using Arduino and an STM32
Digital Control of Power Converters Using Arduino and an STM32
This concise and accessible guide equips readers with the knowledge
and skills needed to implement digital control algorithms to design effi-
cient and reliable power converters using STM32 microcontrollers.
Through this book, Majid Pakdel covers a range of topics including
digital control theory, switching converters theory, the design and imple-
mentation of control algorithms (such as proportional–integral–deriva-
tive and advanced digital control techniques), programming of STM32
microcontrollers, and interfacing with power electronics components.
He also provides step-by-step tutorials and code examples to help read-
ers understand and implement the concepts in their own projects.
Readers will gain a deep understanding of digital control techniques in
power converters, learn how to program STM32 microcontrollers for
control applications, and be able to design and implement their own
digital control algorithms in power electronics systems. The practical
examples provided in the book will help readers apply the knowledge
gained to real-world projects and improve their skills in developing digi-
tal control systems.
The information within is useful for young professionals and stu-
dents aiming at experimental implementation on a microcontroller
platform of a control algorithm for power converters. To fully benefit
from the practical examples demonstrating digital controller imple-
mentation on the STM32, readers should have a solid understanding of
power switching converter topologies, modeling, and control.
Majid Pakdel earned a bachelor’s degree at Amirkabir University
of Technology in 2004, a master’s degree at Isfahan University of
Technology in 2007, a PhD in electrical power engineering at the
University of Zanjan in 2018, and a further master’s degree in AI and
robotics at Malek Ashtar University of Technology in 2023.
Digital Control of
Power Converters
Using Arduino and an
STM32 Microcontroller
Majid Pakdel
Designed cover image: Microcontrollers, PCB electrical boards, semiconductor micro-
chip technology computers machine, circuit electronics concept hardware pi chip, neat
vector illustration of electronic technology component; Shutterstock.
MATLAB® and Simulink® are trademarks of The MathWorks, Inc. and are used with per-
mission. The MathWorks does not warrant the accuracy of the text or exercises in this book.
This book’s use or discussion of MATLAB® or Simulink® software or related products does
not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical
approach or particular use of the MATLAB® and Simulink® software.
First edition published 2025
by CRC Press
2385 NW Executive Center Drive, Suite 320, Boca Raton FL 33431
and by CRC Press
4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
CRC Press is an imprint of Taylor & Francis Group, LLC
© 2025 Majid Pakdel
Reasonable efforts have been made to publish reliable data and information, but the
author and publisher cannot assume responsibility for the validity of all materials or the
consequences of their use. The authors and publishers have attempted to trace the copy-
right holders of all material reproduced in this publication and apologize to copyright
holders if permission to publish in this form has not been obtained. If any copyright
material has not been acknowledged please write and let us know so we may rectify in
any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other
means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access w ww.
copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC
please contact [email protected]
Trademark notice: Product or corporate names may be trademarks or registered trade-
marks and are used only for identification and explanation without intent to infringe.
ISBN: 978-1-032-89038-8 (hbk)
ISBN: 978-1-032-89137-8 (pbk)
ISBN: 978-1-003-54135-6 (ebk)
DOI: 10.1201/9781003541356
Typeset in Minion
by SPi Technologies India Pvt Ltd (Straive)
Contents
Preface, vii
v
vi ◾ Contents
REFERENCES, 208
INDEX, 209
Preface
vii
CHAPTER 1
1.1 INTRODUCTION
In this chapter, we’ll be showing you how you can design and
implement a digital controller on the Arduino to control or regu-
late a continuous-time system. First, we’ll start off by discussing
the advantages of using digital control as opposed to the tradi-
tional analog control and will then briefly go over the Z-transform
and discretization of continuous-time systems. Then we’ll move
on to the six steps for designing and implementing a digital con-
troller on the Arduino and lastly, we’ll finish with designing a
digital controller for a direct current (DC) motor speed control
system. So, this chapter outline is as follows:
DOI: 10.1201/9781003541356-1 1
2 ◾ Digital Control of Power Converters
FIGURE 1.3 The type II compensator (adds two poles and one zero into
control loop).
Digital Control with Arduino ◾ 5
So, this was just a brief motivation for why you should learn to
implement digital controllers and their advantages over analog
controllers. The rest of this chapter will focus on bridging the gap
between the theory and the practical implementation in much
greater detail.
∫
C ( s ) = c ( t ) e − st dt
−∞
(1.1)
U (s)
C (s) = (1.2)
E (s)
where C(s) is the Laplace transform of c(t), U(s) is the control out-
put, and E(s) is the error input.
In other words, it makes analyzing linear systems and designing
controllers a lot easier. In the continuous-time or analog-domain,
the controller C(s) is derived from a differential equation that
describes the dynamics of the physical system where in most cases,
it is the dynamics of an electrical analog circuit. For example, the
type II compensator used for power supply regulation has the fol-
lowing transfer function as shown in Figure 1.3:
Ve ( s ) 1 + C1R2 s
C (s) = = − (1.3)
Vout ( s ) ( 2 1 1C3 ) s + R1C1C3R2 s2
R C + R
Digital Control with Arduino ◾ 9
Ve 1 V
|Vref =0 → Ve C3 s + =− out (1.5)
Vout 1 R1
+ R2
C1s
Ve 1 + C1R2 s
|Vref =0 = − (1.6)
Vout ( R2C1 + R1C3 ) s + R1C1C3 R2 s2
Now don’t worry too much about how this compensation was
derived. Just understand the concept as understanding the spe-
cific controller is way beyond the scope of this chapter. So, we
cross multiply the terms, so that they are written as such:
the integrals. You just look at the Laplace transform table pro-
vided. So, this differential equation (1.8) is what describes physi-
cally the relationship between the input and the output of the
compensator. Now this dynamic is implemented using discrete
components such as op-amp resistors and capacitors. So, how do
we implement this type of dynamic digitally on a microcontroller
for example? In order to answer that, we must briefly go over the
Z-transform. Just like how the Laplace transform puts a continu-
ous signal in terms of its ‘s’ or its complex frequency domain. The
Z-transform puts a discrete or digitized signal in terms of a ‘z’ or
complex frequency domain or ‘z’ is equal to ‘e’ the power of ‘sT’
(esT). Now, C(z) is the Z-transform of c[k], where c[k] is a discrete-
time signal sampled at our sampling time, T, as illustrated in
Figure 1.6.
So, the Z-transform is essentially the discrete version of the
Laplace transform and it follows similar properties of the Laplace
transform and it is a mathematical operation that takes a discrete-
time signal and converts it to its complex frequency- domain
representation:
∞
C (z ) = ∑c k z
k =0
−k
(1.9)
Where z= esT
Most importantly it converts convolution in the discrete-time
domain to multiplication in the complex frequency domain.
c ( t ) = e ( t ) ∗ u ( t ) → C ( s ) = E ( s )U ( s ) (1.11)
z = e sT (1.12)
s= σ + jω (1.13)
= = eσ T e jω=
z e sT T
eσ T ∠ωT (1.14)
z = e sT |s =0 = 1∠0o (1.15)
=z e sT=
|s0 (e σT
)
1 → 0∠ωT (1.16)
1
{ }
Z −1 X ( z ) =
2π j ∫ X ( z ) z n −1
dz (1.20)
1.4 DISCRETIZATION OF CONTINUOUS-TIME
SYSTEMS
In this section, we’ll be going over the discretization of analog or
continuous-time systems using Tustin’s method. Whenever we
have a pure continuous-time feedback loop with the continuous-
time plant, sensor, and controller, we design our controller, C(s),
using classical control techniques as shown in Figure 1.1. The two
most popular techniques being root locus and the frequency
response or, Bode plot analysis. Using the root locus, we observe
how close loop poles move in the s-plane as a function of the gain
Digital Control with Arduino ◾ 15
s +1
C(s) =
s+5 (1.22)
z = e sT (1.23)
ln ( z )
s= (1.24)
T
16 ◾ Digital Control of Power Converters
So, substituting for ‘s’ in C(s), we get C(z) that equals natural
logarithm of ‘z’ over T plus 1 over natural logarithm of ‘z’ over T
plus 5 as follows:
ln ( z )
+1
C (z ) = T (1.25)
ln ( z )
+5
T
1
z e sT ≈
Backward difference := (1.27)
1 − sT
T
1+ s
Tustin′s method ( bilinear transformation ) : z =
e sT ≈ 2
T
1− s
2
(1.28)
Now, if we map the left half plane of the s-plane to the z-plane
using each of these approximations, you can see that Euler’s
method and the backward difference methods map very poorly in
comparison to Tustin’s method as illustrated in Figure 1.10.
Digital Control with Arduino ◾ 17
T
1+ s
z= e sT
≈ 2 → s= 2 z − 1 (1.29)
T T z +1
1− s
2
If we now substitute for ‘s’ and C(s) and, for example, set the
sampling time T to be 0.02 seconds, we get the following z-domain
transfer function:
2 z −1
s +1 s=
C (s) = T z +1 (1.30)
s + 5 T sampling time = 0.02 seconds
( )
18 ◾ Digital Control of Power Converters
0.9619z − 0.9429
C (z ) = (1.31)
z − 0.9048
Then, we’re plotting the Bode plots for both C(s) and C(z) on
the same plot to see how they overlap. Now after running the code
on MATLAB, we will get the figure showing the frequency
responses of both C(s) and C(z), and they are matched very closely
with one another as shown in Figure 1.11.
Although they do not perfectly align, in our case of designing a
digital controller that behaves like the continuous one, it’ll do just
fine. Hopefully, this section gave you enough information to know
how to discretize a continuous-time system. In the next section,
we will look into how a digital system like the one derived here is
implemented in code on the Arduino Integrated Development
Environment (IDE).
Digital Control with Arduino ◾ 19
1.5 ARDUINO IMPLEMENTATION OF
DISCRETE-TIME SYSTEMS
In this section, we will go over to see how you can implement a
discrete-time system on the Arduino. Now we’re going to step
back from any discussion about feedback control and just see how
we can implement any arbitrary discrete-time system on a micro-
controller so let’s say we have a second-order system, H(s), that we
wish to discretize as below:
20
H (s) = 2
(1.32)
s + 2.5s + 20
Now, if we apply a step input for this system which we can sim-
ulate with MATLAB using the following code we can observe how
the output behaves and we get a very typical under-damped
second-order response as shown in Figure 1.12:
20 ◾ Digital Control of Power Converters
2 z −1
s= 2
= ( s ) T z + 1 0.042372z + 0.08475z + 0.04237 (1.33)
H ( z ) H=
T = 0.1sec z − 1.61z + 0.7797
FIGURE 1.14 The transient response of both the analog and digital
systems.
( )
Y ( z ) 1 − 1.61z −1 + 0.7797 z −2
(
= X ( z ) 0.04237 + 0.08475z −1 + 0.04237 z −2 ) (1.36)
Digital Control with Arduino ◾ 25
Y ( z ) − Y ( z )1.61z −1 + Y ( z ) 0.7797 z −2
= X ( z ) 0.04237 + X ( z ) 0.08475z −1 + X ( z ) 0.04237z −2 (1.37)
What you’ll notice over and over again is that z-1 raised to the
−1 correlates to the previous sample from one discrete-time step
ago and that z-2 raised to the −2 correlates to the sample from two
discrete-time steps ago. So, we then take our prior output terms
and move them over to the right side as the following equation:
=
y n 0.04237 x n + 0.08475x n − 1 + 0.04237 x n − 2
+ 1.61 y n − 1 − 0.7797 y n − 2 (1.39)
//Declaring variables
int T = 100; /
/sampling time in milliseconds
(T = 0.1s)
26 ◾ Digital Control of Power Converters
Also, for our void setup function which runs only once at the
beginning of the program, we set all our variables to zero since we
want our system response to have a zero initial condition in order
to mimic a step response, we’ll have to generate a unit step func-
tion which is done here in the code below and the waveform of
x[n] is illustrated in Figure 1.16:
//Main Loop
void loop() {
current_time = millis(); //stores the program
running time(ms)
int delta_time = current_time - last_time; //
computes ΔT
if (delta_time >= T) { //when ΔT = T, read
input x[n] and compute y[n]
elapsed_time = elapsed_time + T/1000.0; //
compute elapsed time in seconds
x_input(); //obtain input x[n]
y = 0.0424*x + 0.0848*x_1 + 0.0424*x_2 +
1.61*y_1 - 0.7797*y_2;
x_2 = x_1;
//store x[n-
1] as x[n-
2] for
next iteration
y_2 = y_1;
//store y[n-
1] as y[n-
2] for
next iteration
x_1 = x;
//store x[n] as x[n-
1] for
next iteration
y_1 = y;
//store y[n] as y[n-
1] for
next iteration
last_time = current_time; //restarts ΔT or
delta_time back to 0
}
}
28 ◾ Digital Control of Power Converters
float T = 100.0;
unsigned long last_time;
unsigned long current_time;
double elapsed_time;
double x;
double y;
double x_1;
double y_1;
double x_2;
double y_2;
void setup() {
Serial.begin(9600);
x = 0.0;
y = 0.0;
x_1 = 0.0;
y_1 = 0.0;
x_2 = 0.0;
y_2 = 0.0;
}
void loop() {
current_time = millis();
int delta_time = current_time - last_time;
if (delta_time >= T) {
elapsed_time = elapsed_time + T/1000.0;
x_input();
Digital Control with Arduino ◾ 29
One neat thing about the Arduino IDE is that you can moni-
tor any value on the Serial Plotter, so we’ll want to monitor the
step input x and the output y and see how they behave; hence,
from Tools menu, we select the Serial Plotter option. So, let’s run
the program as shown in Figure 1.17, we’re getting the slow
From the scope, we can see that this is what we expect from the
theoretical model of our discrete transfer function as shown in
Figure 1.21.
Now we want to compare this with the implementation result.
So, we’ll need to monitor the time and output data, so that we can
create a two-dimensional variable on MATLAB. Now going back
32 ◾ Digital Control of Power Converters
to the Arduino, compile and then upload the program from the
Tools menu, we can use the Serial Monitor to display the values
numerically. By running the program, you can see the elapsed
time and output columns on the Serial Monitor.
Currently, we have completed the initial steps. Next, we will
copy the values up to 10 seconds into Notepad++ and then import
them into MATLAB. To do this, we will create a data variable that
includes both the time and output data. Additionally, we will insert
a row above the time value of 0.1 and set it to 0, as we want to
include a row for time zero (time = 0 s), as illustrated in Figure 1.22.
After that, we do rename the data variable as Output_Data as
shown in Figure 1.23.
Then, we use the From Workspace block from Sources in
Simulink to plot the experimental output data and then use a
scope to view this signal alongside the theoretical step input.
Therefore, we do double click on the From Workspace block and
set Data as Output_Data and Sample time to 0.1 seconds as
depicted in Figure 1.24.
FIGURE 1.22 Creating a data variable that contains time and output
data and adding a row above time 0.1 s.
Digital Control with Arduino ◾ 33
This goes to show that you can use the Arduino or any micro-
controller to implement a difference equation or discrete-time
system. So now that we’re confident that we can implement a
discrete-time system in the real time. Let’s move on to discussing
how we can implement the feedback control algorithm on the
Arduino.
U ( z ) b0 + b1z −1 + b2 z −2 +…+ bm z −m
=
C (z ) =
E ( z ) 1 + a1z −1 + a2 z −2 +…+ an z −n
(1.40)
void loop() {
current_time = millis(); //stores the program
running time(ms)
int delta_time = current_time - last_time; //
computes ΔT
if (delta_time >= T) { //when ΔT = T, read
input x[n] and compute y[n]
sensed_output(); //obtain a current sample,
output_y[n]
e = setpoint – output_y; //e[n] = setpoint
- output_y[n]
u = -a_1*u_1-… -a_n*u_n + b_0*e + b_m*e_m;
//C(z)
u_n = u_(n-
1); //store u[n-
(n-
1)] as
u[n-
n] for next iteration
e_m = e_(m-
1); //store e[n-
(m-
1)] as
e[n-
m] for next iteration
. . .
u_1 = u;
//store u[n] as u[n-
1]
for next iteration
e_1 = e;
//store e[n] as e[n-
1]
for next iteration
last_time = current_time; //restarts ΔT or
delta_time back to 0
}
}
So, let’s work our way from the feedback path, so, if you take a
look at the ‘if’ statement in the code, you’ll notice that it in itself is
the sampler as it only executes every sample time of T. So, for
example, if T equals one second, then the code inside the ‘if’ state-
ment will iterate or update every one second. That being said if we
Digital Control with Arduino ◾ 37
FIGURE 1.29 The impulse function is used only for theoretical deriva-
tion and analysis.
So, now that we are in most of the theory out of the way. In the
next few sections, we’ll dive into the design step process and even-
tually demonstrate this process using a DC motor speed control
example.
We want to let you know that this step-by-step procedure isn’t the
only way to go about designing a digital controller. This is just a
method that makes sense to us and hopefully will make intuitive
sense to you. So, the first step is to obtain the continuous-time
transfer function of the system you wish to control and we will
refer to this transfer function as P(s). Now this step is very open
ended because there are many ways to obtain a transfer function
of an open-loop system. For example, the method used to obtain
the transfer function of a power supply will differ from the method
used to obtain the transfer function of a DC motor or a heater
Digital Control with Arduino ◾ 41
system. So, for now, we’ll assume you know what the open-loop
transfer function is for the system you want to control. There’s
plenty of literature and papers out there. We’ll show you how to
get the transfer function of a quad-copter DC motor, heater sys-
tem, power supply, etc. In the coming section, we’ll be demon-
strating this step for a DC motor but it is your job to figure out the
transfer function of the system you wish to control. So, let’s say
you have your open-loop transfer function. So, the next step is to
choose the sampling frequency or sampling time for your digital
controller. So, there is actually no clear-cut method or equation to
choose a sampling frequency or sampling time. If you Google
this, you’ll see that there are several ways to go about choosing a
sampling time. However, Tustin’s method works better when the
sampling frequency (1/T) is much higher than the Nyquist fre-
quency. So, what is the Nyquist frequency? Nyquist frequency (fn)
is the minimum sampling frequency needed to prevent introduc-
ing aliasing or introducing distortion/error in your closed-loop
system. The Nyquist frequency is defined as being strictly twice
the bandwidth of your open- loop system as the following
equation:
Tbw
fn ≥ 2 × f bw or Tn ≤ (1.41)
2
But like we said Tustin’s method works better when the sam-
pling frequency is much higher than the Nyquist frequency. So,
as a rule of thumb is preferable to use a sampling frequency
that’s 5–10 times faster than the bandwidth (f bw) of your open-
loop system. The bandwidth of your system is typically defined
as being the frequency at which the gain of your system is about
3 dB is less than the DC gain. So, the bandwidth frequency or 3
dB frequency is essentially the point where the DC gain begins
to roll off, especially at 3 dB below the DC gain. So, in order to
find the bandwidth of your system, it is required to know the
42 ◾ Digital Control of Power Converters
transfer function so that you may plot the Bode plot and locate
this minus 3 dB point on the magnitude graph as shown in
Figure 1.31.
Next, we move on to design the controller, C(s). So, as we
mentioned in the earlier section when needing a digital control-
ler, it’s much easier to design it as a continuous mode controller
and then discretize it to implement it digitally. So, we got the
open-loop transfer function P(s) and we’ve chosen our sampling
frequency so can we start the controller, C(s), design process
yet? No, we cannot because we’ll be designing a continuous-
time controller using continuous-time design methods such as
root locus or the frequency response analysis, we need to take
into account the continuous-time effects the sampling and the
ZOH function will have in the control loop as depicted in
Figure 1.32.
If we take a continuous-time signal x(t) and pass it through the
sampler and the ZOH block, we get the expected continuous
staircase signal which we’ll call xS(t), and if we were to overlap
these signals in time you can see the hold delay, and it’s
discontinuous-time hold delay that we wish to model and include
in our control loop before designing our controller as illustrated
in Figure 1.33.
Digital Control with Arduino ◾ 43
FIGURE 1.32 The continuous-time effects of ZOH/sampling in the transmission loop.
44 ◾ Digital Control of Power Converters
x (t ) x (t )
= ∗
∑δ (t − kT )
k =−∞
(1.42)
{ ( s ) T1 X s − j 2Tπ k
(t ) X=
x= ∗
} ∗
∑ (1.43)
k =−∞
between x*(t) and a function we’ll call g(t) where g(t) is the follow-
ing rectangular function:
g (t ) = u (t ) − u (t − T ) (1.44)
1 e − sT 1 − e − sT
{ }
g (t ) =
G ( s ) =−
s s
=
s
(1.45)
{ }
x ∗ (t )
= g ( t ) x s ( t ) → X ∗ ( s=
) G ( s ) Xs ( s ) (1.46)
+∞
1 2π k
∑ Xs − j
T k =−∞ T
G(s) =
Xs ( s ) (1.47)
1
X ( s )G ( s ) = X s ( s ) (1.48)
T
Xs ( s ) 1 − e − sT
(1.39 ) , (1.36 ) → X (s)
≈
sT
(1.49)
1 − e − sT
GZOH ( s ) = (1.50)
sT
So, this is the transfer function block that models the hold delay
in the continuous-time domain as shown in Figure 1.37.
Now, we’ll take this block and we’ll place it in the closed-loop,
and now we can finally go ahead and design C(s) while taking into
account the continuous-time effects of the discrete parts. Therefore,
we can now design C(s) using classical design techniques because
we accounted for the discrete effects in our control loop as depicted
in Figure 1.38. The closed-loop transfer function now looks like
the following equation:
2 z −1
s=
C (s) T z +1 = C (z ) (1.52)
T = chosen sampling time
(
U ( z ) a0 + z −1a1 + z −2a2 +…+ z − M aM )
(
= E ( z ) b0 + z −1b1 + z −2b2 +…+ z − M bM ) (1.54)
this model that relates the input voltage, V(s), to the output speed,
θ ( s ) , as the following equation [4]:
θ ( s ) sθ ( s ) K rad / s
=
P (s) = =
V (s) V (s) ( Js + b )( Ls + R ) + K V
2
(1.56)
di
L + Ri =V − K eθ =V − Kθ (1.58)
dt
( Js + b ) sθ ( s ) =
KI ( s ) (1.59)
So, now the big question is how do we find these values for our
gearmotor? Well, if you’re lucky all this information can be avail-
able on the motor’s data sheet. However, almost 99% of the time
this is never the case, so, we’ll have to somehow figure out these
values using our tools and our intuition. So, let’s try to figure out
K, now K is the electric motor force or torque constant. This is
essentially the DC gain that relates our input voltage to our output
speed in radians per second (rad/s). Now, according to the data
sheet, the nominal speed at the rated voltage of 12 volts is 220
RPM now, 220 RPM can also be expressed as 23 rad/s since we
have:
2π ( rad )
220 RPM =
220 × ≈ 23rad / s (1.61)
60 ( s )
54 ◾ Digital Control of Power Converters
{
if (statep == 2) encoder_count-
-;
if (statep == 4) encoder_count++;
break;
}
case 2:
{
if (statep == 1) encoder_count++;
if (statep == 3) encoder_count-
-;
break;
}
case 3:
{
if (statep == 2) encoder_count++;
if (statep == 4) encoder_count-
-;
break;
}
default:
{
if (statep == 1) encoder_count-
-;
if (statep == 3) encoder_count++;
break;
}
}
statep = state;
}
void get_speed(){
current_angle = (encoder_count*360.0)/(1632.67);
motor_speed = 0.01745*((current_angle
- previous_angle)/0.05);
previous_angle = current_angle;
}
void step_input(){
control = 0;
if (current_time >= 2000 && current_time <=
6000){
control = 12;
}
Digital Control with Arduino ◾ 57
step_input();
get_speed();
analogWrite(pwm_output,(control/
supply_voltage)*255.0);
Serial.print(control);
Serial.print(" ");
Serial.print(elapsed_time);
Serial.print(" ");
Serial.println(motor_speed);
previous_time = current_time;
}
}
Let’s run the script and then from Tools menu, we select the
Serial Monitor option. Now we’ll take 15 seconds rows of data and
then on MATLAB, we’ll create 7 data variables that contain R, L,
K, J, b, and also, the measured Input_Data and Output_Data
which the first column is the elapsed_time and the second col-
umn is control and motor_speed, respectively, as depicted in
Figure 1.44.
Now, before the estimation tool can run, we’ll need to use some
initial values for all the parameters. So earlier we approximated
what K, R, and L could be. So, that’s what we’ll be using for their
initial values as illustrated in Figure 1.45.
Now, if you click on Plot button in that window, you should see the
measured input voltage and output speed data we got from the
Arduino.
Now let’s click on the Estimate button, you can see that the tool
is running some sort of optimization algorithm to curve fit the
simulation output to the measured output and it’s doing this by
iterating and varying the parameters. Now, once we’ve come to a
close enough fit, the tool stops estimating and our parameter val-
ues should have automatically been updated by clicking on the
Exp icon in Experiments sub-window.
Now, using the following script as given below, we can get the
approximate transfer function for our DC gearmotor as the fol-
lowing equation:
0.47
P (s) = 2
(1.62)
3.425e − 5s + 0.01859s + 0.2211
J = 0.004893;
b = 5.8147e-
5;
K = 0.47;
R = 3.8;
L = 0.006999;
num = K;
den = [J*L,(J*R)+(b*L),(b*R)+(K^2)];
sys = tf(num,den);
1.8.2 Controller Design
In this section, we will continue the controller design process for
DC motor speed control example. So, we dedicated the previous
section to obtaining an approximate transfer function for DC
gearmotor which is shown in Figure 1.46.
Digital Control with Arduino ◾ 61
s = tf(‘s’);
P_s = 0.47/(3.425e-
5*s^2 + 0.01859*s + 0.2211);
bode(P_s);
grid on;
1 1
T= = = 0.05seconds (1.63)
f 20
62 ◾ Digital Control of Power Converters
FIGURE 1.47 Choosing a sampling time, T.
Digital Control with Arduino ◾ 63
FIGURE 1.48 The continuous-time block diagram for our control loop.
H (s) = 1 (1.64)
θ ( s ) C (s)P (s)
R (s)
Gθ= = (1.65)
R ( s ) 1 + C ( s ) P ( s ) GZOH ( s ) H ( s )
T ( s ) = C ( s ) P ( s ) GZOH ( s ) H ( s ) (1.67)
64 ◾ Digital Control of Power Converters
FIGURE 1.49 Adding explicit transfer functions for P(s) and GZOH(s).
1 − e − sT
GZOH ( s )
= = , T 0.05sec (1.68)
sT
1 1 1 1 4 1 5
1 + x + x2 + x3 + x + 30240 x ...
Pade Appr. : e x = 2 9 72 1008
1 1 2 1 3 1 4 1 5
1− x + x − x + x − 30240 x ...
2 9 72 1008
(1.69)
Digital Control with Arduino ◾ 65
40s 3 + 960000s
GZOHP ( s ) =
s 4 + 240s 3 + 24000s 2 + 960000s (1.70)
So now that, we know that both P(s) and GZOHP(s) are rational
and we can go ahead and design C(s) as shown in Figure 1.50.
Now, for controlling the speed and position of a DC motor,
typically a proportional integral (PI) controller is used in the
industry and most conventional DC motors have their output
speed regulated using a PI controller as the following equation:
Ki
C (=
s) K p + (1.71)
s
FIGURE 1.50 The rational transfer functions for P(s) and GZOHP(s) in
the control loop.
66 ◾ Digital Control of Power Converters
So now let’s run the script, so the PID tuner raptured open up
automatically and by default it seems like it chose a PI controller
on its own, and what you see now is a step response of the closed-
loop system’s output and upon the top that are sliders that we can
use to adjust the response time and transition behavior such that
it meets our criteria, and on the bottom right should be the Kp and
the Ki gain values that it correlates to. So, we’ll play around with
this plot until we think it looks good. So, this response seems to
work and if we click on the Show Parameters tab, we can see the
gain values, the settling time, overshoot percentage, and the sta-
bility margins as depicted in Figure 1.51.
So far, they all meet our criteria and we think we’ll go with this
design, so, this is our continuous-time PI controller with Kp that is
equal to 0.427 and Ki is equal to 5.114 and we have the following
equation:
Ki 5.114
C (s) = K p + = 0.427 + (1.72)
s s
(
U ( z ) 1= ) (
− z −1 E ( z ) 0.5548 − 0.2991z −1 ) (1.74)
U ( z ) − U ( z ) z −=
1
E ( z ) 0.5548 − E ( z ) 0.2991z −1 (1.75)
Z {}−1
(1.64 ) → u n − u=
n − 1 0.5548e n − 0.2991e n − 1
(1.76)
Then we bring all the delayed input and output terms to the
right side as the equation below:
u n = u n − 1 + 0.5548e n − 0.2991e n − 1 (1.77)
1.8.3 Arduino Demonstration
In this final section, we’ll finally implement the digital controller
designed in the previous section to regulate the speed of our DC
gearmotor. So, we dedicated the last two sections to completing
steps 1–5. Now all that remains is implementing the controller on
the Arduino to demonstrate the closed-loop control. However,
before we go ahead and code up the script, we are going to first
simulate this control structure to see what type of behavior we
should expect. Shown here in Figure 1.53 is the Simulink block
diagram for our closed-loop feedback system. Notice that there’s a
saturation block after the controller. Now what does this block
will do is to limit the control signal to be within the bounds of
70 ◾ Digital Control of Power Converters
FIGURE 1.53 The Simulink block diagram for our closed-loop feedback
system.
0–12 volts and that’s because our real power supply for our PWM
driver is set to 12 volts.
Also, because the control signal of 12 V correlates to a PWM
duty cycle of 100% so it’s inherently the maximum voltage we can
apply. So, a good way to test the feedback system is by stepping the
desired set point to a few speed values and observing how well the
output speed tracks the set point. So, using the staircase signaling
block, we create the set points signal where every four seconds, the
set point changes abruptly to a few different values and we’re
observing the plots for the desired set point and the measured
speed as shown in Figure 1.53. Let’s run the file, while observing
the plot, you can see that it tracks the set point speed very well and
it also meets our design right here in regard to the settling time,
overshoot, and steady-state error as shown in Figure 1.54.
So, in simulation, our results look good. Now, let’s go imple-
ment this exact same test on the Arduino setup. So, here is the
Arduino script.
Digital Control with Arduino ◾ 71
if (statep == 1) encoder_count-
-;
if (statep == 3) encoder_count++;
break;
}
}
statep = state;
}
void get_speed(){
current_angle = (encoder_count*360.0)/(1632.67);
// 1632.67 is the total //number of counts per
revolution. Because it’s a 48 CPR encoder and a
34.1 //gearmotor, you multiply 48 x 34.1 =
1632.67
output_speed = 0.0175*((current_angle - previous_
angle)/0.05); // 0.0175 rad/s = 1 degree.
The formula is essentially calculating theta_dot
// or dteta/dT, where dT = 0.05, is the sampling
time in seconds
previous_angle = current_angle;
}
void step_setpoint(){ // a stair-
case setpoint
input signal to test the //transient response of
the control loop
setpoint = 0;
if (current_time >= 4000 && current_time <=
8000){
setpoint = 5;
// after 4 seconds,
setpoint = 5 rad/s for 4 seconds
}
if (current_time > 8000 && current_time <=
12000){
setpoint = 20;
// setpoint = 20 rad/s
for 4 seconds
}
if (current_time > 12000 && current_time <=
16000){
setpoint = 12;
// setpoint = 12 rad/s
for 4 seconds
}
74 ◾ Digital Control of Power Converters
FIGURE 1.57 Feeding data from the workspace and plot against the
simulated data – Part 1.
Theoretical Overview
of the Buck Converter
DOI: 10.1201/9781003541356-2 81
82 ◾ Digital Control of Power Converters
Vin 24 V
Vout 16 V
Iout( = iL) 2.133 A
Rout 7.5 Ω
∆VO, pk − pk 0.02 Vout
∆IL, pk − pk 0.1 Iout
rESR 3 mΩ
rL 300 mΩ
Vfwd 1.15 V
Vds, sat 0.075 V
L 200 μH
C 40 μF
fsw 100 kHz
Vfwd iL rL Vout
D 0.7094 (2.1)
Vin Vfwd Vce,sat
Theoretical Overview of the Buck Converter ◾ 83
L
Vin Vce,sat iL rL Vout D 243.3 H (2.2)
I L , pk pk f sw
I L , pk pk
C 0.83 F (2.3)
8Vo, pk pk f sw
s = tf(‘s’);
Gid = (3e-6*s+25.07)/(6e-8*s^2+0.000291*s+7.801);
pidTuner(Gid);
By executing the above code, the PID tuner window is popped up.
We select the type of controller as PI and its form as Standard and
then regulate the Response Time and Transient Behavior sliders to
get desired Step Plot. Then, if we click on Show Parameters button, we
can see our controller parameters. Finally, we click on the Export
button and give it a name for our PI controller. In this case, we chose
‘Cont’ as its name and click on the OK button as shown in Figure 2.2.
Now, in the command window, if we type the name (‘Cont’)
and then press the Enter key, you can get the controller transfer
function as the following equation:
Kp
Gc s K p 1
1
K Ti 3.11 105 120.5 (2.8)
p
Ti s s s
Theoretical Overview of the Buck Converter ◾ 85
s
2 z 1 2 1 z
1
(2.10)
Ts z 1 Ts 1 z 1
where TS is the sampling time and is chosen as 100 μs, so we have:
Ts z 1
Gc z K p 1
2Ti z 1
K p z 1 K pTs z 1 / 2Ti
z 1 (2.11)
K p K pTs / 2Ti z K pTs / 2Ti K p
z 1
0.0061z 0.006
z 1
86 ◾ Digital Control of Power Converters
0.006z 1 0.0061
Gc z (2.12)
z 1 1
c2d(Cont,100e-6,’tustin’)
B3 z 3 B2 z 2 B1z 1 B0 yn
Gc z (2.13)
A3 z 3 A2 z 2 A1z 1 1 en
=
A1 1,=
B0 0.0061, B1 = 0.006 (2.14)
s = tf(‘s’);
Gvd = (2.257e-5*s+188.1)/(6e-8*s^2+0.000291*s
+7.801);
pidTuner(Gvd);
Kp
1 Ti 16.05
Gc s K p 1 Kp 4.14 106 (2.16)
Ti s s s
88 ◾ Digital Control of Power Converters
s
2 z 1 2 1 z
1
(2.18)
Ts z 1 Ts 1 z 1
where TS is the sampling time and is chosen as 100 μs, so we have:
Ts z 1
Gc z K p 1
2Ti z 1
K p z 1 K pTs z 1 / 2Ti
z 1 (2.19)
K p K pTs / 2Ti z K pTs / 2Ti K p
z 1
0.00081z 0.0008
z 1
Theoretical Overview of the Buck Converter ◾ 89
0.0008z 1 0.00081
Gc z (2.20)
z 1 1
c2d(Cv,100e-6,’tustin’)
B3 z 3 B2 z 2 B1z 1 B0 yn
Gc z (2.21)
A3 z 3 A2 z 2 A1z 1 1 en
90 ◾ Digital Control of Power Converters
=
A1 1,=
B0 0.00081, B1 = 0.0008 (2.22)
i o j12566.4
2.6 6.236 114.6 (2.25)
d j12566.4
comp 59 114.6 180 6.4 (2.31)
Gc s K p i 1 (2.32)
s
92 ◾ Digital Control of Power Converters
Gc jc K p i 1 (2.33)
jc
Gc jc tan 1 i (2.34)
c
6.4 tan 1 i (2.35)
c
i c tan 6.4 (2.36)
i 12566.4 tan 6.4 1409.55 rad / s (2.37)
2
i 1 1
Gc jc K p 1 Gain 6.236 (2.38)
c
1 / 6.236
Kp 0.159 (2.39)
tan 6.4 1
2
1409.55 224.12
Gc s K p i 1 0.159 1 0.159
s s s
(2.40)
Theoretical Overview of the Buck Converter ◾ 93
v˘o j12566.4
2. 7 ˘
46.79 114.6 (2.42)
d j12566.4
comp 59 114.6 180 6.4 (2.48)
Gc s K p i 1 (2.49)
s
Gc jc K p i 1 (2.50)
jc
Gc jc tan 1 i (2.51)
c
6.4 tan 1 i (2.52)
c
i c tan 6.4o (2.53)
i 12566.4 tan 6.4 1409.55 rad / s (2.54)
2
i 1 1
Gc jc K p 1 Gain 46.79 (2.55)
c
1 / 46.79
Kp 0.021 (2.56)
tan 6.4 1
2
Theoretical Overview of the Buck Converter ◾ 95
1409.55 29.6
Gc s K p i 1 0.021 1 0.021
s s s
(2.57)
3e-06 s + 25.07
----------------------------------
7.33e-
08 s^2 + 0.0002912 s + 7.801
Gvd = <TransferFunction>: sys[1]
Inputs (1): [‘u[0]’]
Outputs (1): [‘y[0]’]
2.257e-
05 s + 188.1
---------------------------------
7.33e-
08 s^2 + 0.000291 s + 7.801
The above codes are used for printing the transfer functions of
Gid and Gvd as given in Equations (2.6) and (2.7). The code below
is used for the Bode plot of Gid as illustrated in Figure 2.6.
w = np.logspace(1,5)
mag,phase,omega = bode(Gid,w)
plt.tight_layout()
3.11e-
05 s + 120.5
------------------
s
Gpci_mat = Gid*Gci_mat
w = np.logspace(1,5)
mag,phase,omega = bode(Gpci_mat,w)
plt.tight_layout()
The closed-loop transfer function and the code for the Bode
plot of it are depicted in Figure 2.8.
Tpci_mat = Gpci_mat/(1+Gpci_mat)
w = np.logspace(1,5)
mag,phase,omega = bode(Tpci_mat,w)
plt.tight_layout()
0.159 s + 224.1
---------------
s
Gpci_pm = Gid*Gci_pm
w = np.logspace(1,5)
mag,phase,omega = bode(Gpci_pm,w)
plt.tight_layout()
The closed-loop transfer function and the code for the Bode
plot of it are depicted in Figure 2.10.
Tpci_pm = Gpci_pm/(1+Gpci_pm)
w = np.logspace(1,5)
mag,phase,omega = bode(Tpci_pm,w)
plt.tight_layout()
The code below is used for the Bode plot of Gvd as illustrated in
Figure 2.11.
w = np.logspace(1,5)
mag,phase,omega = bode(Gvd,w)
plt.tight_layout()
Theoretical Overview of the Buck Converter ◾ 101
4.14e-
06 s + 16.05
------------------
s
Gpcv_mat = Gvd*Gcv_mat
w = np.logspace(1,5)
mag,phase,omega = bode(Gpcv_mat,w)
plt.tight_layout()
The closed-loop transfer function and the code for the Bode
plot of it are depicted in Figure 2.13.
Tpcv_mat = Gpcv_mat/(1+Gpcv_mat)
w = np.logspace(1,5)
mag,phase,omega = bode(Tpcv_mat,w)
plt.tight_layout()
0.021 s + 29.6
--------------
s
Gpcv_pm = Gvd*Gcv_pm
w = np.logspace(1,5)
mag,phase,omega = bode(Gpcv_pm,w)
plt.tight_layout()
The closed-loop transfer function and the code for the Bode
plot of it are depicted in Figure 2.15.
104 ◾ Digital Control of Power Converters
Tpcv_pm = Gpcv_pm/(1+Gpcv_pm)
w = np.logspace(1,5)
mag,phase,omega = bode(Tpcv_pm,w)
plt.tight_layout()
FIGURE 2.17 The step response of current control loop (up: MATLAB,
down: Phase Margin).
FIGURE 2.18 The step response of voltage control loop (up: MATLAB,
down: Phase Margin).
Theoretical Overview of the Buck Converter ◾ 107
%clear all
pkg load control
s = tf(‘s’);
%Initialization of converter parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Vin = 24;
Vo = 16;
Io = 2.133;
Ro = Vo/Io;
fsw = 100000;
rL = 300e- 3;
rESR = 3e- 3;
Vds = 0.075;
Vfwd = 1.15;
iL = Io;
D = (Vfwd + iL*rL + Vo)/(Vin - Vds + Vfwd);
L = 200e-6;
C = 40e-6;
delta_iL = 0.1 * iL;
delta_Vo = 0.02 * Vo;
%L >= (Vin - Vds - iL*iL - Vo)*D/(fsw*delta_iL);
%C >= delta_iL/(8*delta_Vo*fsw);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 ◾ Digital Control of Power Converters
The codes for small signal output current to duty cycle and out-
put voltage to duty cycle transfer functions are as below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gci = Kpi*(wi_i/s + 1);
Gcv = Kpv* (wi_v/s + 1);
%Gc1 = 1;
Ti = Gci*GIoD;
Tv = Gcv*GVoD;
Theoretical Overview of the Buck Converter ◾ 111
The following codes are used for calculating gain and phase
margins using closed-loop transfer functions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate gain and phase margins
[gain_margin, phase_margin, crossover_
frequencies, phase_crossover] = margin(Ti);
%Display Current Bode Plot the results
fprintf(‘Current, Gain margin: %.2f dB\n’,
20*log10 (gain_margin));
fprintf(‘Current Phase margin: %.2f degrees \n’,
phase_margin);
fprintf(‘Current Crossover frequencies: %.2f
rad/s\n’, crossover_frequencies);
fprintf(‘Current Phase at crossover: %.2f
degrees\n’, phase_crossover);
[gain_margin, phase_margin, crossover_
frequencies, phase_crossover] = margin(Tv);
%Display Voltage Bode Plot the results
fprintf(‘Voltage Gain margin: %.2f dB\n’,
20*log10 (gain_margin));
fprintf(‘Voltage Phase margin: %.2f degrees\n’,
phase_margin);
fprintf(‘Voltage Crossover frequencies: %.2f
rad/s\n’, crossover_frequencies);
fprintf(‘Voltage Phase at crossover: %.2f
degrees\n’, phase_crossover);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 ◾ Digital Control of Power Converters
So, in this case, the duty cycle is updated every 100 μs. We run
the program and go to the command window and get current and
voltage closed-loop gain and phase margins with current and
voltage controllers with and without current and voltage control-
lers and then get the controllers transfer functions in z-domain as
below.
>> loop_design_octave
Current, Gain margin: Inf dB
Current Phase margin: 59.06 degrees
Current Crossover frequencies: NaN rad/s
Current Phase at crossover: 12563.57 degrees
Voltage Gain margin: Inf dB
Voltage Phase margin: 58.94 degrees
Voltage Crossover frequencies: NaN rad/s
Voltage Phase at crossover: 12569.17 degrees
>> Gci
Transfer function ‘Gci’ from input ‘u1’ to output
…
0.1594 s + 225.5
y1: ----------------
s
Continuous-time model.
>> Gcv
Transfer function ‘Gcv’ from input ‘u1’ to output
…
0.02127 s + 30.16
y1: -----------------
s
Continuous-time model.
Theoretical Overview of the Buck Converter ◾ 113
>> Gi_d
Transfer function ‘Gi_d’ from input ‘u1’ to
output …
0.1594 z - 0.1369
y1: -----------------
z - 1
Sampling time: 0.0001 s
Discrete-time model.
>> Gv_d
Transfer function ‘Gv_d’ from input ‘u1’ to
output …
0.02127 z - 0.01825
y1: -------------------
z - 1
Sampling time: 0.0001 s
Discrete-time model.
%clear all
pkg load control
s = tf(‘s’);
%Initialization of converter parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Vin = 24;
Vo = 16;
Io = 2.133;
Ro = Vo/Io;
114 ◾ Digital Control of Power Converters
fsw = 100000;
rL = 300e- 3;
rESR = 3e- 3;
Vds = 0.075;
Vfwd = 1.15;
iL = Io;
D = (Vfwd + iL*rL + Vo)/(Vin - Vds + Vfwd);
L = 200e-6;
C = 40e-6;
delta_iL = 0.1 * iL;
delta_Vo = 0.02 * Vo;
%L >= (Vin - Vds - iL*iL - Vo)*D/(fsw*delta_iL);
%C >= delta_iL/(8*delta_Vo*fsw);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Initialization of the crossover frequency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wc_i = 2*pi*2000;
fc_i = wc_i/(2*pi);
wc_complex_i = wc_i*1i;
wc_v = 2*pi*2000;
fc_v = wc_v/(2*pi);
wc_complex_v = wc_v*1i;
targ_PM_deg_i = 59;
targ_PM_deg_v = 59;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Small Signal Transfer Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Output Current to Duty Cycle beginning
GIoD_num = (1+s*C*rESR)*(Vin - Vds + Vfwd);
GIoD_den = (L*C*Ro + L*C*rESR)*s^2 + (C*Ro*rL +
C*rESR*iL + C*Ro*rESR + L)*s + rL + Ro;
GIoD = (GIoD_num/GIoD_den);
%Output Current to Duty Cycle ending
%Output Voltage to Duty Cycle beginning
GVoD_num = (1+s*C*rESR)*(Vin - Vds + Vfwd)*Ro;
GVoD_den = (L*C*Ro + L*C*rESR)*s^2 + (C*Ro*rL +
C*rESR*rL + C*Ro*rESR + L)*s + rL + Ro;
Theoretical Overview of the Buck Converter ◾ 115
GVoD = (GVoD_num/GVoD_den);
%Output Voltage to Duty cycle ending
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Transfer functions to realize the gain and
phase at crossover frequency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Output Current to Duty Cycle gain_phase
beginning
g = wc_complex_i;
GIoD_num_g = (1+g*C*rESR)*(Vin - Vds + Vfwd);
GIoD_den_g = (L*C*Ro + L*C*rESR)*g^2 + (C*Ro*rL
+ C*rESR*rL + C*Ro*rESR + L)*g + rL + Ro;
GIoD_g = (GIoD_num_g/GIoD_den_g);
%Output Current to Duty Cycle gain_phase ending
%Output Voltage to Duty Cycle gain_phase
beginning
g = wc_complex_v;
GVoD_num_g = (1+g*C*rESR)*(Vin - Vds + Vfwd)*Ro;
GVoD_den_g = (L*C*Ro+ L*C*rESR)*g^2 + (C*Ro*rL +
C*rESR*iL + C*Ro*rESR + L)*g + rL + Ro;
GVoD_g = (GVoD_num_g/GVoD_den_g);
%Output Voltage to Duty Cycle gain_phase ending
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Current Controller design begin
i_gain = abs(GIoD_g);
i_phase = angle(GIoD_g)*(180/pi);
targ_PM_rad_i = targ_PM_deg_i*(pi/180);
phase_comp_i = targ_PM_rad_i - (i_phase*(pi/180))
- pi;
fi_i = fc_i * tan(-1*phase_comp_i);
Kpi = (1/i_gain)/sqrt((fi_i/fc_i)^2 + 1);
wi_i = fi_i*2*pi;
%Current Controller design ends
%Voltage Controller design begin abs (GVOD_g);
v_gain = abs(GVoD_g);
116 ◾ Digital Control of Power Converters
v_phase = angle(GVoD_g)*(180/pi);
targ_PM_rad_v = targ_PM_deg_v*(pi/180);
phase_comp_v = targ_PM_rad_v - (v_phase*
(pi/180)) - pi;
fi_v = fc_v * tan(-1*phase_comp_v);
Kpv = (1/v_gain)/sqrt((fi_v/fc_v)^2 + 1);
wi_v = fi_v*2*pi;
%Voltage Controller design ends
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gci = Kpi*(wi_i/s + 1);
Gcv = Kpv* (wi_v/s + 1);
%Gc1 = 1;
Ti = Gci*GIoD;
Tv = Gcv*GVoD;
%Create a figure to hold the plots
%figure;
%Plot Bode plot for the system
%bode(Ti);
%bode(Tv);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate gain and phase margins
[gain_margin, phase_margin, crossover_
frequencies, phase_crossover] = margin(Ti);
%Display Current Bode Plot the results
fprintf(‘Current, Gain margin: %.2f dB\n’,
20*log10 (gain_margin));
fprintf(‘Current Phase margin: %.2f degrees \n’,
phase_margin);
fprintf(‘Current Crossover frequencies: %.2f
rad/s\n’, crossover_frequencies);
fprintf(‘Current Phase at crossover: %.2f
degrees\n’, phase_crossover);
[gain_margin, phase_margin, crossover_
frequencies, phase_crossover] = margin(Tv);
%Display Voltage Bode Plot the results
Theoretical Overview of the Buck Converter ◾ 117
Digital Control
Implementation of
the Buck Converter
3.1 HARDWARE OUTLINE
In this chapter, we are going to discuss about the hardware we will
use to implement the buck converter controller. We will utilize
the STM32 Nucleo-G474RE board and it is connected to the main
converter board as shown in Figure 3.1.
We have actually an AC–DC converter that converts single
phase 230 Vac to 24 Vdc and that is an input to the system. In the
main converter, we have an auxiliary power supply and, of course,
we have an inductor coil, and on the right-hand side of it, we have
a diode and on the left- hand side, there is a Metal- Oxide-
Semiconductor Field- Effect Transistor (MOSFET) and the
MOSFET is significantly over sized and it does not require a heat
sink, but the diode requires a heat sink. Then we have the output
of the main converter and the resistive load is connected to the
output. There are four 10 μF parallel capacitors and also we have
an inductor that serves outgoing current to the output and it
118 DOI: 10.1201/9781003541356-3
Digital Control Implementation of the Buck Converter ◾ 119
FCLK
FPWM (3.1)
ARR 1 PSC 1
FCLK
ARR 1 (3.2)
PSC 1 FPWM
FCLK
ARR 1 (3.3)
PSC 1 FINT
The FINT in this case is 10 kHz, and the same procedure done
before is followed here. We select the PSC which is 1 in this case,
also the FCLK is 170 MHz, so the calculated ARR is to be 8499 and
we keep the auto-reload preload to be Disable. So, everything is
perfect and we can click on the Generate Code button. After gen-
erating the code, the STM32CubeIDE is automatically opened. So,
the successful code generation message window is popped up
finally, and then we click on the Open Project button. So, we
launch the program in the workspace directory. Finally, you can
see successfully importing the program into the workspace
124 ◾ Digital Control of Power Converters
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
FIGURE 3.3 Adding code to start the PWM with a duty cycle of 41.2%.
If you connect pin PC0 to the oscilloscope, you can see the
PWM signal with a frequency of 100 kHz and a duty cycle of
41.2%. We will use a maximum of 70% of duty cycle in this pro-
gram. Next, we are going to initialize the ADC and DMA which is
actually done together in this case. As a first thing, we will utilize
the ADC with DMA and add the following code before while (1):
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)
adcValues, 2);
Also, in the Private variables section, you need
to set up the code below.
float Vactual = 0.0;
float Iactual = 0.0;
uint32_t adcValues[2];
HAL_TIM_Base_Start_IT(&htim4);
void HAL_TIM_PeriodElapsedCallback(TIM_
HandleTypeDef* htim){
if(htim-
>Instance == TIM4){
Vactual = adcValues[0]*0.008137207031;
Iactual = adcValues[1]*0.001611328125;
}
}
RawAdcVal R1 R2
Vactual AdcRes Vref R (3.4)
2 2
Vref R R2
Vactual RawAdcVal 1 (3.6)
2AdcRes R2
RawAdcVal
AdcRes Vref
2
AmpGain
I actual
Rshunt (3.9)
RawAdcVal
3. 3
4096
50
I actual (3.10)
0.01
You should note that the voltage read in the STM32 microcon-
troller ADC pin is as below:
RawAdcVal
Vread 3. 3 (3.11)
4096
3. 3
4096
50
I actual RawAdcVal (3.12)
0.01
128 ◾ Digital Control of Power Converters
Then we can run the program and debug the code. So, we run
the code in the debug mode to verify that the Vactual and Iactual are
really updated with the output voltage and output current. That is
very important before going ahead to control the converter and
first thing is to check that the duty cycle can be changed and the
second part is to check the timer interrupt is being triggered and
it is actually very important to execute the control code and the
third part is ensuring that you can read the actual output voltage
and current with the equations have been proposed because they
are very important to obtaining closed-loop control. If one of
them is not set, nothing will be worked basically. We have checked
the PWM signal before but the others have not been checked yet
and they will be demonstrated in this section. Therefore, for run-
ning the program in debug mode, we do right click on the project
name, select Debug As and then choose Debug Configurations…
item. In the Debug Configurations window and in the Debugger
tab, we set the Enable live expressions and then click on the Close
button.
Then, we click on the Debug icon (green insect). In the opened
Confirm Perspective Switch window, we do click on the Switch
button. From Windows tab in menu, we select Show View and the
Expressions item. Then we click on the Add new expression, to see
the expressions we want, Vactual is one of them and Iactual is being
the other one and we want to show up them, so, we click on the
Resume (F8) icon in menu. The duty cycle of the PWM signal has
been set to as below equation:
400
PWM duty cycle 100 47% (3.14)
849
The system is turned on and the values of Vactual and Iactual are
displayed as Table 3.1.
Digital Control Implementation of the Buck Converter ◾ 129
3.4 FIRMWARE IMPLEMENTATION OF
VOLTAGE CONTROL
In this section, we will discuss about the next part of firmware
which is implementing the closed-loop control system, especially
the function that will implement the closed-loop control system,
that is, changing the duty cycle based on the center inputs. We
will think about the broader scheme which in terms of codes
in it and code the firmware in such a way that be easily used in
other families or use a function or separate file, that you actually
call to implement the controller, so that it can be used in other
microcontrollers as well and you basically copy and paste certain
file and change the duty cycle update and also do ADC update
130 ◾ Digital Control of Power Converters
from the DMA. The first step is that basically, you should create a
header file, so we do right click on the Inc folder, select New, and
then choose the Header File option. We do type a name, custom.h,
in Header file row and click on the Finish button. We define a
void function inside custom.h file as below, since it will not return
anything and save it.
#ifndef INC_CUSTOM_H_
#define INC_CUSTOM_H_
void CNTRL_ROUTINE(void);
#endif
After that, we do right click on the Src folder, select New, and
then choose the Source File option. We do type a name, custom.c,
in Source file row and click on the Finish button. First, we are
going to make the function we defined before, so we add the fol-
lowing code in custom.c file.
void CNTRL_ROUTINE(void){
}
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include main.h
#include custom.h
extern float Vref;
extern float Iref;
extern float Vactual;
extern float Iactual;
extern float e_v[5];
extern float y_v[5];
extern float e_i[5];
extern float y_i[5];
extern uint32_t adcValues[2];
void CNTRL_ROUTINE(void){
}
yn1 yn (3.15)
yn 3 yn 2 (3.17)
The same updates are applied for the errors, so, first, we do
copy and paste the Vactual equation code from interrupt duty func-
tion inside the CNTRL_ROUTINE function and apply the
updates for outputs and errors as depicted in the following code.
void CNTRL_ROUTINE(void){
Vactual = adcValues[0]*0.008137207031;
y_v[3] = y_v[2];
y_v[2] = y_v[1];
y_v[1] = y_v[0];
e_v[3] = e_v[2];
e_v[2] = e_v[1];
e_v[1] = e_v[0];
e_v[0] = Vref – Vactual;
y_v[0] = A1_v*y_v[1] + A2_v*y_v[2] +
A3_v*y_v[3] + B0_v*e_v[0] + B1_v*e_v[1] +
B2_v*e_v[2] + B3_v*e_v[3];
Digital Control Implementation of the Buck Converter ◾ 133
Then we add the codes for e_v[0] and PI controller and define
limits for maximum and minimum duty cycles, y_v[0]. Since the
y_v[0] is of float type and is between 0 and 1, we should convert it
to integer and we know that the MAX_DUTY is 849 in this case as
you can see in the above code. It is the great advantage of STM32
microcontrollers that you can implement a controller mathemati-
cally and allow you to define float-type numbers and it usually
does the mathematical operations in less than 10 μs. Now we are
going to define coefficients and variables that have been utilized in
the code inside the custom.c file as the following code.
#define A1_v 1
#define A2_v 0
#define A3_v 0
#define B0_v 0.001107
#define B1_v 0.002056
#define B2_v 0
#define B3_v 0
#define y_v_max 0.75
#define y_v_min 0.0
#define MAX_DUTY 849
so we go back to the main.c file and call it inside the interrupt duty
function and do comment the Vactual and Iactual expressions, as depicted
in the following code, since they will be calculated inside the
CNTRL_ROUTINE function in custom.c file.
TIM1->CCR1=400;
3.5 FIRMWARE IMPLEMENTATION OF
CURRENT CONTROL
In this section, we will discuss about implementation of current
closed-loop control specifically output current to duty cycle which
Digital Control Implementation of the Buck Converter ◾ 135
#ifndef INC_CUSTOM_H_
#define INC_CUSTOM_H_
void CNTRL_ROUTINE_VOLTAGE(void);
void CNTRL_ROUTINE_CURRENT(void);
#endif
136 ◾ Digital Control of Power Converters
Then we are going to the file custom.c and we create the func-
tion CNTRL_ROUTINE_CURRENT, exactly like the function
CNTRL_ROUTINE_VOLTAGE, and we use the Iactual code line
instead of Vactual in this case, and also change any v, in arrays or
variables to i, as you can see in the code below.
void CNTRL_ROUTINE_CURRENT(void){
Iactual = adcValues[1]*0.001611328125;
y_i[3] = y_i[2];
y_i[2] = y_i[1];
y_i[1] = y_i[0];
e_i[3] = e_i[2];
e_i[2] = e_i[1];
e_i[1] = e_i[0];
e_i[0] = Iref – Iactual;
y_i[0] = A1_i*y_i[1] + A2_i*y_i[2] +
A3_i*y_i[3] + B0_i*e_i[0] + B1_i*e_i[1] +
B2_i*e_i[2] + B3_i*e_i[3];
if(y_i[0] > y_i_max){
y_i[0] = y_i_max;
}
if(y_i[0] < y_i_min){
y_i[0] = y_i_min;
}
TIM1-
>CCR1 = (int)(MAX_DUTY*y_i[0]);
}
We know that the output resistor value is 7.5 Ω and we set the
Iactual to 2.133 A, so, the output voltage will be 16 V. We have used
5% resistors for resistor divider sensors to get more accurate out-
put voltage it is better to use 1% resistors to achieve more accurate
values. Then we are going to define the PI current closed-loop
controller coefficients as well as maximum and minimum values
for the duty cycles as depicted in the following code.
#define A1_v 1
#define A2_v 0
Digital Control Implementation of the Buck Converter ◾ 137
#define A3_v 0
#define B0_v 0.001107
#define B1_v 0.002056
#define B2_v 0
#define B3_v 0
#define A1_i 1
#define A2_i 0
#define A3_i 0
#define B0_i 0.008334
#define B1_i 0.01548
#define B2_i 0
#define B3_i 0
#define y_i_max 0.75
#define y_i_min 0.0
#define y_v_max 0.75
#define y_v_min 0.0
#define MAX_DUTY 849
You should note that we do not call the voltage controller func-
tion here, so we do comment it. Then we can build the program by
clicking on the Build All (Ctrl+B) icon in the menu and it is built
successfully. Since, the Iactual is 2.133 A, and the output resistor is
7.5 Ω, we should expect to have the output voltage in the range
from 16 V to 16.5 V. So, we can run the program to upload it to the
STM32 microcontroller and the download is verified successfully.
138 ◾ Digital Control of Power Converters
RawAdcVal
OffsetVal AdcRes
Vref
I actual 2 (3.18)
Sensitivity
2AdcRes 12
= 2= 4096 (3.23)
void CNTRL_ROUTINE_CURRENT(void){
// Iactual = adcValues[1]*0.001611328125;
Iactual = 25-
adcValues[1]*0.008056640625;
You should note that the Hall effect current sensors are very
prone to be affected by magnetic fields. In the next section, we will
discuss about average current mode control.
#ifndef INC_CUSTOM_H_
#define INC_CUSTOM_H_
void CNTRL_ROUTINE_VOLTAGE(void);
void CNTRL_ROUTINE_CURRENT(void);
void DOUB_LOOP(void);
void DOUB_LOOP_VOLTAGE(void);
void DOUB_LOOP_CURRENT(void);
#endif
You should note that the first function is for the entire double
loop controller, the second one is for the voltage control loop, and
the third one is for the current control loop.
In custom.c file, we define the loop counter variable (loop_cnt)
as external integer and add the codes inside the entire double loop
control function and call the double loop current control in every
iteration and double loop voltage control in every 10 iterations as
given in the following code.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include main.h
#include custom.h
#define A1_v 1
#define A2_v 0
#define A3_v 0
#define B0_v 0.001107
#define B1_v 0.002056
#define B2_v 0
#define B3_v 0
#define A1_i 1
#define A2_i 0
#define A3_i 0
#define B0_i 0.008334
142 ◾ Digital Control of Power Converters
void CNTRL_ROUTINE_CURRENT(void){
Iactual = adcValues[1]*0.001611328125;
y_i[3] = y_i[2];
y_i[2] = y_i[1];
y_i[1] = y_i[0];
e_i[3] = e_i[2];
e_i[2] = e_i[1];
e_i[1] = e_i[0];
e_i[0] = Iref – Iactual;
y_i[0] = A1_i*y_i[1] + A2_i*y_i[2] +
A3_i*y_i[3] + B0_i*e_i[0] + B1_i*e_i[1] +
B2_i*e_i[2] + B3_i*e_i[3];
if(y_i[0] > y_i_max){
y_i[0] = y_i_max;
}
else if(y_i[0] < y_i_min){
y_i[0] = y_i_min;
}
TIM1-
>CCR1 = (int)(MAX_DUTY*y_i[0]);
}
void DOUB_LOOP(void){
loop_cnt++;
if(loop_cnt == 10){
loop_cnt = 0;
DOUB_LOOP_VOLTAGE();
}
DOUB_LOOP_CURRENT();
}
void DOUB_LOOP_VOLTAGE(void);
void DOUB_LOOP_CURRENT(void);
You should note that the double loop current control function,
DOUB_LOOP_CURRENT, is identical to the current control
routine function, CNTRL_ROUTINE_CURRENT, however, the
double loop voltage control function, DOUB_LOOP_VOLTAGE,
is slightly different from the voltage control routine function,
CNTRL_ROUTINE_VOLTAGE. We do copy and paste the codes
inside the CNTRL_ROUTINE_VOLTAGE function to inside the
144 ◾ Digital Control of Power Converters
void DOUB_LOOP_VOLTAGE(void){
Vactual = adcValues[0]*0.008137207031;
y_v[3] = y_v[2];
y_v[2] = y_v[1];
y_v[1] = y_v[0];
e_v[3] = e_v[2];
e_v[2] = e_v[1];
e_v[1] = e_v[0];
e_v[0] = Vref – Vactual;
y_v[0] = A1_v*y_v[1] + A2_v*y_v[2] +
A3_v*y_v[3] + B0_v*e_v[0] + B1_v*e_v[1] +
B2_v*e_v[2] + B3_v*e_v[3];
if(y_v[0] > y_v_max){
y_v[0] = y_v_max;
}
else if(y_v[0] < y_v_min){
y_v[0] = y_v_min;
}
Iref = Iref_max*y_v[0];
}
void DOUB_LOOP_CURRENT(void);
void DOUB_LOOP_CURRENT(void){
Iactual = adcValues[1]*0.001611328125;
y_i[3] = y_i[2];
y_i[2] = y_i[1];
y_i[1] = y_i[0];
e_i[3] = e_i[2];
e_i[2] = e_i[1];
e_i[1] = e_i[0];
e_i[0] = Iref – Iactual;
y_i[0] = A1_i*y_i[1] + A2_i*y_i[2] +
A3_i*y_i[3] + B0_i*e_i[0] + B1_i*e_i[1] +
B2_i*e_i[2] + B3_i*e_i[3];
if(y_i[0] > y_i_max){
y_i[0] = y_i_max;
}
else if(y_i[0] < y_i_min){
y_i[0] = y_i_min;
}
TIM1-
>CCR1 = (int)(MAX_DUTY*y_i[0]);
}
So, the reference current, Iref, basically keeps the inductor cur-
rent in CCM, which is literally constant, remember that Iref is run-
ning for 10 cycles and the function DOUB_LOOP_CURRENT
runs for 10 times at first before start up the function DOUB_
LOOP_VOLTAGE, so you should note that adequate Iref should be
there and not be too high that is why we initiate it to 0.2 A first,
until it gets updated inside the function DOUB_LOOP_VOLTAGE,
146 ◾ Digital Control of Power Converters
Then, we can build the program and run the program to upload
the code to the STM32 microcontroller. Just keep in mind that the
system is not modifying the duty cycle directly as a result of the
sensed output voltage, it takes and generates a reference current
value from the output voltage and the reference current value is
what used to actually modify the duty cycle directly, so it is almost
indirect control instead of direct control. As you can see on the
oscilloscope, the output voltage is not constant and it is going up
and down from 11.3 V to 11.5 V, so you need to modify the voltage
and current controller coefficients. So, we go back to the code and
inside the main.c file we change the set point, Vref value, from 10 V
to 15 V. You should note that we can only change the set point Vref
Digital Control Implementation of the Buck Converter ◾ 147
example, we change the B0_v and B1_v and also B0_i and B1_i
values to one-tenth of their previous values in custom.c file as
given in the code below.
#define A1_v 1
#define A2_v 0
#define A3_v 0
#define B0_v 0.0001107
#define B1_v 0.0002056
#define B2_v 0
#define B3_v 0
#define A1_i 1
#define A2_i 0
#define A3_i 0
#define B0_i 0.0008334
#define B1_i 0.001548
#define B2_i 0
#define B3_i 0
#define A1_v 1
#define A2_v 0
#define A3_v 0
#define B0_v 0.0001107
#define B1_v 0.0002056
#define B2_v 0
#define B3_v 0
#define A1_i 1
#define A2_i 0
Digital Control Implementation of the Buck Converter ◾ 149
#define A3_i 0
#define B0_i 0.00008334
#define B1_i 0.0001548
#define B2_i 0
#define B3_i 0
Then, we run the program and can see that the output voltage
is now more stable and it is fixed at 18.1 V.
CHAPTER 4
Digital Control
Implementation with
PLECS
4.1 INTRODUCTION
PLECS is a simulation software tool used for the design and anal-
ysis of power electronic systems. It stands for piecewise linear
electrical circuit simulation and is developed by Plexim GmbH.
PLECS provides a user-friendly interface for modeling complex
power electronic systems and simulating their behavior in real-
time. Key features of PLECS include:
4.2 DESIGNING A CLOSED-LOOP
CONTROLLER IN PLECS AND MATLAB
In this section, we intend to design a buck converter in the PLECS
software environment and simulate it. The designed buck con-
verter is shown in Figure 4.1.
The circuit parameter values are clearly visible in Figure 4.1.
The switching frequency is 20 kHz and the triangular wave gen-
erator amplitude changes between 0 and 1, and it is compared
with constant duty cycle value with a logical comparator or rela-
tional operator to generate the PWM signal and, finally, it is fed to
152 ◾ Digital Control of Power Converters
FIGURE 4.1 The designed buck converter in PLECS.
Digital Control Implementation with PLECS ◾ 153
the gate driver of the MOSFET. Also, there is a load change at 100
ms and the load resistance is changing from 8 Ω to 4 Ω at the time
of 100 ms. The output voltage waveform is also depicted in Figure
4.2 which has an undesirable overshoot at the startup and there is
an offset or a deviation from the reference voltage value, 5 V, as
shown in Figure 4.2. The simulation parameters window for this
system is illustrated in Figure 4.3.
For designing a PID controller for this power converter, we
need to know the control to output frequency response of the
system. Therefore, in library browser, control, and small signal
analysis section, we do drag and drop the small signal pertur-
bation and add it with the reference voltage value and also do
drag and drop the small signal response as shown in Figure 4.4
and connect it to the measured output voltage as shown in
Figure 4.1.
154 ◾ Digital Control of Power Converters
>> H = tfest(sys,2)
H =
2497 s + 5.186e08
-----------------------
s^2 + 4201 s + 5.639e08
Continuous-time identified transfer function.
Parameterization:
Number of poles: 2 Number of zeros: 1
Number of free coefficients: 4
Use "tfdata", "getpvec", "getcov" for
parameters and their uncertainties.
Status:
Estimated using TFEST on frequency response data
"sys".
Fit to estimation data: 99.2% (simulation focus)
FPE: 7.625e-05, MSE: 7.07e-
05
>> pidTuner(H)
>> PID
PID =
1
Ki * ---
s
with Ki = 18.5
Continuous-
time I-
only controller.
162 ◾ Digital Control of Power Converters
18.5
PID s (4.2)
s
FIGURE 4.16 Setting the upper and lower saturation limits in the anti-
windup tab.
164 ◾ Digital Control of Power Converters
FIGURE 4.17 Adding the PID(s) block to the closed-loop control system.
Digital Control Implementation with PLECS ◾ 165
By following these steps, you can effectively generate code for the
STM32 Nucleo-G474RE microcontroller in PLECS and imple-
ment the control system on the microcontroller for real-world
applications. Now, we are going to create the same buck converter
using the STM32 PLECS library and then generate the code for
the STM32 Nucleo-G474RE microcontroller in STM32CubeIDE.
To do that, we create two subsystems one for the buck converter
and the other one for the PID controller as shown in Figure 4.19.
The circuit inside the Buck_Converter subsystem is depicted in
Figure 4.20.
As you can see in Figure 4.20, we have added a resistor divider
with the values R5 = 100 kΩ and R6 = 10 kΩ, since we want to get
FIGURE 4.19 Creating two subsystems for buck converter and PID
controller.
Digital Control Implementation with PLECS ◾ 167
FIGURE 4.20 The circuit inside the Buck_Converter subsystem.
168 ◾ Digital Control of Power Converters
R6 1
Vosense Vo Vo (4.3)
R5 R6 11
and model our controller. PLECS does offer other tools that fur-
ther support the simulation development and testing of power
electronics controllers. In this section, the application that we’re
going to be using to demonstrate the built-in analysis tools is
an auxiliary-based power supply based on the flyback converter
topology as shown in Figure 4.44.
So, because we will be saving some time by using a few prebuilt
models, we’ll take a moment here to review what’s already been
developed. As we open new models, we’ll explain the changes
that we’ve made. So hopefully it’s easy for you to follow the flow.
As an input to the converter, here in Figure 4.44, we have a single-
phase rectifier. We’re using a simplified high-frequency trans-
former with three output windings, 5 V and plus and minus 12 V.
The total output power is around 30 W. This is a very low-cost
simple implementation. It’s a popular choice for power supply
designs. The topology provides isolation between the primary
and the secondary sides, and it also then gives the designer the
ability to provide multiple outputs for different voltage levels and
a choice of positive or negative voltage for the output. So, during
this section, we will add an inner peak current controller, which
will regulate the peak MOSFET current and then also a voltage
controller to regulate the 5-V winding. As we mentioned, we’re
going to present these control analysis tools within the context of
a design example. So, for the current and voltage control loop
design of the flyback converter, we can use the procedures
depicted in Figure 4.45.
This section will use a design flow that works well within the
PLECS modeling environment using these tools. There are of
course other approaches and methodologies that are possible.
So as a starting point, we will determine the small signal trans-
fer function of the current-controlled converter in the form of
generating a Bode plot, and this will then allow us to design a
voltage controller and then subsequently calculate the loop gain
Digital Control Implementation with PLECS ◾ 181
FIGURE 4.44 The topology of auxiliary-based power supply.
182 ◾ Digital Control of Power Converters
FIGURE 4.45 The current and voltage control loop design of the flyback
converter.
and as shown in Figure 4.48 that the startup transients are really
not very smooth. Hence, we need to add a voltage controller.
This is what we’re going to be working on. In the PLECS library
browser, we’re going to add components in our library to our
schematic and to implement our control architecture. So first, we
should replace the pulse generator block with the peak current
controller that we’ve been talking about. So, we can use the search
function and search for the component and then we drag it into
our schematic. First, we’ll connect the switch signal to the
MOSFET, and now we can right click this component as with all
components in the library that are subsystems. You can select the
look under mask option as shown in Figure 4.49, and you can see
the implementation as in Figure 4.46, which we’ve modeled it and
you can then modify it as you would for your own application.
So, we can see here the behavior described in Figure 4.46. If we
double click the peak current controller, we can see the masked
subsystem parameters, so the only default parameters here that
we’re going to change are just the duty cycle, which we will set to
186 ◾ Digital Control of Power Converters
FIGURE 4.49 Selecting the look under mask option.
Digital Control Implementation with PLECS ◾ 187
50%. We’ll check the boxes, which will display the parameters on
the schematic for us to see as depicted in Figure 4.50.
There we go, so next, what we want to do is we want to create a
current sense signal to feed into our Isense of the peak current con-
troller. So, we’re going to do that by simply placing a resistor com-
ponent, which we’ll grab from our passive electrical library and
place that into our schematic, and we’ll wire it up. We will change
the resistor value to 0.5 ohms and we can display that. Now, we
also need a voltmeter component. Again, utilizing the search
function and we wire it up as illustrated in Figure 4.51.
So, here we go, and we’ve routed now our Isense signal, and now
for the reference current signal, Iref, we’re actually going to first use
a constant block, so we’re going to specify a set point value here,
0.1 amps as you can see in Figure 4.51. This is going to limit our
actual peak inductor current to 10 amps and it’s going to create an
188 ◾ Digital Control of Power Converters
FIGURE 4.51 Wiring up the peak current controller signals.
Digital Control Implementation with PLECS ◾ 189
So, we’re going to start this analysis, and then we’re going to
explain a few more things. So, that was very quick, we’re done,
that analysis was super- fast, and that’s because the impulse
response analysis is actually the fastest method that we have of
generating a Bode plot as shown in Figure 4.57.
When we run the impulse response analysis, it actually first
runs a steady-state analysis in order to find the stable operating
point of the system. The steady-state analysis is based on a Newton
Raphson iterative technique, and it may not converge when the
system is run from startup because the system is under damped,
and the state variables in PLECS are going to be too far from the
final operating point. So, the simplest method of addressing this
problem is to simulate the system for a specified number of initial-
ization cycles before the steady-state analysis is run. If we go back
to our configuration settings and we go to the steady-state options,
194 ◾ Digital Control of Power Converters
we can see here in Figure 4.58 that we have the ability to specify
this number of initialization cycles.
This then causes the Jacobian matrix to be calculated from
an operating point that is closer to the steady-state operating
point, which increases the likelihood of convergence in the
under-damp system and thus allowing the analysis to run to
completion. So, that’s our analysis, so we can go back to this
frequency response. We can see the frequency response here of
our current controlled flyback converter. We’re now going to
save this data. So, we’ll go file export as a CSV file as depicted
in Figure 4.59.
So, we’ll save it as flyback.csv, so now at this stage, we are ready
to start talking about that voltage controller. So, what we’re going
to use MATLAB for a PID controller design. So, in the MATLAB
command window, we write the following codes.
sys = frd(val,w);
bode(sys),grid minor
G = tfest(sys,3)
G =
-4.024e04 s^2 + 5.83e09 s + 1.338e12
------------------------------------------
s^3 + 2.596e05 s^2 - 4.657e08 s - 6.749e12
Continuous-
time identified transfer function.
Parameterization:
Number of poles: 3 Number of zeros: 2
Number of free coefficients: 6
196 ◾ Digital Control of Power Converters
pidTuner(G)
>> Cg
Cg =
1
Kp + Ki * --- + Kd * s
s
with Kp = 2.22, Ki = 4.78e+04, Kd = 1.23e-
05
Continuous-
time PID controller in parallel
form.
FIGURE 4.65 The output voltage waveforms during the load step.
FIGURE 4.68 The output voltage waveforms with STM32 target elements.
204 ◾ Digital Control of Power Converters
FIGURE 4.69 Selecting the execution settings… option.
Digital Control Implementation with PLECS ◾ 205
208
Index
Note: Pages in italics refer to figures and pages in bold refer to tables.
A B
ACS712 current sensor, 138 bilinear transformation, 16, 48
AmpGain, 127 Bode plots
Arduino demonstration closed-loop transfer function
DC gearmotor’s, Serial Plotter, of Gid, 96, 96
76, 76 of Gpci_mat, 97, 97–98, 99
Measured_Speed, 77, 77 of Gpcv_mat, 102, 102
script, 70–75 of Gpcv_pm, 103, 104
set point speed tracking, of Gvd, 100, 101
70, 70 of Tpci_mat, 98, 98, 100, 100
simulated data, 78, 79 of Tpcv_mat, 102, 103
simulink block diagram, closed- of Tpcv_pm, 103, 104, 105
loop feedback system, 69, 70 CSV file, 194, 195
theoretical result and measured of impulse response analysis, 193, 193
result, 78, 80, 80 open voltage loop, 191
Arduino IDE’s Serial Plotter, 29, 29 buck converter
ARR, see Auto Reload Register (ARR) CCM, 90
Auto Reload Register (ARR), 123 compensator phase, 91, 93–94
average current mode control, buck controller design with OCTAVE
converter crossover frequency, 108
CCM, 147 current and voltage controller
double loop current control design, 110
function, 143–146 current and voltage controller
implementation of, 140, 147 transfer functions,
loop counter variable, 140–143 110–111
output voltage/output current, gain and phase margins,
139–140 111–117
209
210 ◾ Index
MATLAB, 18 I
natural logarithm, 15–16
INA180, 121, 127
root locus, 14
Integrated Development Environment
Tustin’s method, 14, 16–17, 17
(IDE), 18
Z-transform, 10
inverse Z-transform, 16, 25, 48, 68–69
closed-loop feedback diagram,
13
difference equation, 14 K
Kirchhoff ’s Current Law
(KCL), 10 Kirchhoff ’s Current Law (KCL), 10
Laplace transform, 8, 8–11, 11
s-plane and z-plane, 11, 12 M
type II compensator, 4, 9
digital feedback control algorithm MATLAB, 18–19, 22, 58, 61, 157
control structure, 38, 38 Bode diagram of, 159
‘if ’ statement, 37 ‘bode’ function, 20
implementation, 35, 35–36 c2d function, 68, 86, 89
impulse function, 38, 38 closed-loop controller in, see
ZOH block operation, 37, 37–38 closed-loop controller in
double loop control, 139, 141 PLECS and MATLAB
duty cycle method, 140 closed-loop PID controller,
160–161
control design tools, 64, 66
E current control loop, 84
explicit transfer functions, 64, 64 frequency responses, 24
measured output speed, 77, 77
Octave, 108
F phase margin-based frequency
firmware response, 95
implementation PI controllers, pidTuner tool, 95,
of current control, 134–139 97, 101, 105, 105, 106, 113
of voltage control, 129–134, 131 PID controller design, 194, 197
manual initialization, 124–129, 125 rational transfer function, 65
peripheral initialization with two-dimensional variable, 31
CubeMX, 121–124 MATLAB Simulink, 30, 54–55, 77, 105
Metal-Oxide-Semiconductor Field-
Effect Transistor (MOSFET),
G 108, 118–120, 139–140, 153,
Google Colab code, 95 162, 180, 183, 185
H N
Hall effect current sensor, 138 Newton’s second law, 52
214 ◾ Index