The Proportional Integral Derivative (PID) controller is a classic control system used for a variety of embedded and industrial control processes. This article presents a real-world thermal PID application using the Arduino UNO R4 Minima as a controller and a MikroElektronika Heater Click as the controlled system.
The MIKROE 3996 Heater Click board hereafter referred to as the “Heater” is a small breakout board designed for the mikroBUS system. It features a heating element composed of a long PCB trace as shown in Figures 1 and 2. Figure 1 shows a thermal image while Figure 2 shows the temperature sensor and the PCB heater traces.
The heater is controlled via a MOSFET which provides a convenient way to control the system via a PWM signal. The temperature is monitored by a Texas Instruments TMP235 thermal sensor which drives a 12-bit Microchip MCP3221 ADC. This provides a I2C interface via the mikroBUS socket.
This article provides a brief introduction to the Heater from an embedded control engineers’ perspective with emphasis on the step response and the Arduino code interface. The article concludes by documenting the performance of a controller designed to toggle the thermal system between two setpoints.
Highlight of the PID controller design are included. However, this is not a PID tutorial. Please refer to this introductory article for additional information about the PID controller. The PID controller itself is designed from an educational perspective. The individual P, I, and D elements are implemented as percentages. This approach allows students to better understand the contributions of each parameter to the whole system.
The code for this project is included here: Heater.zip (2.9 KB)
Figure 1: Thermal image of an operational MIKROE Heater Click board. The center of the board is about 175 °F (80 °C).
Tech Tip: from Figure 1, we see a hot spot in the center of the PCB. This has important implications for power dissipation and circuit layout. We can visualize the heater as many individual resistors all physically placed in parallel. Each resistor contributes to the heat of its neighbors. Consequently, the resistors in the center are the hottest with progressive cooling at the edges. When designing a PCB be sure to provide additional spacing for the center resistors to avoid excessive heating.
On a related note, this property extends to cables. Bundles of wires have less ability to dissipate heat when compared to individual isolated wires. This has profound fire safety implications when wires are passed through conduit. Be sure to follow all applicable code and derate your system appropriately.
Figure 2: Close up image showing the PCB trace and TMP235 thermal sensor.
Physical connection of the mikroBUS to Arduino
The Heater is connected to the Arduino Minima using a MIKROE-5739 Click Schield as shown in Figure 3.
Note that the default MIKROE-3996 configuration is used. Zero-Ohm jumpers connect the logic to 3.3 VDC while the heating element is powered by 5 VDC sourced from the Arduino via the mikroBUS connection.
Figure 3: Image of the Heater installed on an Arduino Minima via a MIKROE-5739 shield.
Programmatic interface to the MikroElektronika Heater Click board
The core programmatic interface to the Heater is described in seven lines of code. The first three lines retrieve data from the Microchip MCP3221. The corresponding data transfer is shown in Figure 4 as captured using a Digilent Analog Discovery.
The scaling of the 12-bit ADC is shown on the next two lines. Personally, I like to use a two-step process where the value is first converted into a voltage and the converted into the desired units. This makes it easy to troubleshoot as the value may be sent to the serial port and then compared to the actual voltage input to the ADC. Note:
-
The MCP3221 uses the 3.3 VDC for power and reference voltage.
-
The Heaters TMP235 is a 10 mV / °C device where 0 °C is located at 0.5 VDC
-
A smoothing running average filter of the last 16 samples is used to reduce noise.
The Heater has a MOSFET to turn the heating element on and off. A PWM signal is used to drive the MOSFET via an analogWrite( ) function.
Wire.requestFrom(0b1001101, 2);
uint8_t upper_byte = Wire.read();
uint8_t lower_byte = Wire.read();
uint16_t ADC_12_bit_binary = (upper_byte << 8) | lower_byte;
float ADC_voltage = f_map((float)ADC_12_bit_binary, 0, 4095, 0, 3.3);
float averagADCVoltage = runningAverageOf16(ADC_voltage);
float temp_in_F = f_map(averagADCVoltage, 0.5, 1.5, 32.0, 212.0);
// Placeholder for PID code
analogWrite(PWM_PIN, duty_cycle_8_bit);
Figure 4: Logic analyzer screen capture of the I2C Arduino to MCP3221 signal.
System characterization as a first step
Determination of step response is an important first step for a control system. Armed with this information we can make general observations about the nature of the system. We can also determine an appropriate sample time.
This is a simple process for the Heater, as all we need to do is turn it on and record the results. The system has a 1st order response very similar to charging a capacitor via a series resistor. Figure 5 shows the observed and calculated response assuming a time constant of 75 seconds.
Calculated = T_{final} – (T_{final} – T_{initial})\exp^{-t/\tau} = 163 - (163 - 86.33) \exp^{-t/75}
The close relationship between the observed and calculated values indicates good thermal isolation of the heating element. In fact, the orderly system suggests that there is negligible heating of other structures (no 2nd and higher modes to consider). This supports the excellent thermal isolation suggested in Figure 1.
Figure 5: Graph showing the Heater’s first order step response indicating a system with a time constant of 75 seconds.
PID Controller
The core PID routine was previously described in this article. Figure 6 captures the core design features of the controller:
-
Parallel PID construction.
-
Each P, I, and D calculation includes a limiter block with limits set at +/- 100.0. This allows the system’s operation to be analyzed on a percent basis making the PID controller easier to understand.
-
An integral windup function (lockout) is included to freeze the accumulator if the P term is saturated.
-
The derivative is taken directly from the feedback to prevent an impulse upon setpoint change.
-
A running average low-pass filter is included to reduce D noise.
Figure 6: A practical modified PID controller includes saturation blocks for the individual P, I, and D contribution.
How is sample time determined for a PID controller?
The clock icon in Figure 6 implies that this is a sampled system. As a first approximation we can assume a PID sample time of 10 or 20 time faster than the system’s time constant which was calculated as 75 seconds in Figure 5.
Ideally, we could enter the PID routine once every 7.5 seconds (10 x sampling) or once every 3.75 seconds (20 x sampling). In practice, the system is noisy. It may not look it from Figure 5, however things are not so clear when we try to hold temperature to within a degree.
One solution is to low pass the feedback signal. This can be challenging as the filter add delay. Instead of the PID acting on the “now,” it acts on the average events that took place in the past. Knowing that the time constant is 75 seconds allows a certain amount of filtering. For this example, the sample clock was set to 250 ms and a 16-stage low pass filter was implemented. This 4-second delay does not appear to cause problems.
PID controller performance
As a demonstration, the system was set to toggle between 120 °F and 130 °F every 1.5 minutes. The results are shown in Figure 7. The system displays mild hunting at startup but steadies out by 400 seconds. Some overshoot is seen before settling into the steady state temperature.
Figure 7: System response when toggled between two temperatures.
Tech Tip: Toggling a thermal system between two setpoint is a challenging operation as the system is asymmetrical. We can actively add heat, but the cooling is passive. To improve the performance, the system is operated at elevated temperatures to facilitate cooling. Different tuning numbers would be required if the system were operated at lower temperatures.
Figure 8 show the corresponding internal workings of the controller. Here the individual P, I, and D components are shown as percentages:
-
The P term saturates when a large temperature change is commanded.
-
The D term opposes any change in temperature. This is most noticeable when the temperature is changing quickly between the two states.
-
The I term shows the clos accumulation of error. Note that the integral is frozen in position when the P term is saturated. This action is a reasonable solution to the integral windup that would otherwise occur when the system is jumping between temperature setpoints.
Figure 8: Percent contributions of the individual P, I, and D components.
Tech Tip: Expressing the PID components in terms of percentage provides a convenient way to visualize the operation of the PID controller.
Figure 9 presents the final plant drive which is a summation of the P, I, and D terms. For this system values less than 0 are interpreted as zero drive. Unlike a motor which may be reversed, there is no way to “reverse” the heat transfer in this simple system.
Figure 9: Percent plant drive. Note that negative values are interpreted as zero drive.
Next Steps
This project is far from complete. Many opportunities for experimentation remain including:
-
For students and those new to control systems, step back and construct a “bang-bang” controller where on-off control is used for the heating element. It’s worth the effort to explore the rudimentary controller. After all, we should strive to design using the simplest controller possible – but no simpler that the demanded by the task at hand.
-
Operate the controller with only P, and then with only PI controls turned on to better understand the nature of a thermal control system.
-
Identify and resolve the difference between this PID controller and one presented in your textbook.
-
Optimize the sample time.
-
Characterize the noise and determine the performance limitations of the filter.
-
Modify the click to include a precision voltage reference for the ADC.
-
Optimize the PID algorithm.
-
Determine how to automatically tune the PID for this non-linear system.
-
Encase the heater in an insulated chamber (oven).
-
Include gain scheduling for improved performance. For example, mid-to-long-term steady state stability could use different gain and filter setting than when the system is starting up.
-
Incorporate the heater into a larger system.
-
Add a cooling fan to remove heat from the system.
-
Design your own PCB using the TMP235 / MCP3221 or switch to other components.
Parting thoughts
Real world control systems present the best learning opportunities. The MIKROE-3996 Heater Click is a prime example. As demonstrated, the board is easy to connect to a microcontroller yet offers many lessons in control systems.
You are encouraged to replicate and then adapt this experiment to you needs. Please share your observations and recommendations in the comments section.
Best wishes,
APDahlen
Related Information
Please follow these links to related and useful information:
About this author
Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.