Low Cost PID Controller for Student Digital Control Laboratory Based on Arduino or STM32 Modules

: In the teaching process, it is important that students do not carry out exercises only by computer simulations, but also that they carry out research in real time. In times of distance learning during the COVID-19 pandemic, it would be necessary to ﬁnd a solution so that the students can perform such exercises individually at home. Therefore, it has become necessary to develop cheap and simple modules of digital controllers along with analog objects with adjustable order and time constants. This paper describes a low-cost proportional–integral–derivative (PID) controller for teaching students control techniques and analog control objects in real time. The PID controller is based on the cheap and widely available microcontroller modules Arduino or STM32. The advantage of this solution is that the algorithm of the digital PID controller is calculated every constant period of time. Both the solutions presented in the paper have been successfully tested by students in practice during remote learning during the COVID-19 pandemic.


Introduction
The student digital control laboratory aims to teach the student how to design and implement automatic control systems.These systems have many practical uses, such as in cars, planes, robots, factories, processes, space structures, and more.The student starts by learning the basic software and hardware for control systems and the technologies for sensors and actuators.Then, the student creates a control system, tests it in one of the laboratory platforms, and checks whether it meets the design requirements.
Typically in the student digital control laboratory, programs such as Matlab, Matlab-Simulink, Octave, LabView, Psim, Pspice, etc., are used for simulation tests.Performing such simulation tests is a very good complement to the theoretical knowledge gained during lectures.In the course of further research, it is necessary to supplement the knowledge related to the implementation of digital control algorithms in real control systems.
My many years of teaching practice show that during the process of teaching about automatic control systems, the contact of students with a live automatic control system is also important.Such contact works better on the imagination of students and allows a better understanding of the theoretical dependencies specific to automatic control systems.
For this purpose, ready-made modules specially designed for this type of laboratory are used.For example, these can be modules from National Instruments NI myRIO [1] or Speedgoat systems [2].Unfortunately, such systems are quite expensive and it is difficult to persuade students to use them in the case of distance learning, as was the case during the COVID-19 pandemic.
Attention should also be paid to entire families of microcontrollers from Microchip to 8-bit PIC MCUs, 16-bit MCUs, dsPIC33 digital signal controllers and 32-bit MCUs [11].For student activities, one can also use the Microchip University Program [12].For rapid prototyping, Microchip has prepared the Curiosity Nano Platform [13].A good implementation of the PID controller can be found in [14].
Among the cheapest of these are Arduino and STM32, which is why they were chosen.The low prices of microcontroller modules allow the adoption of a solution in which each student has his/her own module with a microcontroller.There is an advantage to using Arduino modules, i.e., the use of a simplified programming environment.This makes it easier to work with students for whom computer science is not their main field of study, for example, students of electrical engineering.However, Arduino software, due to its simplifications, does introduce some limitations.
In the case of modules with STM32 microcontrollers, the manufacturer's software STM32CubeIDE can be used [10].The STM32CubeIDE is an advanced C/C++ development platform with peripheral configuration, code generation, code compilation, and debugging capabilities for STM32 microcontrollers and microprocessors.It is based on the Eclipse ® /CDT™ framework, and the GCC toolchain for development and GDB for debugging [10].The development platform is no longer so simple for students, and requires them to be more involved.It should be noted, however, that the capabilities of ARM microcontrollers are much greater than those of AVR.
The paper focuses on the implementation of the proportional-integral-derivative (PID) controller using Arduino Uno and STM32.On the Arduino website one can find many functions with ready-made programs to implement the PID controller [15][16][17][18][19][20][21][22][23][24][25][26][27][28].All of these execute the PID algorithm in the main loop of the program, and the sampling period is variable and depends on the complexity of the main program.For the correct determination of the integral and differential, the system time is taken and the time since the last call of the PID algorithm is calculated.This is only acceptable for slowly-varying simple control objects.For more demanding control objects such as power electronics, circuits, quadcopters, etc., this solution cannot be used.
The proposed solution uses a fixed sampling period.Thanks to this, the big value of jitter and the formation of beating output signals can be avoided, which significantly improves the quality of control.In this way, students also learn the correct methods of programming systems working in real time.
Another problem to be solved is the realization of the controlled systems.It was assumed that the time constants of controlled systems would be from one to several seconds, and from one to several orders.For simplicity, it was also assumed that the controlled systems will be powered directly by the microcontroller module.Adopting such a solution is safe because students do not always cope well with systems powered by many sources.In the simplest solution, simple passive RC systems were used as an object.It is also possible to use the active version of the controlled system, using rail-to-rail operational amplifiers, also powered by the microcontroller module.This solution allows for the implementation of a wider range of transmittance compared to passive systems.
During the tests, it is also necessary to measure and record signals; for this purpose, one of the cheap USB oscilloscopes can be used.The Serial Analyzer can be used if an Arduino module is being used [7].
In Section 2, an example of converting the analog PID controller to a digital version is presented.Simple object simulators are described in Section 3. Section 4 is devoted to the implementation of the digital PID controller using the Arduino Uno module.Examples of laboratory test results are also shown.Section 5 is devoted to the implementation of the digital PID controller using the STM32 microcontroller.Also, in this case, sample results of laboratory tests are shown.Section 6 briefly discusses the methods for selecting the parameters of the PID controller.

