Lesson 47: Using K-Type MAX6675 as thermostat with relay | Arduino Step By Step Course
This project guides you through building a temperature control system using an Arduino, a MAX6675 K-type thermocouple, and a relay. This system can maintain a desired temperature range, making it suitable for various applications. You can use this to build:
- A temperature-controlled incubator for delicate experiments or seedlings.
- An automated brewing system for maintaining consistent wort temperatures.
- A climate-controlled enclosure for reptiles or other temperature-sensitive animals.
- A simple heating or cooling system for a small space.
The system uses the MAX6675 to read temperatures from a K-type thermocouple, providing accurate temperature readings over a wide range (depending on the thermocouple type, up to 1000°C or more). The Arduino processes these readings and controls a relay to switch a heating or cooling element on or off, maintaining the temperature within a user-defined range. (in video at 00:32)
Hardware/Components
To build this project, you will need the following components:
- Arduino board (Uno, Nano, etc.)
- MAX6675 thermocouple amplifier
- K-type thermocouple
- Relay module
- AC bulb (or other load, such as a fan or heater)
- Jumper wires
- Breadboard (optional, but recommended)
Wiring Guide
The wiring is explained in detail in the video. (in video at 03:28) Refer to the video for a visual guide. The key connections include connecting the MAX6675 to the Arduino (pins 2-6), the relay module to the Arduino (pin 8 and power), and the relay module to the AC load. A critical aspect is correctly identifying the live and neutral wires of your AC load before connecting them to the relay. Incorrect wiring can lead to dangerous situations.

