Sunday, February 5, 2023

PID Temperature Controller using ESP32 and MAX6675

ESP32 PID Temperature Controller using MAX6675 K-Thermocouple to Digital Converter IC



PID based Temperature Controller

This is a PID Controller based Heater that can be used to control the temperature of the Ceramic Heater.

What is a Temperature PID Controller ? 

As the name suggests a temperature PID controller deals with temperature, PID temperature control is a closed-loop control algorithm that improves the accuracy of the process. The PID temperature control works using a mathematical formula to calculate the difference between the current temperature and set point. And then it tries to deliver the required power to ensure the target temperature remains constant, this not only reduces environmental impact but it also reduces overshoots that can be found in the traditional on-off control mechanism.

How does a Temperature PID Controller work?

As in any PID control first, we need to be aware of the output or what we want the controller to do, for this project we want to maintain a certain temperature of the heating element (we will set that temperature with the help of the rotary encoder) so to maintain the temperature we need to read out of the temperature, for that we are using a K-type thermocouple, in conjunction with MAX6675 Cold-Junction-Compensated K-Thermocouple to Digital Converter IC that can measure hundreds of degree Celsius without any issues. And the temperature readout from the thermocouple acts as feedback. Now as we have set the temperature we want to achieve and we have a real-time readout of the temperature value the controller can calculate the error value and with the help of proportional integral and derivative control the system can achieve its target, for this project we will control a PWM signal with the calculated output value. That is how a Temperature Based PID controller works.

MAX6675 K-Thermocouple IC Working 

Components Required to build a PID Enabled Temperature Controller

The components required to build the MAX6675 based PID Controlled Heater are listed below

  • ESP32 - 1
  • 128 X 64 OLED Display - 1
  • Generic Rotary Encoder - 1
  • MAX6675 Module - 1
  • K-type Thermocouple - 1

PID Enabled Temperature Controller Circuit Diagram


Download EAGLE CAD file from GIT Hub

In this project, we use the MAX6675 K-type Thermocouple sensor to read the temperature data from the thermocouple, and in this section, we will explain all the details with the help of the schematic. Let me give you a brief overview of what is happening with this circuit. The MAX6675 is a Cold-Junction-Compensated K- Thermocouple to Digital Converter module and it connects to Arduino according to the schematic. The power is provided to the circuit with the help of a +5V supply of the ESP32 . Also to set the temperature and change the modes we are using a generic Rotary encoder. Next, we have the 128X64 OLED display the display shows the temperature data and it also shows the set temperature. By pressing the button on the rotary encoder, we can switch in between two modes one is to set the temperature and another one to monitor the trout from the thermocouple. Other than that, the circuit stays pretty simple.


MAX6675 based PID Enabled Temperature Controller ESP32 Code

The complete code used in this project can be found at the bottom of this page. After adding the required header files and source files, you should be able to directly compile the Arduino code without any errors. You can download the PID controller library, MAX6675 Library, AAdafruit_SSD1306 Library from the link given below, or else you can use the board manager method to install the library.

A simple explanation of the code is given as comments, and in this section, we will go a little more in-depth about it. First, we start by including all the required libraries, after that, we define all the necessary pins that are required to read the encoder, drive the OLED and MAX6675 Thermocouple temperature sensor. Once that is done, we define all the values for the Kp, Ki, and Kd, and include all the required variables.

Next, we have defined the __Kp, __Ki, and __Kd values for our code. These three constants are responsible for setting up the output response for our code. Please do note that for this project, I have used the trial-and-error method to set the constants, but you can calculate the values if that is something necessary for your project.

/*In this section we have defined the gain values for the
 * proportional, integral, and derivative controller I have set
 * the gain values with the help of trial and error methods.
*/
#define __Kp 30 // Proportional constant
#define __Ki 0.7 // Integral Constant
#define __Kd 200 // Derivative Constant

Next, we declare all the required variables and we create three instances one for PID one for the OLED, and the final one for the thermocouple. Variable clockPin and clockPinStatedebounce, and encoder_btn_count all four used to read data from the encoder the temperature_value_c holds the temperature readout from the thermocouple, finally the encoder_btn_count holds how many times the button of the encode is pressed.

PID Enabled Temperature Controller Testing 

To test the circuit the following setup is used, as you can see, I have connected  Multimeter to display the duty cycle of the output PWM signal. And as a heater, I have used a ceramic heating element, controlled by SSR



Now, to set the temperature you need to press the button of the rotary encoder, which set the setpoint or the target temperature of the PID algorithm once set press the button another time to make the changes permanent and the heater block starts heating and you can see the duty cycle also increases.


Once the desired temperature is reached the PWM duty cycle is reduced and you can observe a certain spike in the duty cycle when the controller wants to compensate for the error and increase the temperature.



This marks the end of the tutorial. I hope you liked the article and learned something new. If you have any questions regarding the article, you can leave them in the comment section below or you can use our Electronics Forum. You can also check out the video at the bottom.

ESP32 Code and Eagle CAD file can download here,
 
there are two files, one for OTA update, use any one at your preference, 
https://github.com/almorawaka/ESP32-MAX6675-based-PID-Controler
 

==========================Update on 20-02-2023======================

I have successfully designed and fabricated a printed circuit board (PCB), which involved the assembly of various electronic components through soldering. Moving forward, I plan to integrate a calibration mechanism for the sensor and implement a closed-loop system to reduce any errors in temperature regulation when controlling the heater.

To achieve this, I will need to establish a set of procedures to determine the sensitivity and accuracy of the sensor, and calibrate it accordingly. This process may involve adjusting gain, offset, and linearity of the sensor output to match the expected response. Once the sensor is properly calibrated, I will proceed to implement a feedback loop system that utilizes the sensor data to regulate the temperature of the heater.



My GitHub repository boasts a treasure trove of electronic design assets, including the Eagle CAD file that is available for your perusal.If you have a keen eye for detail and an unquenchable thirst for electronic design, then you will undoubtedly appreciate the intricate layout and intricate component placement within this file. It is a testament to the power of technology and human ingenuity, and it stands as a shining example of what can be achieved through tireless effort and unwavering dedication.

So, why wait? Visit my GitHub repository today and explore the myriad of design assets that are available for you to download and use in your own projects. With the Eagle CAD file at your fingertips, you too can experience the thrill of creating your own electronic masterpiece and pushing the boundaries of what is possible in the world of electronic design.

 
 
 
=====================What Next?============ ==== ============= 
  1. calibrate sensor 
  2.  increase clearance of PCB 
  3. add Heater Power Monitoring current Transformer
    Once i complete this I'll update here and GitHub for you. untill then Bye..
 

No comments:

Search This Blog