Realization of Digital PID Controller
A block diagram of the automatic control system is shown in Figure 1.The system consists of a controller with transfer function G(s) of an object (plant, controlled system, system) with transfer function H(s) and a summation node in which the setpoint X(s) is compared with the output signal Y(s) [29][30][31][32][33][34].
Electronics 2023, 12, 3235 3 of 23 results of laboratory tests are shown.Section 6 briefly discusses the methods for selecting the parameters of the PID controller.

Realization of Digital PID Controller
A block diagram of the automatic control system is shown in Figure 1.The system consists of a controller with transfer function G(s) of an object (plant, controlled system, system) with transfer function H(s) and a summation node in which the setpoint X(s) is compared with the output signal Y(s) [29][30][31][32][33][34]. - x Figure 1.A feedback loop with standard analog PID controller.
For this control system, a system of equations can be written, where the following pertains: X(s)-desired process value or setpoint; E(s)-error value as the difference between a desired setpoint X(s) and the measured process value Y(s), E(s) = X(s) − Y(s); G(s)-controller transfer function; H(s)-transfer function of controlled system (plant); Y(s)-measured process value; U(s)-control variable.
From the system of Equation (1) it is possible to determine the transfer function of a closed loop control system The block diagram of a PID controller is shown in Figure 1.The transfer function of the analog PID controller transmittance of the regulator is determined by the equation where the following pertains: kc-controller gain, a tuning parameter; Td-derivative time, a tuning parameter; Ti-integral time, a tuning parameter.
For a PID controller, the control value U(s) consists of three terms: Up(s)-proportional term, Ui(s)-integral term, Ud(s)-derivative term.The value of the PID controller control variable can be determined using the equation For this control system, a system of equations can be written, where the following pertains: X(s)-desired process value or setpoint; E(s)-error value as the difference between a desired setpoint X(s) and the measured process value Y(s), E(s) = X(s) − Y(s); G(s)-controller transfer function; H(s)-transfer function of controlled system (plant); Y(s)-measured process value; U(s)-control variable.
From the system of Equation (1) it is possible to determine the transfer function of a closed loop control system The block diagram of a PID controller is shown in Figure 1.The transfer function of the analog PID controller transmittance of the regulator is determined by the equation where the following pertains: k c -controller gain, a tuning parameter; T d -derivative time, a tuning parameter; T i -integral time, a tuning parameter.
For a PID controller, the control value U(s) consists of three terms: U p (s)-proportional term, U i (s)-integral term, U d (s)-derivative term.The value of the PID controller control variable can be determined using the equation Integral term ( 4) is transferred into digital form using bilinear transformation where T s is the sampling period.
The derivative term is transferred to digital form using backward difference transformation Finally, the digital control variable can be determined from the equation while the value of the digital control variable can be calculated using the difference equation The block diagram of such a digital PID controller is depicted in Figure 2.
() = () ( ) Integral term ( 4) is transferred into digital form using bilinear transformation where Ts is the sampling period.
The derivative term is transferred to digital form using backward difference transformation Finally, the digital control variable can be determined from the equation while the value of the digital control variable can be calculated using the difference equation The block diagram of such a digital PID controller is depicted in Figure 2.  It was assumed that in order to simplify the implementation, the PID controller algorithm will be implemented using floating point arithmetic.A range of variability of variables of −1.0. ..1.0 was also assumed.In addition, saturation of the variables u i (n) and u(n) has been added to the algorithm.
The digital control variable u(n) is typically converted to an analog form by a D/A converter.Often, as in this case, a PWM modulator is also used as a D/A converter.Before D/A conversion, the control variable u(n) should be scaled to the range of the D/A converter and cast to the integer type where N L is the number of states of the digital PWM modulator or D/A converter.The variable range u N (n) is 0 . . .N L − 1.The block diagram of the digital PID controller with saturation, and with a PWM modulator on the output, is shown in Figure 3.This variant of the regulator was adopted for implementation using microcontrollers.Of course, in the literature, it is possible to find many other implementations [29][30][31][32][33][34] of the digital PID controller that can be used here.
Electronics 2023, 12, 3235 5 of 23 It was assumed that in order to simplify the implementation, the PID controller algorithm will be implemented using floating point arithmetic.A range of variability of variables of −1.0…1.0 was also assumed.In addition, saturation of the variables ui(n) and u(n) has been added to the algorithm.
The digital control variable u(n) is typically converted to an analog form by a D/A converter.Often, as in this case, a PWM modulator is also used as a D/A converter.Before D/A conversion, the control variable u(n) should be scaled to the range of the D/A converter and cast to the integer type where NL is the number of states of the digital PWM modulator or D/A converter.The variable range uN(n) is 0 … NL − 1.The block diagram of the digital PID controller with saturation, and with a PWM modulator on the output, is shown in Figure 3.This variant of the regulator was adopted for implementation using microcontrollers.Of course, in the literature, it is possible to find many other implementations [29][30][31][32][33][34] of the digital PID controller that can be used here.
Figure 3.The block diagram of the digital PID controller with saturation, and with the PWM modulator on the output.
For example, by performing a simple modification of the algorithm, one can eliminate the effect of changing the setpoint on the derivative term.The block diagram of such a solution is shown in Figure 4.For example, by performing a simple modification of the algorithm, one can eliminate the effect of changing the setpoint on the derivative term.The block diagram of such a solution is shown in Figure 4.It was assumed that in order to simplify the implementation, the PID controller algorithm will be implemented using floating point arithmetic.A range of variability of variables of −1.0…1.0 was also assumed.In addition, saturation of the variables ui(n) and u(n) has been added to the algorithm.
The digital control variable u(n) is typically converted to an analog form by a D/A converter.Often, as in this case, a PWM modulator is also used as a D/A converter.Before D/A conversion, the control variable u(n) should be scaled to the range of the D/A converter and cast to the integer type where NL is the number of states of the digital PWM modulator or D/A converter.The variable range uN(n) is 0 … NL − 1.The block diagram of the digital PID controller with saturation, and with a PWM modulator on the output, is shown in Figure 3.This variant of the regulator was adopted for implementation using microcontrollers.Of course, in the literature, it is possible to find many other implementations [29][30][31][32][33][34] of the digital PID controller that can be used here.For example, by performing a simple modification of the algorithm, one can eliminate the effect of changing the setpoint on the derivative term.The block diagram of such a solution is shown in Figure 4.It was assumed that the sampling frequency of f s signals is constant and that the algorithm of the digital PID controller is calculated with the same frequency.

