1. Introduction
This article demonstrates how to control a 4-wire server fan based on temperature or user input, using the Model-Based Design Toolbox for S32K1 MCUs. The Model-Based Design Toolbox represents a solution which allows the complex applications deployment on NXP hardware directly from Simulink. By incorporating hardware optimized software, such as drivers, libraries, and tools, the Model-Based Design Toolbox allows the users to focus only on the algorithm development, while making such applications hardware-aware is handled by the toolbox. By the integration with the MathWorks ecosystem, the MBDT leverages the model-based design paradigm, thus enabling a programming process based on models - users to not need to write C code for implementing their designs, but create logical diagrams using Simulink blocks performing dedicated functions.
In this article, we will demonstrate how this tool can be used for designing and deploying on an S32K146 Evaluation board a fan speed control application. Thus, the steps proposed for achieving this are described thorughout the article, in the following sections:
II. Application overview – how the application works and the links between the components;
III. Hardware design – the description of the components used and the electrical diagram;
IV. Software design – the application execution flow and the model implementation explained in more detail;
V. Conclusion – the results of the application.
2. Application overview
The application has 2 operating modes: automatic and manual. To switch between operating modes, we need to press the SW2 button on the S32K146 board. When starting, the application is in automatic operating mode (the RGB LED of the board lights up green): this means the fan adjusts its speed according to temperature. In manual mode (the RGB LED of the board lights up blue), the fan changes its speed depending on the value supplied by the potentiometer.
The graphs and values of fan speed, acceleration, ambient temperature will be displayed in FreeMASTER.
Block diagram
Figure 1: Block diagram
3. Hardware design
A. Hardware components
The required hardware components are:
S32K146 Evaluation board
Thermistor NTC100K
4-wire PC fan
12V power supply AC-DC.
1) S32K146 Evaluation board
The S32K146EVB serves as an affordable evaluation and development board designed for a wide range of industrial and automotive uses. The board is the “brain” of the application, as it collects data from various sensors (e.g.: temperature sensor) and uses the algorithm to regulate the actuators (e.g.: the BLDC motors found in fans).
For more information about the board, please read from here.
2) Thermistor NTC100K
A thermistor is a resistor whose resistance is dependent on temperature. The temperature value is calculated using the Steinhart-Hart equation.
The Steinhart-Hart coefficients A, B, C vary depending on the type and model of thermistor and the temperature range of interest. To find these coefficients, we use three values of resistance data for three known temperatures.
For example, from NTC100K Thermistor Datasheet we get for the temperatures of 15℃, 25℃ and 45℃, corresponding resistance values of 156407 Ω, 100000 Ω and 43659 Ω. It results, according to the equation (2), that the coefficients have the following values:
So, the steps to determine the temperature are to get the thermistor resistance using a voltage divider converter along with an analog-to-digital converter (ADC) and calculate the temperature from the resistance.
3) 4-wire PC fan
The fan has 4 pins, being used especially for processors with high power consumption. The simplest fan has 2 pins, one for power and one for ground. The 3-wire fan has an extra pin called “tachometer”, which indicates the speed of the fan (one/two impulses are received for each rotation). With 2-wire and 3-wire fans, the speed is controlled by increasing/decreasing the voltage on the power pin. Instead, the 4-wire fan has a control pin and uses PWM (Pulse Width Modulation) to control the speed.
Before using the fan, we must find out information such as the maximum speed, the number of impulses per rotation given by tachometer and the minimum operating speed if necessary. These things can be looked up in the fan’s datasheet or can be determined experimentally if we have an unknown fan, and we cannot find its datasheet.
The values of the fan used as an example in this article were determined experimentally and we got an approximate maximum speed of 300 rotations per second (18000 rpm), two impulses per rotation and a minimum operating speed of 80 rotations per second (4800 rpm).
4) 12V power supply AC-DC
The power supply converts alternating current (AC) electrical energy into direct current (DC) electrical energy with an output voltage of 12 volts. In our application, it will supply the power for the fan.
B. Electrical schematic
There are multiple hardware configurations for the fan speed control application. We will use, in addition to the hardware components mentioned above (fan, 12V power supply, thermistor), the potentiometer, the SW2 button and the RGB LED of the S32K146EVB.
We propose the following electrical schematic:
Figure 2: Electrical schematic
Note! It is important that all components are connected to the same ground.
C. Hardware setup
A possible hardware setup may look like in the following picture:
Figure 3: Hardware setup example
4. Software design
A. Prerequisite software
To be able to follow the next steps in this article, the following software is necessary:
MATLAB ® and Simulink ® (2021a or newer), including Stateflow ® , MATLAB ® Coder TM , Simulink ® Coder TM , Embedded Coder ®
Model-Based Design Toolbox for S32K1xx 4.3.0
B. Flow chart
The application execution flow is based on a state machine, according to the following diagram.
Figure 4: State machine
The architecture of the application is a closed-loop system, which uses a feedback signal to adjust the fan speed. The current fan speed and the fan acceleration update their values with each tachometer pulse. The difference between the reference speed (set using temperature or potentiometer) and the current speed, alias error signal, is the input to the closed-loop control system, such as a proportional-integral-derivative (PID) controller. The PID controller applies a correction based on proportional, integral, and derivative coefficients to a control function, in our case, it determines the PWM duty cycle to control the fan speed.
Figure 5: Application's model
C. Model Overview
From top to bottom (Figure 5), we have 4 big sections:
Initialization
Switching between operating modes
The operating modes which set the reference speed
The control algorithm.
Let’s take each section to dive further into the details.
1) Initialization
Figure 6: Initialization section
We have on the first line, from left to right, the Configuration block for S32K1xx processor family, the FreeMASTER Configuration Block and two ADC Configuration Blocks. On the second line, we have the pull-up resistance on the tachometer pin and two mapping functions from the temperature value and the ADC potentiometer value to the reference speed. Also, there are variables for current operating mode, temperature value, reference speed, current speed, duty cycle and some other auxiliary variables.
The required configurations of the processor configuration block are the default ones, except for the processor model and the download interface (Figure 7).
For the FreeMASTER configuration block, we set the communication interface (e.g.: LPUART1), the baud rate (e.g.: 19200) and long interrupt serial communication. This component provides an interface where relevant data such as current speed, ambient temperature and many others are displayed.
In the ADC configurations blocks, used for reading temperature and potentiometer value, we select the ADC converter number and the resolution mode (e.g.: 10-bit conversion).
Figure 7: Configuration block for S32K1xx family of processors
2) Switching between operating modes
We configure the pin PTC12 (SW2) to generate a falling edge interrupt on each button press, using the GPI ISR block.
Figure 8: Switching mode section
When an interrupt is generated, the value of the operation mode variable is toggled and the color of the LED corresponding to the operating mode lights up (automatic mode – green, manual mode – blue). It also enables/disables the PIT (Periodic Interrupt Timer) interrupt which triggers the ADC reading of the temperature sensor at each one second (Figure 10).
Figure 9: The function called when the interrupt is generated.
Figure 10: Enable/disable PIT interrupt for ADC temperature reading; RGB LED in Automatic & Manual mode
3) Operation modes
Figure 11: Operation modes section
Depending on the operating mode, the reference speed is set by the temperature value or the ADC value of the potentiometer.
a) Automatic mode
In this mode, the PIT block is enabled, and the ADC value of the thermistor is read once per second. The resolution of the ADC is 10 bits. So, it can take values between 0 and 1023, which corresponds to voltage values between 0V and 5V. Let’s assume Vout is the output voltage on the pin PTB13.
Also, according to the electrical schematic from Figure 2, we have a voltage divider:
We can find out the value of thermistor resistance:
And then, we can get the temperature value using the Steinhart-Hart equation (1).
Based on the temperature, we can map a value for reference speed. We can define for this mapping a linear function or whatever we need.
Figure 12: Automatic mode
b) Manual mode
In manual mode, the ADC value of the potentiometer is read and mapped to reference speed.
Figure 13: Manual mode
4) The control algorithm
Figure 14: The control algorithm section
In this section, the closed loop of the system is implemented using the PID (Proportional-Integral-Derivative) controller. We must tune the coefficients of the PID controller (Kp, Ki, Kd) to produce the optimal control function. There are several methods for tuning: manual, Ziegler-Nichols or using software specialized tools. For this application, the Ziegler-Nichols method was used, and, for fine adjustment, manual tunning was applied. After applying this method, we get the following values: KP = 0.003, KI = 0.003, KD = 0.001.
Figure 15: PID controller configuration
With the PWM signal from the PID controller, we control the fan speed via the PWM pin, using the FTM PWM Config block. We configure the block as follows:
Figure 16: Parameters of FTM PWM Config block
According to the electrical schematic (Figure 2), the pin used to control the fan speed is PTC1, which corresponds to FTM0_CH1. This means we need to configure the channel 1 from the module 0 of FTM (FlexTimer Module).
To get the fan current speed, we need to know the period between two pulses from the tachometer. This can be determined using the FTM Input Capture block, which returns the timestamp in microseconds. Remember that there are two pulses per rotation.
We measure the speed once every 10 milliseconds. So, the acceleration has the following value:
Figure 17: Calculation of fan speed and acceleration
D. FreeMASTER
To visualize the results of the application, you can create a FreeMASTER project. Add the .elf file of the built application to Project – Resource files – “pack” directory setup – MAP Files – Default symbol file. You can add variables to the Variable Watch section, such as operation mode, current speed, reference speed, temperature (Figure 19). Also, you can create a graph, as in Figure 18, using the FreeMASTER oscilloscope with the current speed and reference speed.
In the following figure, we can observe that the system is initially in automatic mode: temperature is about 28 Celsius degrees, which corresponds to an approximate reference speed of 110 rotations per seconds. Then, when SW2 button is pressed (at second 685 on the graph), the system goes into manual mode and the reference speed is set about 236 rotations per second, which is mapped according to the value read from the potentiometer. The current speed (red line) is tracking the reference speed imposed (green line).
Figure 18: Graphic with reference speed and current speed
Also, we can track values such as operation mode - boolean value (0 for automatic mode and 1 for manual mode), temperature in Celsius degrees, reference speed and current speed measured in rotations per second, acceleration or the PWM duty cycle used for controlling the fan speed.
Figure 19: Variable watch section
5. Conclusion
In conclusion, the application consists in controlling the fan speed depending on the temperature or the potentiometer value, using the Model-Based Design Toolbox for S32K1 MCUs. It combines notions from systems theory, electronics, and embedded systems, representing an academic study on the use of fans for temperature control in different applications like servers, routers, switches, etc.
Useful links:
1. S32K146EVB:
https://www.nxp.com/document/guide/getting-started-with-the-s32k146-evaluation-board-for-general-purpose:NGS-S32K146EVB
2. NTC100K Thermistor:
https://www.tme.eu/Document/f9d2f5e38227fc1c7d979e546ff51768/NTCM-100K-B3950.pdf
https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation#Steinhart%E2%80%93Hart_coefficients
3. 4-wire fan:
https://www.electroschematics.com/4-wire-pc-fan/
https://www.nidec.com/en/product/search/category/B101/M111/S100/NCJ-V40S-E5-57/
4. PID coefficients:
https://control.com/textbook/closed-loop-control/p-i-and-d-responses-graphed/
https://en.wikipedia.org/wiki/Proportional%E2%80%93integral%E2%80%93derivative_controller
NXP is a trademark of NXP B.V. All other product or service names are the property of their respective owners. © 2024 NXP B.V. MATLAB, Simulink, and Embedded Coder are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks.
View full article