Cerca codice

Lesson 47: Using K-Type MAX6675 as thermostat with relay | Arduino Step By Step Course

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)
  • relayON and relayOFF: 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_TEMPERATURE and STOP_TEMPERATURE: These variables define the temperature range for the control system. The system will turn on the relay when the temperature falls below START_TEMPERATURE and turn it off when the temperature reaches STOP_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
25-Arduino code for a MAX6675 K-type thermocouple with relay (no display)
Lingua: C++
/*
 * Questo è il codice Arduino per il termocoppia K-Type MAX6675 con relè e display. 
 * Il pin di uscita 10 è collegato al relè. 
 * Quando la temperatura raggiunge il valore desiderato, il pin 10 accende il relè.
 * 
 * Questo codice è stato spiegato nel nostro video su https://youtu.be/cD5oOu4N_AE
 * 
 * Scritto da Ahmad Nejrabi per Robojax Video 
 * Data: 9 dicembre 2017, ad Ajax, Ontario, Canada 
 * Autorizzazione concessa per condividere questo codice a condizione che questa nota venga mantenuta con il codice. 
 * Dichiarazione di non responsabilità: questo codice è "COSÌ COM'È" e solo per scopi educativi.
 * 
 * /
 * 
 * // Questo esempio è di pubblico dominio. Divertiti! 
 * // 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);
 // Usa i pin di Arduino
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
    pinMode(10, OUTPUT); // imposta il pin 10 come uscita

  Serial.println("Robojax: MAX6675 test");
 // aspetta che il chip MAX si stabilizzi
  delay(500);
}

void loop() {
 // test di lettura di base, stampa semplicemente la temperatura attuale


   Serial.print("C = ");
   Serial.println(thermocouple.readCelsius());
   Serial.print("F = ");
   Serial.println(thermocouple.readFahrenheit());

 // Se la temperatura supera gli 80,0 °C, accendi il relè.
   if(thermocouple.readCelsius() > 80.00){
    digitalWrite(10, LOW); // imposta il pin 10 a LOW
   }else{
    digitalWrite(10, HIGH); // imposta il pin 10 su ALTO
   }


   delay(1000);
}
757-Arduino code to measure a MAX6675 K-type thermocouple
Lingua: C++
++
/*
 * Libreria da: // www.ladyada.net/learn/sensors/thermocouple
 * 
 * Questo è il codice Arduino per misurare il sensore MAX6675 termocoppia di tipo K
 * e controllare un relè come riscaldatore o condizionatore
 * 
 * Guarda le istruzioni video per questo codice: https://youtu.be/dVh77wT-4Ao
 * 
 * Scritto da Ahmad Shamshiri il 20 maggio 2020 ad Ajax, Ontario, Canada
 * www.robojax.com
 * 
 * Ottieni questo codice e altri codici Arduino da Robojax.com.
 * Impara Arduino passo dopo passo in un corso strutturato con tutto il materiale, schemi di wiring e librerie
 * tutto in un unico posto. Acquista il mio corso su Udemy.com http://robojax.com/L/?id=62
 * 
 * Se hai trovato utile questo tutorial, per favore sostienimi così posso continuare a creare 
 * contenuti come questo. Puoi sostenermi su Patreon http://robojax.com/L/?id=63
 * 
 * o fare una donazione tramite PayPal http://robojax.com/L/?id=64
 * Video correlati:
 * Introduzione a MAX6675 tipo K: https://youtu.be/VGqONmUinqA
 * Utilizzo della termocoppia tipo K MAX6675 con display LED: https://youtu.be/cD5oOu4N_AE
 * Utilizzo della termocoppia tipo K MAX6675 con LCD1602-I2C: https://youtu.be/BlhpktgPdKs
 * Utilizzo di 2 o più termocoppie tipo K MAX6675: https://youtu.be/c90NszbNG8c
 * Utilizzo della termocoppia tipo K MAX6675 come controllore per riscaldatore o condizionatore: [questo video]
 *  * Questo codice è "COSÌ COM'È" senza garanzia o responsabilità. Libero di essere utilizzato purché tu mantenga intatta questa nota.* 
 * Questo codice è stato scaricato da Robojax.com
 *  Questo programma è software libero: puoi ridistribuirlo e/o modificarlo
 * ai sensi dei termini della GNU General Public License come pubblicato dalla
 * Free Software Foundation, o della versione 3 della Licenza, o
 * (a tua scelta) di qualsiasi versione successiva.
 * 
 *  Questo programma è distribuito con la speranza che sia utile,
 * ma SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di
 * COMMERCIABILITÀ o IDONEITÀ PER UN PARTICOLARE SCOPO. Vedi la
 * GNU General Public License per ulteriori dettagli.
 * 
 *  Dovresti aver ricevuto una copia della GNU General Public License
 * insieme a questo programma. Se non l'hai fatto, vedi <https://www.gnu.org/licenses/>.
 */