Controlled System Simulators
It was assumed that the regulator together with the controlled system must be resistant to assembly errors, easy to implement, and cheap.Therefore, it was assumed that the controlled system should be powered by the microcontroller module.Therefore, the simplest RC systems were used to simulate dynamic objects.Figure 5 shows a two-stage RC network that forms a second-order controlled system.This circuit is limited because its quality factor Q is always less than 0.5, with R 1 = R 2 and C 1 = C 2 , Q = 1/3.In this circuit, Q approaches the maximum value of 1/2 when the impedance of the second RC stage is much larger than the first.
Electronics 2023, 12, 3235 6 of 23 It was assumed that the sampling frequency of fs signals is constant and that the algorithm of the digital PID controller is calculated with the same frequency.

Controlled System Simulators
It was assumed that the regulator together with the controlled system must be resistant to assembly errors, easy to implement, and cheap.Therefore, it was assumed that the controlled system should be powered by the microcontroller module.Therefore, the simplest RC systems were used to simulate dynamic objects.Figure 5 shows a two-stage RC network that forms a second-order controlled system.This circuit is limited because its quality factor Q is always less than 0.5, with R1 = R2 and C1 = C2, Q = 1/3.In this circuit, Q approaches the maximum value of 1/2 when the impedance of the second RC stage is much larger than the first.
Second-order passive controlled system simulator.
The transfer function of the two-stage RC network can also be expressed in Larger values of the quality factor Q are attainable using a positive feedback amplifier.Figure 6 shows an operational amplifier used in this manner.Capacitor C1, no longer connected to the ground, provides a positive feedback path.These circuits were described in 1955 by R. P. Sallen and E. L. Key, and hence they are generally known as Sallen-Key filters [35,36].The operational amplifier is a rail-to-rail input-output, and is supplied by a microcontroller module (+5 V for Arduino, and 3.3 V for STM32).The transfer function of the second-order active object simulator for the position of the switch S1 in the on position is The transfer function of the two-stage RC network can also be expressed in Larger values of the quality factor Q are attainable using a positive feedback amplifier.Figure 6 shows an operational amplifier used in this manner.Capacitor C 1 , no longer connected to the ground, provides a positive feedback path.These circuits were described in 1955 by R. P. Sallen and E. L. Key, and hence they are generally known as Sallen-Key filters [35,36].The operational amplifier is a rail-to-rail input-output, and is supplied by a microcontroller module (+5 V for Arduino, and 3.3 V for STM32).
Electronics 2023, 12, 3235 6 of 23 It was assumed that the sampling frequency of fs signals is constant and that the algorithm of the digital PID controller is calculated with the same frequency.

Controlled System Simulators
It was assumed that the regulator together with the controlled system must be resistant to assembly errors, easy to implement, and cheap.Therefore, it was assumed that the controlled system should be powered by the microcontroller module.Therefore, the simplest RC systems were used to simulate dynamic objects.Figure 5 shows a two-stage RC network that forms a second-order controlled system.This circuit is limited because its quality factor Q is always less than 0.5, with R1 = R2 and C1 = C2, Q = 1/3.In this circuit, Q approaches the maximum value of 1/2 when the impedance of the second RC stage is much larger than the first.The transfer function of the two-stage RC network can also be expressed in Larger values of the quality factor Q are attainable using a positive feedback amplifier.Figure 6 shows an operational amplifier used in this manner.Capacitor C1, no longer connected to the ground, provides a positive feedback path.These circuits were described in 1955 by R. P. Sallen and E. L. Key, and hence they are generally known as Sallen-Key filters [35,36].The operational amplifier is a rail-to-rail input-output, and is supplied by a microcontroller module (+5 V for Arduino, and 3.3 V for STM32).The transfer function of the second-order active object simulator for the position of the switch S1 in the on position is The transfer function of the second-order active object simulator for the position of the switch S 1 in the on position is Electronics 2023, 12, 3235 where k-is the plant gain.
Using the circuits shown above, it is possible to build higher-order systems.