Code Explanation
The Arduino code (shown in snippets below) utilizes the MAX6675 library to read temperature values from the thermocouple. The user-configurable parts of the code are:
relayPin: The Arduino pin connected to the relay module (default: 8). (in video at 07:35)relayONandrelayOFF: These constants define the logic levels to turn the relay on and off, respectively. These are usually LOW and HIGH, but might need to be adjusted depending on your relay module. (in video at 07:35)TEMPERATURE_UNIT: Sets the unit for temperature readings (1 for Celsius, 2 for Fahrenheit, 3 for Kelvin). (in video at 08:21)START_TEMPERATUREandSTOP_TEMPERATURE: These variables define the temperature range for the control system. The system will turn on the relay when the temperature falls belowSTART_TEMPERATUREand turn it off when the temperature reachesSTOP_TEMPERATURE(or vice-versa, depending on whether you're controlling a heater or cooler). (in video at 08:31)CONTROL_TYPE: A variable that determines whether the system acts as a heater (1) or cooler (2). (in video at 08:36)
The code includes functions like readTemperature(), printTemperature(), loadControl(), and relayControl() to handle temperature reading, display, and relay operation. These functions are clearly defined and explained in the video. (in video at 09:37, 10:00, 10:43, 11:30)
const int relayPin =8;
const int relayON = LOW;// do not change
const int relayOFF = HIGH; //do not change
int relayState = relayOFF;//initial state of relay
const int TEMPERATURE_UNIT =1;//1=Celsius, 2=Fahrenheit, 3=Kelvin
const float START_TEMPERATURE = 80.0;//unit above
const float STOP_TEMPERATURE = 100.0;//unit above
const int CONTROL_TYPE = 1;// 1= heater, 2=cooler
Live Project/Demonstration
The video demonstrates the functioning of the temperature control system for both heating and cooling scenarios. (in video at 13:15 and 15:28) The demonstration clearly shows how the system maintains the temperature within the set range by turning the relay on and off as needed.
Chapters
- [00:00] Introduction
- [00:55] Heater/Cooler Control Explanation
- [03:28] Wiring Diagram
- [06:26] Code Explanation
- [13:15] Heater Control Demonstration
- [15:28] Cooler Control Demonstration
- [17:29] Conclusion
/*
* Voici le code Arduino pour le thermocouple K-Type MAX6675 avec relais et affichage. La broche de sortie 10 est connectée au relais. Lorsque la température atteint la valeur souhaitée, la broche 10 active le relais.
*
* Ce code a été expliqué dans notre vidéo à l'adresse https://youtu.be/cD5oOu4N_AE
*
* Écrit par Ahmad Nejrabi pour Robojax Video
* Date : 9 décembre 2017, à Ajax, Ontario, Canada
* Autorisation accordée pour partager ce code à condition que cette note soit conservée avec le code.
* Avertissement : ce code est "EN L'ÉTAT" et uniquement à des fins éducatives.
*
* /
*
* // Cet exemple est dans le domaine public. Profitez-en !
* // www.ladyada.net/learn/sensors/thermocouple
*/
#include "max6675.h"
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
void setup() {
Serial.begin(9600);
// Utilisez les broches Arduino
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
pinMode(10, OUTPUT); // définir la broche 10 comme sortie
Serial.println("Robojax: MAX6675 test");
// attendre que la puce MAX se stabilise
delay(500);
}
void loop() {
// test de lecture basique, il suffit d'afficher la température actuelle
Serial.print("C = ");
Serial.println(thermocouple.readCelsius());
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());
// Si la température dépasse 80,0°C, mettez le relais en marche.
if(thermocouple.readCelsius() > 80.00){
digitalWrite(10, LOW); // mettre la broche 10 à l'état bas
}else{
digitalWrite(10, HIGH); // mettre la broche 10 à HIGH
}
delay(1000);
}
++
/*
* Bibliothèque provenant de : // www.ladyada.net/learn/sensors/thermocouple
*
* Ceci est un code Arduino pour mesurer le capteur de thermocouple K-Type MAX6675 et contrôler un relais en tant que chauffage ou refroidissement.
*
* Regardez l'instruction vidéo pour ce code : https://youtu.be/dVh77wT-4Ao
*
* Écrit par Ahmad Shamshiri le 20 mai 2020 à Ajax, Ontario, Canada www.robojax.com
*
* Obtenez ce code et d'autres codes Arduino sur Robojax.com. Apprenez Arduino étape par étape dans un cours structuré avec tout le matériel, des schémas de câblage et des bibliothèques, le tout au même endroit. Achetez mon cours sur Udemy.com http://robojax.com/L/?id=62
*
* Si vous avez trouvé ce tutoriel utile, veuillez me soutenir afin que je puisse continuer à créer du contenu comme celui-ci. Vous pouvez me soutenir sur Patreon http://robojax.com/L/?id=63
*
* ou faire un don via PayPal http://robojax.com/L/?id=64
* Vidéos connexes :
* Introduction au MAX6675 K-Type : https://youtu.be/VGqONmUinqA
* Utilisation du thermocouple K-Type MAX6675 avec un affichage LED : https://youtu.be/cD5oOu4N_AE
* Utilisation du thermocouple K-Type MAX6675 avec LCD1602-I2C : https://youtu.be/BlhpktgPdKs
* Utilisation de 2 ou plusieurs thermocouples K-Type MAX6675 : https://youtu.be/c90NszbNG8c
* Utilisation du thermocouple K-Type MAX6675 comme contrôleur de chauffage ou de refroidissement : [cette vidéo]
* * Ce code est fourni "TEL QUEL" sans garantie ni responsabilité. Libre d'être utilisé tant que vous conservez cette note intacte.*
* Ce code a été téléchargé depuis Robojax.com.
* Ce programme est un logiciel libre : vous pouvez le redistribuer et/ou le modifier sous les termes de la GNU General Public License telle que publiée par la Free Software Foundation, soit la version 3 de la Licence, ou (à votre choix) toute version ultérieure.
*
* Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; sans même la garantie implicite de COMMERCIALISABILITÉ ou d'ADAPTATION À UN BUT PARTICULIER. Voir la GNU General Public License pour plus de détails.
*
* Vous auriez dû recevoir une copie de la GNU General Public License avec ce programme. Sinon, consultez <https://www.gnu.org/licenses/>.
*/
#include "max6675.h"
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
int GNDpin = 2;
int VCCpin =3;
int thermoCLK = 4;
int thermoCS = 5;
int thermoDO = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
const int relayPin =8;
const int relayON = LOW; // ne pas changer
const int relayOFF = HIGH; // ne pas changer
int relayState = relayOFF; // état initial du relais
const int TEMPERATURE_UNIT =1; // 1=Celcius, 2=Fahrenheit, 3=Kelvin
const float START_TEMPERATURE = 80.0; // unité ci-dessus
const float STOP_TEMPERATURE = 100.0; // unité ci-dessus
const int CONTROL_TYPE = 1; // 1=chauffage, 2=climatisation
float temperature;
void setup() {
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
Serial.begin(9600);
Serial.println("MAX6675 test with relay");
// utiliser les broches Arduino
pinMode(relayPin, OUTPUT); // broche pour relais
digitalWrite(relayPin, relayState);
pinMode(VCCpin, OUTPUT);
digitalWrite(VCCpin, HIGH);
pinMode(GNDpin, OUTPUT);
digitalWrite(GNDpin, LOW);
// attendre que la puce MAX se stabilise
delay(500);
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
}
void loop() {
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
readTemperature();
printTemperature();
loadControl();
if(temperature >=89.5)
{
// /
}
delay(1000);
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
} // boucle
/*
* loadControl()
* @brief contrôle la charge basée
* @param état peut être soit : relayON soit relayOFF
* @return ne retourne rien
* Écrit par Ahmad Shamshiri pour robojax.com
* le 20 mai 2020 à 15:23 à Ajax, Ontario, Canada
*/
void loadControl()
{
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
// Serial.print("Début : ");
// Serial.print(DÉBUT_TEMPERATURE);
// Serial.print(" Arrêt : ");
// Serial.println(TEMPERATURE_STOP);
if(CONTROL_TYPE ==1)
{
if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
{
relayControl(relayON);
}
if(STOP_TEMPERATURE <=temperature)
{
relayControl(relayOFF);
}
}else{
if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
{
relayControl(relayOFF);
}
if(STOP_TEMPERATURE <=temperature)
{
relayControl(relayON);
}
}
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
} // loadControl()
/*
* relayControl(int état))
* @brief met le relais EN MARCHE ou ARRÊT
* @param état est "relayON" ou "relayOFF" défini en haut du code
* @return ne retourne rien
* Écrit par Ahmad Shamshiri pour robojax.com
* le 20 mai 2020 à 15:23 à Ajax, Ontario, Canada
*/
void relayControl(int state)
{
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
if(state ==relayON)
{
digitalWrite(relayPin, relayON);
Serial.println("Relay ON");
}else{
digitalWrite(relayPin, relayOFF);
Serial.println("Relay OFF");
}
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
} // relayControl()
/*
* readTemperature()
* @brief lit la température en fonction de l'UNITÉ_DE_TEMPERATURE
* @param température moyenne
* @return renvoie l'une des valeurs ci-dessus
* Écrit par Ahmad Shamshiri pour robojax.com
* le 20 mai 2020 à 15:23 à Ajax, Ontario, Canada
*/
void readTemperature()
{
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
if(TEMPERATURE_UNIT ==2)
{
temperature = thermocouple.readFahrenheit(); // convertir en Fahrenheit
}else if(TEMPERATURE_UNIT ==3)
{
temperature = thermocouple.readCelsius() + 273.15; // convertir en Kelvin
}else{
temperature = thermocouple.readCelsius(); // retourner Celsius
}
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
} // readTemperature()
/*
* printTemperature()
* @brief imprime la température sur le moniteur série
* @param type de caractère
* @param "type" est un caractère
* C = Celsius
* K = Kelvin
* F = Fahrenheit
* @return aucun
* Écrit par Ahmad Shamshiri pour robojax.com
* le 08 mai 2020 à 02:45 à Ajax, Ontario, Canada
*/
void printTemperature()
{
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
Serial.print(temperature);
printDegree();
if(TEMPERATURE_UNIT ==2)
{
Serial.print("F");
}else if(TEMPERATURE_UNIT ==3)
{
Serial.print("K");
}else{
Serial.print("C");
}
Serial.println();
// Robojax.com chauffe/rafraîchisseur avec thermocouple MAX6675
} // printTemperature()
/*
* @brief imprime le symbole de degré sur le moniteur série
* @param aucun
* @return ne retourne rien
* Écrit par Ahmad Shamshiri le 13 juillet 2019
* pour le tutoriel Robojax Robojax.com
*/
void printDegree()
{
Serial.print("\\xC2");
Serial.print("\\xB0");
}
Ce dont vous pourriez avoir besoin
-
eBay
-
AliExpressAchetez 1 relais KY-019 5V à canal sur AliExpresss.click.aliexpress.com
-
BanggoodAchetez le MAX6675 de type K sur Banggood.banggood.com
Ressources et références
-
ExterneFiche technique MAX6675 (PDF)datasheets.maximintegrated.com
Fichiers📁
Aucun fichier disponible.