#include "max6675.h"
 // Riscaldatore/raffreddatore Robojax.com con termocoppia 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; // non cambiare
const int relayOFF = HIGH; // non cambiare
int relayState = relayOFF; // stato iniziale del relè

const int TEMPERATURE_UNIT =1; // 1=Gradi Celsius, 2=Gradi Fahrenheit, 3=Kelvin
const float START_TEMPERATURE = 80.0; // unità sopra
const float STOP_TEMPERATURE = 100.0; // unità sopra
const int CONTROL_TYPE = 1; // 1= riscaldatore, 2=raffreddatore
float temperature;

void setup() {
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
  Serial.begin(9600);
  Serial.println("MAX6675 test with relay");
 // usa i pin di Arduino
  pinMode(relayPin, OUTPUT); // pin per relè
  digitalWrite(relayPin, relayState);

  pinMode(VCCpin, OUTPUT);
  digitalWrite(VCCpin, HIGH);

  pinMode(GNDpin, OUTPUT);
  digitalWrite(GNDpin, LOW);

 // attendere che il chip MAX si stabilizzi
  delay(500);
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
}

void loop() {
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
   readTemperature();
   printTemperature();
   loadControl();
   if(temperature >=89.5)
   {
 // /
   }
   delay(1000);
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
} // ciclo



/*
 * loadControl()
 * @brief controlla il carico basato 
 * @param stato può essere : relayON o relayOFF
 * @return non restituisce nulla
 * Scritto da Ahmad Shamshiri per robojax.com
 * il 20 maggio 2020 alle 15:23 ad Ajax, Ontario, Canada
 */
void loadControl()
{
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
 // Serial.print("Inizio: ");
 // Serial.print(START_TEMPERATURE);
 //  Serial.print(" Ferma: ");
 // Serial.println(TEMPERATURA_DI_FERMO);
  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);
    }
  }

 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
} // loadControl()


/*
 * relayControl(int stato))
 * @brief accende o spegne il relè
 * @param stato è "relayON" o "relayOFF" definito all'inizio del codice
 * @return non restituisce nulla
 * Scritto da Ahmad Shamshiri per robojax.com
 * il 20 maggio 2020 alle 15:23 ad Ajax, Ontario, Canada
 */
void relayControl(int state)
{
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
  if(state ==relayON)
  {
    digitalWrite(relayPin, relayON);
    Serial.println("Relay ON");
  }else{
   digitalWrite(relayPin, relayOFF);
    Serial.println("Relay OFF");
  }
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675

} // relayControl()


/*
 * readTemperature()
 * @brief legge la temperatura basata sull'UNITÀ_DI_TEMPERATURA
 * @param temperatura media
 * @return restituisce uno dei valori sopra
 * Scritto da Ahmad Shamshiri per robojax.com
 * il 20 maggio 2020 alle 15:23 ad Ajax, Ontario, Canada
 */
void readTemperature()
{
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675

  if(TEMPERATURE_UNIT ==2)
   {
   temperature = thermocouple.readFahrenheit(); // convertire a Fahrenheit
   }else if(TEMPERATURE_UNIT ==3)
   {
    temperature = thermocouple.readCelsius() + 273.15; // convertire in Kelvin
   }else{
    temperature = thermocouple.readCelsius(); // restituire Celsius
   }
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
} // leggiTemperatura()

/*
 * printTemperature()
 * @brief stampa la temperatura sul monitor seriale
 * @param tipo di carattere
 * @param "tipo" è carattere
 *  C = Celsius
 *  K = Kelvin
 *  F = Fahrenheit
 * @return nessuno
 * Scritto da Ahmad Shamshiri per robojax.com
 * il 08 maggio 2020 alle 02:45 ad Ajax, Ontario, Canada
 */
void printTemperature()
{
 // Riscaldatore/raffreddatore Robojax.com con termocoppia 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();
 // Riscaldatore/raffreddatore Robojax.com con termocoppia MAX6675
} // printTemperatura()

/*
 * @brief stampa il simbolo di grado sul monitor seriale
 * @param nessuno
 * @return non restituisce nulla
 * Scritto da Ahmad Shamshiri il 13 luglio 2019
 * per il Tutorial Robojax Robojax.com
 */
void printDegree()
{
    Serial.print("\\xC2");
    Serial.print("\\xB0");
}

Cose di cui potresti avere bisogno

Risorse e riferimenti

File📁

Nessun file disponibile.