Realization of Digital PID Controller Using Arduino Module
The most popular Arduino Uno [7] module was used to implement the digital PID controller.An internal 10-bit A/D converter is used to convert the y(t) signal and a PWM modulator was used as an 8-bit D/A converter to convert the u(t) control variable signal.
The basic source of information about the Arduino system is the webpage arduino.cc[7], but other sites are also worth viewing, for example, Gammon Forum contains a lot of interesting solutions for Arduino modules [37].
In a "standard" C/C++ program, a "main" function should be present.This main function will be called invoked, and from there, one calls up other functions and executes the functionalities of the program.In Arduino, there is no main function.This is replaced by two the functions setup() and loop() [7].These two functions must be present in every Arduino sketch.Other functions must be created outside the brackets of those two functions.The setup() function will only run once, after each powerup or reset of the Arduino board.It is used to initialize variables, pin modes, start using libraries, etc., whereas function loop() loops consecutively.In the function loop(), the main program should be written, with the understanding that the initialization is already done.In this function, one must always keep in mind that the last line is followed by the first line.In this solution, the frequency of invoking the loop() function depends on its length, and it is also important that this frequency is not constant.Depending on (6) for the calculation of the control variable, there is a sampling period T s .
To solve this problem, a solution is proposed that provides a constant sampling period of T s .A sampling period equal to the period of the PWM modulator for pin 5 is assumed, and this period is equal to 1.024 ms.The A/D converter is directly triggered by the microcontroller counter, and an interrupt is generated when the conversion is complete.In this interrupt, the entire algorithm of the regulator is calculated and a new value of u(n) is sent to the PWM modulator.The timing diagram of data flow in the digital PID controller is depicted in Figure 7.The advantage of this solution is that the algorithm of the digital PID controller is calculated every constant period of time.

Arduino Uno
Interrupt f s = 976.17Hz f s = 976.17Hz x(n) The block diagram of the controller based on Arduino Uno is depicted in Figure 8.A skeleton version of the program that implements the PID controller is shown in Listing 1, while a detailed version of the program is included in Appendix A. The setup() function includes the initialization of all hardware.The entire program of the PID controller has been placed in the function ISR (ADC_vect), which handles the interrupt generated by the A/D converter.The loop() function, in this case, can be left empty.The ISR (ADC_vect) function can be used instead of loop(), but it must be kept in mind that the time it takes to execute the entire algorithm cannot be longer than the sampling period.Therefore, one should check the execution time with an oscilloscope.9 shows the time waveform for pin 13 of the microcontroller; it is set to logic one at the beginning of the interrupt handling procedure and reset at the end.It shows that despite the use of a floating point for calculations, the entire interrupt handling procedure takes about one-fifth of the sampling period.
Figure 10 shows a simplified schematic diagram of the experimental control system.In the system, a cascade connection of two RC networks was used as a controlled system.Both networks have the ability to adjust the time constant using potentiometers.Additionally, a second PWM modulator (pin 6) and R 3 C 3 network have been used as an additional analog output.In this case, the PWM modulator is used to image the e(t) signal.The view of the control system is shown in Figure 11.It should be noted that the entire system is supplied with a DC voltage of +5 V; therefore, all signals are in the range of 0. ..+5V. Figure 9 shows the time waveform for pin 13 of the microcontroller; it is set to logic one at the beginning of the interrupt handling procedure and reset at the end.It shows that despite the use of a floating point for calculations, the entire interrupt handling procedure takes about one-fifth of the sampling period.Figure 10 shows a simplified schematic diagram of the experimental control system.In the system, a cascade connection of two RC networks was used as a controlled system.Both networks have the ability to adjust the time constant using potentiometers.Additionally, a second PWM modulator (pin 6) and R3C3 network have been used as an additional analog output.In this case, the PWM modulator is used to image the e(t) signal.The view of the control system is shown in Figure 11.It should be noted that the entire system is supplied with a DC voltage of +5 V; therefore, all signals are in the range of 0…+5 V.
An example of experimental time waveforms using a PI controller (closed-loop) with a first-order object for a step change of setpoint x(t) from 0.2 to 0.8 is shown in Figure 12, which shows the time waveforms of the controller error e(t) and the measured value of the process y(t).Figure 10 shows a simplified schematic diagram of the experimental control system.In the system, a cascade connection of two RC networks was used as a controlled system.Both networks have the ability to adjust the time constant using potentiometers.Additionally, a second PWM modulator (pin 6) and R3C3 network have been used as an additional analog output.In this case, the PWM modulator is used to image the e(t) signal.The view of the control system is shown in Figure 11.It should be noted that the entire system is supplied with a DC voltage of +5 V; therefore, all signals are in the range of 0…+5 V.
An example of experimental time waveforms using a PI controller (closed-loop) with a first-order object for a step change of setpoint x(t) from 0.2 to 0.8 is shown in Figure 12, which shows the time waveforms of the controller error e(t) and the measured value of the process y(t).An example of experimental time waveforms using a PI controller (closed-loop) with a first-order object for a step change of setpoint x(t) from 0.2 to 0.8 is shown in Figure 12, which shows the time waveforms of the controller error e(t) and the measured value of the process y(t).By modifying the program from Listing A1, it can also be used to identify the param eters of the controlled system; for example, performing a test for a step response of an open-loop system.
For programming and debugging, the typical Arduino IDE 2 (Integrated Develop ment Environment) is used.Among the many useful features of this environment, th Serial Plotter function should be mentioned [7].It allows us to visualize the variables sen by the serial port.Figure 13  By modifying the program from Listing A1, it can also be used to identify the parameters of the controlled system; for example, performing a test for a step response of an open-loop system.
For programming and debugging, the typical Arduino IDE 2 (Integrated Development Environment) is used.Among the many useful features of this environment, the Serial Plotter function should be mentioned [7].It allows us to visualize the variables sent by the serial port.Figure 13

