@tutorial: Temperature PID controller - Arduino

What’s up my friends, welcome back. What I want, is the aluminum block below to have let’s say, exactly 100 degrees. I’ll control the real temperature using a K type thermocouple. To read the data I'll use the MAX66 75 breakout module and control the PID algorithm with and Arduino. Finally, to apply power we will make a small circuit using a MOSFET or maybe a TRIAC in case of high AC voltages. This will be a close loop. The thermocouple measures

External Link: https://electronoobs.com/eng_arduino_tut24.php

by: ELECTRONOOBS on 2026-05-18

~PART 1 How PID works

So, the first thing is to understand how a PID temperature controller will work. As in any PID system we need to define a final process, in our case will be the final temperature that we will achieve. In order to control this temperature, we will need a feedback. So, any PID control will have some sort of feedback. In our case, that feedback is made using a K type thermocouple that will measure the real temperature of the system.


This type of control will also need a setpoint, which in our case is the desired temperature. The system will make the difference between the desired value and the feedback from the output (error), and using 3 constants, the proportional, the integral and derivative, we can change the output according to the feedback.


So, if we want the heating block to have exactly 100 degrees what we will do first is apply power to it. This will start heating up. By the time it reaches the setpoint value, which in this case is 100 degrees, the feedback will inform that to the PID control and this will start lowering the power applied to the heating element, and in our example that will be made using a PWM signal applied to a MOSFET that will control the voltage that goes to the heating element inside the block.


So, it is obvious, if the real temperature is higher than the setpoint, we lower the power value, if the real temperature is lower than the setpoint, well, we increase the power till it reaches the desired value.

If we do just that, that is called P control, or proportional control, and will end up in a temperature oscillation between certain values and it will very difficult or never be stable. For that we add the D control, or derivative. This kind of control will react to the speed of temperature change. So, if, for example, if we blow air on the aluminum block, the derivative will fast push the power to maximum in order to keep the temperature. Finally, we have the I, or integral. This will sum the error on each loop getting bigger and bigger with each loop, or, in case of negative error, getting lower and lower. The sum of all these parts, the P, the I and D, makes a PID control. It’s our job to find the correct constants for each of this PID elements.



PART 2 K-Thermocouple temperature read


So, let’s start building this project. The first thing we will do, is see how to read the real temperature. Below we can see a K type thermocouple, and the MAX 66 75 breakout module. This will amplify and compensate the voltage created by the thermocouple. It has an SPI communication so we'll have to connect these pins to the Arduino SPI port.


Use the connections below and let’s test it out. On a breadboard I connect the MAX 66 75 and the thermocouple. Be careful, the thermocouple has polarity so connect positive to positive and negative to negative. Connect the SPI pins to the Arduino and also supply 5V and GND.

Thermocouple Arduino Read Schematic
Thermocouple Arduino Read Schematic

Thermocouple read code


Now upload the next code to the Arduino UNO/NANO. This code will just read the SPI data from the module using a function called "readThermocouple" and that gives us the real temperature. We print the value on the LCD screen. Have in mind that if you don't have the LiquidCrystal_I2C library, you'll have to dwonload it and install it to the Arduino IDE. I heat the thermocouple with a lighter and there you go, I have the real value on the LCD screen.


PART 3 PID control


So now that we know how to read the real temperature, let’s mount this next schematic and control the power applied to the heating element with a MOSFET. I mount the circuit on a breadboard once again and upload the next code. This second code has the PID algorithm already created. We read the temperature, calculate the error, sum the PID values and create a PWM signal on digital pin D3 that will be applied to the MOSFET. I set the desired temperature at 100 degrees and use the LCD to print the set value and the real temperture.


Temperature PID control Arduino schematic
Temperature PID control Arduino schematic

Code for fixed PID control



Ok so the code below is a bit large. But don't worry. It is very easy. We set a variable setpoint at 100 degrees for this example. Then we read the thermocouple real temperature value as in the past example. Then we use 3 constants and calculate the PID sum. Depending on that value we create a PWM signal on pin D3 and apply it to the MOSFET gate using a BJT driver.


As you can see the temperature stays at that value. But that is after trying a lot of PID constants and that is the tricky part of this project. So, what you will have to do is try your own values till you get the correct ones. I advise you to start with the I and D values equal 0 and then increase those values slowly till you get good results.

Here on my oscilloscope I have the PWM signal of the MOSFET connected.


At the beginning, till the system reaches the desired value the pulse has a small width since I use a BJT to activathe the N channel gate, so the mosfet is activated with a LOW value in this case. Once the set value is reached it starts to wambble around and by that maintaining the temperature. As you can see, if I try to cool down the heating element by blowing air with this tube, the PWM signal width get’s lower in order to keep the same value. So, the control works.


Part 4 PID + rotary control


Now, all this system needs is some sort of control togheter with the LCD screen in order to view and also be able to set the desired temperature value as this commercial PID controller has the set and up and down buttons. For that I'll use the rotary encoder. It has a push button integrated so I can use it to enter the setpoint menu and increase or decrease the value.


PID DC heater PID control schematic
PID DC heater PID control schematic

This above is the final schematic of this project. We have an LCD screen to print the values, the rotary encoder with push button insede for control, the thermocouple with the MAX 66 75 module, the MOSFET and the BJT as a driver circuit that will control the power and the heating element. Make sure that the thermocouple is touching the heating element in order to know the real value. I mount everything on the breadboard and now let’s test the new code, which by the way, you can also download from a below.

The default value is now 0 degrees. Press the set button of the rotary encoderRotate it to the left in order to increase or decrease the temperature value. Press the set button once again and now you can set the P constant for the PID control. Press once again and select the I value. Finally, press the button again and select the D value. Now press the button and exit the menu and the new settings are stored. I set it to 100 degrees and now the real read starts increasing till it reaches the desired value and it stays there. When we reach the desired value you can see the PWM wombelling in order to maintain that value.

There you go our PID of temperature works. I could 3D print a case for this project just as the commercial one has, but, since I’ll use this project for my soldering station in a future tutorial, I won’t do that now. In a future project, I’ll use this 12V soldering iron with a thermocouple inside and make my own soldering station so stay tuned for that guys.


Have in mind that today’s project is for only DC power control, so only DC heating elements will work. I’ll also make a tutorial on a temperature PID control, but for AC 220V voltage using a TRIAC control as in the past TRIAC tutorial.



If my videos/tutorials help you and you would like to help my projects, I have a Patreon campaign. I would really appreciate that guys and it will help my workshop for other new projects and keep this channel/webpage going. BTW, thanks to all my Patreons.

I hope that you’ve enjoyed this small tutorial on PID control. If you have any question about this video or any other, just leave it in the comment section below or on my FORUM page. Also, don’t forget to subscribe and watch all of my other great tutorials. Remember, if you consider helping my projects check my Patreon page as well.

Leave a comment

Please login in order to comment.

Comments

ADVERTISERS
ADVERTISERS
PCBWAY