Realization of Digital PID Controller Using STM32G491RE-Nucleo
The STM32 family of 32-bit microcontrollers is based on the ARM Cortex ® -M proces sor from the STMicroelectronics company.STM32 microcontrollers combine very high performance, real-time capabilities, digital signal processing, low power and connectivity while maintaining full integration and ease of development [10].
The company's offer includes a whole range of development boards [10].A low-cos

Realization of Digital PID Controller Using STM32G491RE-Nucleo
The STM32 family of 32-bit microcontrollers is based on the ARM Cortex ® -M processor from the STMicroelectronics company.STM32 microcontrollers combine very high performance, real-time capabilities, digital signal processing, low power and connectivity, while maintaining full integration and ease of development [10].
The company's offer includes a whole range of development boards [10].A low-cost module equipped with a 12-bit A/D converter and a 12-bit D/A converter, STM32G491RE-Nucleo, was chosen to implement the digital PID controller.The STM32 Nucleo-64 development board with STM32G491RE microcontroller supports Arduino and ST morpho connectivity [10,58].Figure 14 shows the view of the STM32G491RE-Nucleo board.

Realization of Digital PID Controller Using STM32G491RE-Nucleo
The STM32 family of 32-bit microcontrollers is based on the ARM Cortex ® -M processor from the STMicroelectronics company.STM32 microcontrollers combine very high performance, real-time capabilities, digital signal processing, low power and connectivity, while maintaining full integration and ease of development [10].
The company's offer includes a whole range of development boards [10].A low-cost module equipped with a 12-bit A/D converter and a 12-bit D/A converter, STM32G491RE-Nucleo, was chosen to implement the digital PID controller.The STM32 Nucleo-64 development board with STM32G491RE microcontroller supports Arduino and ST morpho connectivity [10,58].Figure 14 shows the view of the STM32G491RE-Nucleo board.

Realization of Digital PID Controller Using STM32G491RE-Nucleo
The STM32 family of 32-bit microcontrollers is based on the ARM Cortex ® -M processor from the STMicroelectronics company.STM32 microcontrollers combine very high performance, real-time capabilities, digital signal processing, low power and connectivity, while maintaining full integration and ease of development [10].
The company's offer includes a whole range of development boards [10].A low-cost module equipped with a 12-bit A/D converter and a 12-bit D/A converter, STM32G491RE-Nucleo, was chosen to implement the digital PID controller.The STM32 Nucleo-64 development board with STM32G491RE microcontroller supports Arduino and ST morpho connectivity [10,58].Figure 14 shows the view of the STM32G491RE-Nucleo board.For programming and debugging, the typical STM32CubeIDE ver.1.11.2.(Integrated Development Environment) is used.STM32Cube includes STM32CubeMX, a graphical software configuration tool that allows the generation of C initialization code using graphical wizards, according to the hardware configuration of our board.For example, if we have the STM32G491RE-Nucleo, which is based on the STM32G491RE microcontroller, and we want to use its user LED (marked as LD2 on the board), then STM32CubeMX will automatically generate all source files containing the C code required to configure the microcontroller (clock, peripheral ports, and so on) and the GPIO connected to the LED.
The block diagram of the digital PID controller based on STM32G491RE-Nucleo is depicted in Figure 15.The microcontroller is powered by +3.3 V, so all signals in the system are in the range of 0. . .+3.3 volts.crocontroller (clock, peripheral ports, and so on) and the GPIO connected to the LED The block diagram of the digital PID controller based on STM32G491RE-Nucle depicted in Figure 15.The microcontroller is powered by +3.3 V, so all signals in the sys are in the range of 0… +3.3 volts.
As in the case of the implementation of the digital controller using Arduino, the en algorithm is executed when handling the interrupt from the A/D converter.Due to assumed sampling frequency of fs = 10 kHz, the time available for calculations is less t 100 µs.A timing diagram of data flow in the digital PID controller based STM32G491RE-Nucleo is shown in Figure 16.As in the case of the implementation of the digital controller using Arduino, the entire algorithm is executed when handling the interrupt from the A/D converter.Due to the assumed sampling frequency of f s = 10 kHz, the time available for calculations is less than 100 µs.A timing diagram of data flow in the digital PID controller based on STM32G491RE-Nucleo is shown in Figure 16.
For programming and debugging, the typical STM32CubeIDE ver.1.11.2.(Integrated Development Environment) is used.STM32Cube includes STM32CubeMX, a graphical software configuration tool that allows the generation of C initialization code using graphical wizards, according to the hardware configuration of our board.For example, if we have the STM32G491RE-Nucleo, which is based on the STM32G491RE microcontroller, and we want to use its user LED (marked as LD2 on the board), then STM32CubeMX will automatically generate all source files containing the C code required to configure the microcontroller (clock, peripheral ports, and so on) and the GPIO connected to the LED.
The block diagram of the digital PID controller based on STM32G491RE-Nucleo is depicted in Figure 15.The microcontroller is powered by +3.3 V, so all signals in the system are in the range of 0… +3.3 volts.
As in the case of the implementation of the digital controller using Arduino, the entire algorithm is executed when handling the interrupt from the A/D converter.Due to the assumed sampling frequency of fs = 10 kHz, the time available for calculations is less than 100 µs.A timing diagram of data flow in the digital PID controller based on STM32G491RE-Nucleo is shown in Figure 16.The following is a list of hardware initialization of the digital PID controller based on STM32G491RE for STM32CubeMX during the generation of C initialization code: Initialization of interrupt system.
The STM32CubeMX interface for pin assignment of the STM32G491RE microcontroller is depicted in Figure 17.
Electronics 2023, 12, 3235 13 of 23 The following is a list of hardware initialization of the digital PID controller based on STM32G491RE for STM32CubeMX during the generation of C initialization code:  As with Arduino, a digital PID controller has been implemented.A simplified skeleton version of the program that implements the PID controller is shown in Listing 3.However, it should be noted that using the STM32CubeMX development environment is more complex than in the case of Arduino, and despite creating precise instructions on how to configure the microcontroller, it sometimes causes problems for students.Unfortunately, the listing of such a program containing the necessary initialization of the microcontroller hardware is several pages long, and it is easy to get lost in it.As with Arduino, a digital PID controller has been implemented.A simplified skeleton version of the program that implements the PID controller is shown in Listing 3.However, it should be noted that using the STM32CubeMX development environment is more complex than in the case of Arduino, and despite creating precise instructions on how to configure the microcontroller, it sometimes causes problems for students.Unfortunately, the listing of such a program containing the necessary initialization of the microcontroller hardware is several pages long, and it is easy to get lost in it.
Figure 18 shows the time waveforms for the case of processing a sinusoidal input signal y(t) with a frequency of 1 kHz and an amplitude of 1 V (the input signal has a +1.5 V offset) to an analog output u(t).
Figure 18 shows the time waveforms for the case of processing a sinusoidal inpu signal y(t) with a frequency of 1 kHz and an amplitude of 1 V (the input signal has a +1.V offset) to an analog output u(t).

Tuning the PID Controller
After successfully implementing a digital PID controller, the first question is how t tune a PID controller.That is, what should be the values of the three basic parameters o the regulator kc, Ti and Td.Probably the first, and certainly the best known, are the Zeigler Nichols rules.Published in 1942 [59], Zeigler and Nichols described two methods of tun ing a PID loop.Over several decades of using regulators, many rules have been created t address the question of how to tune a PID controller.This problem is so ubiquitous tha thousands of publications on this subject can be found in the literature [30][31][32][33][34]54,60].Th description of even the most important ones would exceed the scope of this publicatio many times, which is why only one very simple method will be presented.
One of the simpler methods of determining digital PID controller parameters is th method called Direct Synthesis tuning rules [61], based on first-order model approxima tion.In this method, it is necessary to know the gain of the object in the open loop kp an the time constant τp.The excitation value of the regulator kc is determined from the equa

Tuning the PID Controller
After successfully implementing a digital PID controller, the first question is how to tune a PID controller.That is, what should be the values of the three basic parameters of the regulator kc, Ti and Td.Probably the first, and certainly the best known, are the Zeigler-Nichols rules.Published in 1942 [59], Zeigler and Nichols described two methods of tuning a PID loop.Over several decades of using regulators, many rules have been created to address the question of how to tune a PID controller.This problem is so ubiquitous that thousands of publications on this subject can be found in the literature [30][31][32][33][34]54,60].The description of even the most important ones would exceed the scope of this publication many times, which is why only one very simple method will be presented.
One of the simpler methods of determining digital PID controller parameters is the method called Direct Synthesis tuning rules [61], based on first-order model approximation.In this method, it is necessary to know the gain of the object in the open loop kp and the time constant τp.The excitation value of the regulator kc is determined from the equation

Tuning the PID Controller
After successfully implementing a digital PID controller, the first question is how to tune a PID controller.That is, what should be the values of the three basic parameters of the regulator k c , T i and T d .Probably the first, and certainly the best known, are the Zeigler-Nichols rules.Published in 1942 [59], Zeigler and Nichols described two methods of tuning a PID loop.Over several decades of using regulators, many rules have been created to address the question of how to tune a PID controller.This problem is so ubiquitous that thousands of publications on this subject can be found in the literature [30][31][32][33][34]54,60].The description of even the most important ones would exceed the scope of this publication many times, which is why only one very simple method will be presented.
One of the simpler methods of determining digital PID controller parameters is the method called Direct Synthesis tuning rules [61], based on first-order model approximation.
In this method, it is necessary to know the gain of the object in the open loop k p and the time constant τ p .The excitation value of the regulator k c is determined from the equation where τ ratio defines the speed of response.While the value of the integral time T i parameter is τ p , the value of derivative time T d = 0. Depending on the requirements, the value of τ ratio is selected from the range 1 to 4. For a value of 1, the response of a closed system will be the fastest, and for a value of 4, the response will be the slowest.For the control objects under consideration, the usefulness of this method has been confirmed in practice.

Students Tasks
The primary purpose of the automatic control laboratories is to provide students enrolled in the fundamental automatic control course with a practical understanding of feedback control principles, especially in the real-time process.Through hands-on experimentation, students will learn how to determine the dynamic properties of real systems and adjust the PID controllers accordingly.It is possible to find examples of laboratory tasks on the web pages of many universities, for example [62-64], or in some papers [44][45][46][47][48][49][50][51], [65][66][67][68][69].
Typically, student assignments are divided into five basic stages: identification of the process, calculation of the PID controller parameters, implementation of the PID controller, testing of the closed-loop with PID controller, and analysis of the results.

Identification of the Process
At the beginning of the laboratory exercise, the task of students is to identify the parameters of the tested object, which are determined by the gain of the object in the open-loop k p and the time constant τ p , and time delay τ d .These parameters are determined from a unit-step response of the process.Examples of object step responses are shown in Figures 13 and 19.
My didactic practice shows that in order to deepen the knowledge of students, it is required that they also implement a simple hysteresis regulator, as shown in Figure 23.Examples of waveforms for such a controller are shown in Figure 24.
where τratio defines the speed of response.While the value of the integral time Ti parameter is τp, the value of derivative time Td = 0. Depending on the requirements, the value of τratio is selected from the range 1 to 4. For a value of 1, the response of a closed system will be the fastest, and for a value of 4, the response will be the slowest.For the control objects under consideration, the usefulness of this method has been confirmed in practice.

Students Tasks
The primary purpose of the automatic control laboratories is to provide students enrolled in the fundamental automatic control course with a practical understanding of feedback control principles, especially in the real-time process.Through hands-on experimentation, students will learn how to determine the dynamic properties of real systems and adjust the PID controllers accordingly.It is possible to find examples of laboratory tasks on the web pages of many universities, for example [62-64], or in some papers [44][45][46][47][48][49][50][51], [65][66][67][68][69].
Typically, student assignments are divided into five basic stages: identification of the process, calculation of the PID controller parameters, implementation of the PID controller, testing of the closed-loop with PID controller, and analysis of the results.My didactic practice shows that in order to deepen the knowledge of students, it is required that they also implement a simple hysteresis regulator, as shown in Figure 23.Examples of waveforms for such a controller are shown in Figure 24.

Calculation of the PID Controller Parameters
In the next step, the student's task is to calculate the values of the PID controller parameters kc, ki and kd for the given control parameters and the selected method.They are also encouraged to check the obtained results by performing simulation tests, for example using Matlab or Matlab-Simulink.

The PID Controller Implementation
The PID controller is implemented by students based on algorithms presented in Sections 2-5.Students are also encouraged to create their implementations of the PID controller.For the more talented students, it is recommended they implement the PID controller with autotuning.

Testing of Close Loop with PID Controller
During the tests of the closed-loop system, the quality parameters of the regulation are examined, such as rise time, overshoot, settling time, steady-state error, stability, etc.Typically, these parameters are determined from a unit-step response of a closed-loop system.

Analysis of the Results
Finally, students are required to prepare a written report on the research carried out and an analysis of the research results obtained.
The solutions presented in the paper have been successfully tested in practice during distance learning during the COVID-19 pandemic.These methods were used during the distance learning of the classes in Design of Industrial Control Systems, Digital Signal Processors and Microcontrollers, and Foundations of Digital and Microprocessor Engineering, and they were successfully used by over one hundred graduate or undergraduate students.Currently, these laboratory tasks are conducted under on-site conditions.
The laboratory also serves students specializing in systems identification, providing them with practical experience in evaluating the strengths and weaknesses of various methods for modeling real dynamic systems.
In addition to regular instruction, the automatic control laboratory continuously supports master's and bachelor's thesis research, as well as student projects.

Calculation of the PID Controller Parameters
In the next step, the student's task is to calculate the values of the PID controller parameters k c , k i and k d for the given control parameters the selected method.They are also encouraged to check the obtained results by performing simulation tests, for example using Matlab or Matlab-Simulink.

The PID Controller Implementation
The PID controller is implemented by students based on algorithms presented in Sections 2-5.Students are also encouraged to create their implementations of the PID controller.For the more talented students, it is recommended they implement the PID controller with autotuning.

Testing of Close Loop with PID Controller
During the tests of the closed-loop system, the quality parameters of the regulation are examined, such as rise time, overshoot, settling time, steady-state error, stability, etc.Typically, these parameters are determined from a unit-step response of a closed-loop system.

Analysis of the Results
Finally, students are required to prepare a written report on the research carried out and an analysis of the research results obtained.
The solutions presented in the paper have been successfully tested in practice during distance learning during the COVID-19 pandemic.These methods were used during the distance learning of the classes in Design of Industrial Control Systems, Digital Signal Processors and Microcontrollers, and Foundations of Digital and Microprocessor Engineering, and they were successfully used by over one hundred graduate or undergraduate students.Currently, these laboratory tasks are conducted under on-site conditions.
The laboratory also serves students specializing in systems identification, providing them with practical experience in evaluating the strengths and weaknesses of various methods for modeling real dynamic systems.
In addition to regular instruction, the automatic control laboratory continuously supports master's and bachelor's thesis research, as well as student projects.

Figure 1 .
Figure 1.A feedback loop with standard analog PID controller.

Figure 2 .
Figure 2. The block diagram of the digital PID controller.

Figure 2 .
Figure 2. The block diagram of the digital PID controller.

Figure 4 .
Figure 4.The block diagram of the digital PID controller with modified derivative term connection.

Figure 3 .
Figure 3.The block diagram of the digital PID controller with saturation, and with the PWM modulator on the output.

Figure 3 .
Figure 3.The block diagram of the digital PID controller with saturation, and with the PWM modulator on the output.

Figure 4 .
Figure 4.The block diagram of the digital PID controller with modified derivative term connection.Figure 4. The block diagram of the digital PID controller with modified derivative term connection.

Figure 4 .
Figure 4.The block diagram of the digital PID controller with modified derivative term connection.Figure 4. The block diagram of the digital PID controller with modified derivative term connection.

Figure 7 .
Figure 7. Timing diagram of data flow in the digital PID controller.

3 Figure 10 .
Figure 10.A simplified schematic diagram of the experimental control system.

3 Figure 10 .
Figure 10.A simplified schematic diagram of the experimental control system.

Figure 11 .
Figure 11.The view of the control system.Figure 11.The view of the control system.

Figure 11 .
Figure 11.The view of the control system.

Figure 13 .
Figure 13.Time waveform of step response of second-order controlled system y(t), drawn by Seria Plotter for fsk = 10 Hz.

Figure 13 . 24 Listing 2 .Figure 13 .
Figure 13.Time waveform of step response of second-order controlled system y(t), drawn by Serial Plotter for fsk = 10 Hz.

Figure 14 .
Figure 14.A view of the STM32G491RE-Nucleo module.Figure 14.A view of the STM32G491RE-Nucleo module.

Figure 14 .
Figure 14.A view of the STM32G491RE-Nucleo module.Figure 14.A view of the STM32G491RE-Nucleo module.

Figure 15 .
Figure 15.The block diagram of the digital PID controller based on STM32G491RE-Nucleo.

Figure 16 .
Figure 16.Timing diagram of data flow in the digital PID controller based on STM32G491RE-N cleo.

Figure 15 .
Figure 15.The block diagram of the digital PID controller based on STM32G491RE-Nucleo.

Figure 15 .
Figure 15.The block diagram of the digital PID controller based on STM32G491RE-Nucleo.

Figure 16 .
Figure 16.Timing diagram of data flow in the digital PID controller based on STM32G491RE-Nucleo.

Figure 16 .
Figure 16.Timing diagram of data flow in the digital PID controller based on STM32G491RE-Nucleo.

Figure 17 .
Figure 17.STM32CubeMX interface for pin assignment of the STM32G491RE microcontroller.

Figure 17 .
Figure 17.STM32CubeMX interface for pin assignment of the STM32G491RE microcontroller.

Figure 18 .
Figure 18.Time waveforms of the STM32G491RE-Nucleo module, y(t) orange curve, u(t) blue curve.As in the case of the Arduino module, the program has been modified to determine the step response of the open-loop system.The open-loop step response of the second-order object is depicted in Figure 19.If the student does not have an oscilloscope, the capabilities of the Development Environment STM32CubeIDE can be used for imaging variables y(n).

Figure 19 .
Figure 19.Time waveforms of the open-loop step response of the second-order object, u(t)-green y(t)-blue.

Figure 19 .
Figure 19.Time waveforms of the open-loop step response of the second-order object, u(t)-green, y(t)-blue.Examples of experimental time waveforms of a PI controller (closed-loop) with a second-order object for a step change of setpoint x(t) from 0.2 to 0.7 are shown in Figures20-22.They show the time waveforms of the control variable u(t) and the measured value of the process y(t) for different controller parameters.

Figure 19 .
Figure 19.Time waveforms of the open-loop step response of the second-order object, u(t)-green y(t)-blue.
At the beginning of the laboratory exercise, the task of students is to identify the parameters of the tested object, which are determined by the gain of the object in the openloop kp and the time constant τp, and time delay τd.These parameters are determined from a unit-step response of the process.Examples of object step responses are shown in Figures 13 and 19.

Figure 23 .
Figure 23.The block diagram of the hysteresis controller based on Arduino Uno.Figure 23.The block diagram of the hysteresis controller based on Arduino Uno.

Figure 23 .
Figure 23.The block diagram of the hysteresis controller based on Arduino Uno.Figure 23.The block diagram of the hysteresis controller based on Arduino Uno.

Figure 24 .
Figure 24.Time waveforms of the hysteresis controller, closed-loop step response of the first-order object, u(t)-green, y(t)-purple.

Figure 24 .
Figure 24.Time waveforms of the hysteresis controller, closed-loop step response of the first-order object, u(t)-green, y(t)-purple.

Figure 8 .
Timing diagram of data flow in the digital PID controller.The block diagram of controller based on Arduino Uno.Listing 1.A skeleton version of the program that implements the PID controller.Timing diagram of data flow in the digital PID controller.
Figure 8.The block diagram of controller based on Arduino Uno.Listing 1.A skeleton version of the program that implements the PID controller.Figure 8.The block diagram of controller based on Arduino Uno.Listing 1.A skeleton version of the program that implements the PID controller.