Código de búsqueda

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)
Idioma: C++
/*
 * Este es el código de Arduino para el termopar K-Type MAX6675 con relé y pantalla. El pin de salida 10 está conectado al relé. Cuando la temperatura alcanza el valor deseado, el pin 10 activa el relé.
 * 
 * Este código ha sido explicado en nuestro video en https://youtu.be/cD5oOu4N_AE
 * 
 * Escrito por Ahmad Nejrabi para Robojax Video Fecha: 9 de diciembre de 2017, en Ajax, Ontario, Canadá Se concede permiso para compartir este código siempre que se mantenga esta nota con el código. Descargo de responsabilidad: Este código es "TAL CUAL" y solo para fines educativos.
 * 
 * /
 * 
 * // Este ejemplo es de dominio público. ¡Disfruta!
 * // 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);
 // Utiliza los pines de Arduino
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
    pinMode(10, OUTPUT); // configurar el pin 10 como salida

  Serial.println("Robojax: MAX6675 test");
 // esperar a que el chip MAX se estabilice
  delay(500);
}

void loop() {
 // prueba de lectura básica, solo imprime la temperatura actual


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

 // Si la temperatura supera los 80.0C, enciende el relé.
   if(thermocouple.readCelsius() > 80.00){
    digitalWrite(10, LOW); // establecer pin 10 en bajo
   }else{
    digitalWrite(10, HIGH); // establecer pin 10 ALTO
   }


   delay(1000);
}
757-Arduino code to measure a MAX6675 K-type thermocouple
Idioma: C++
++
/*
 * Biblioteca de: // www.ladyada.net/learn/sensores/thermocouple
 * 
 * Este es un código de Arduino para medir el sensor termopar tipo K MAX6675 y controlar el relé como calentador o enfriador.
 * 
 * Vea el video instructivo de este código: https://youtu.be/dVh77wT-4Ao
 * 
 * Escrito por Ahmad Shamshiri el 20 de mayo de 2020 en Ajax, Ontario, Canadá.
 * www.robojax.com
 * 
 * Obtenga este código y otros códigos de Arduino en Robojax.com.
 * Aprenda Arduino paso a paso con un curso estructurado con todo el material, diagramas de cableado y bibliotecas en un solo lugar. Compre mi curso en Udemy.com: http://robojax.com/L/?id=62
 * 
 * Si este tutorial le resultó útil, por favor, apóyeme para que pueda seguir creando contenido como este. Puedes apoyarme en Patreon http://robojax.com/L/?id=63
 * 
 * o hacer una donación con PayPal http://robojax.com/L/?id=64
 * Videos relacionados:
 * Introducción al MAX6675 Tipo K: https://youtu.be/VGqONmUinqA
 * Uso del termopar MAX6675 Tipo K con pantalla LED: https://youtu.be/cD5oOu4N_AE
 * Uso del termopar MAX6675 Tipo K con LCD1602-I2C: https://youtu.be/BlhpktgPdKs
 * Uso de 2 o más termopares MAX6675 Tipo K: https://youtu.be/c90NszbNG8c
 * Uso del termopar MAX6675 Tipo K como controlador de calefacción o refrigeración: [este video]
 * * Este código se ofrece "tal cual", sin garantía ni responsabilidad. Uso gratuito siempre que conserve esta nota intacta.*
 * Este código se ha descargado de Robojax.com.
 * Este programa es software libre: puede redistribuirlo y/o modificarlo bajo los términos de la Licencia Pública General de GNU publicada por la Free Software Foundation, ya sea la versión 3 de la Licencia o (a su elección) cualquier versión posterior.
 * 
 * Este programa se distribuye con la esperanza de que sea útil, pero SIN NINGUNA GARANTÍA; ni siquiera la garantía implícita de COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la Licencia Pública General de GNU para obtener más detalles.
 * 
 * Debería haber recibido una copia de la Licencia Pública General de GNU junto con este programa. De no ser así, consulte <https://www.gnu.org/licenses/>.
 */


#include "max6675.h"
 // Calentador/enfriador Robojax.com con termopar 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; // no cambies
const int relayOFF = HIGH; // no cambies
int relayState = relayOFF; // estado inicial del relé

const int TEMPERATURE_UNIT =1; // 1=Celsius, 2=Fahrenheit, 3=Kelvin
const float START_TEMPERATURE = 80.0; // unidad de arriba
const float STOP_TEMPERATURE = 100.0; // unidad de arriba
const int CONTROL_TYPE = 1; // 1= calentador, 2= enfriador
float temperature;

void setup() {
 // Calentador/enfriador Robojax.com con termopar MAX6675
  Serial.begin(9600);
  Serial.println("MAX6675 test with relay");
 // usar pines Arduino
  pinMode(relayPin, OUTPUT); // pin para relé
  digitalWrite(relayPin, relayState);

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

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

 // Espere a que el chip MAX se estabilice
  delay(500);
 // Calentador/enfriador Robojax.com con termopar MAX6675
}

void loop() {
 // Calentador/enfriador Robojax.com con termopar MAX6675
   readTemperature();
   printTemperature();
   loadControl();
   if(temperature >=89.5)
   {
 // /
   }
   delay(1000);
 // Calentador/enfriador Robojax.com con termopar MAX6675
} // bucle



/*
 * loadControl()
 * @brief controla la carga según el estado de @param.
 * @param puede ser relayON o relayOFF.
 * @return no devuelve nada.
 * Escrito por Ahmad Shamshiri para robojax.com
 * el 20 de mayo de 2020 a las 15:23 en Ajax, Ontario, Canadá.
 */
void loadControl()
{
 // Calentador/enfriador Robojax.com con termopar MAX6675
 // Serial.print("Inicio: ");
 // Serial.print(TEMPERATURA_DE_INICIO);
 // Serial.print("Detener: ");
 // Serial.println(TEMPERATURA_DE_PARADA);
  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);
    }
  }

 // Calentador/enfriador Robojax.com con termopar MAX6675
} // Control de carga()


/*
 * relayControl(int state))
 * @brief activa o desactiva el relé.
 * @param state es "relayON" o "relayOFF" definido al principio del código.
 * @return devuelve "none".
 * Escrito por Ahmad Shamshiri para robojax.com
 * el 20 de mayo de 2020 a las 15:23 en Ajax, Ontario, Canadá.
 */
void relayControl(int state)
{
 // Calentador/enfriador Robojax.com con termopar MAX6675
  if(state ==relayON)
  {
    digitalWrite(relayPin, relayON);
    Serial.println("Relay ON");
  }else{
   digitalWrite(relayPin, relayOFF);
    Serial.println("Relay OFF");
  }
 // Calentador/enfriador Robojax.com con termopar MAX6675

} // Control de relé()


/*
 * readTemperature()
 * @brief lee la temperatura basándose en TEMPERATURE_UNIT
 * @param temperatura promedio
 * @return devuelve uno de los valores anteriores
 * Escrito por Ahmad Shamshiri para robojax.com
 * el 20 de mayo de 2020 a las 15:23 en Ajax, Ontario, Canadá
 */
void readTemperature()
{
 // Calentador/enfriador Robojax.com con termopar MAX6675

  if(TEMPERATURE_UNIT ==2)
   {
   temperature = thermocouple.readFahrenheit(); // convertir a Fahrenheit
   }else if(TEMPERATURE_UNIT ==3)
   {
    temperature = thermocouple.readCelsius() + 273.15; // convertir a Kelvin
   }else{
    temperature = thermocouple.readCelsius(); // regresar Celsius
   }
 // Calentador/enfriador Robojax.com con termopar MAX6675
} // leerTemperatura()

/*
 * printTemperature()
 * @brief imprime la temperatura en el monitor serial
 * @param tipo de carácter
 * @param "tipo" es carácter
 * °C = Celsius
 * K = Kelvin
 * F = Fahrenheit
 * @return none
 * Escrito por Ahmad Shamshiri para robojax.com
 * el 8 de mayo de 2020 a las 02:45 en Ajax, Ontario, Canadá
 */
void printTemperature()
{
 // Calentador/enfriador Robojax.com con termopar 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();
 // Calentador/enfriador Robojax.com con termopar MAX6675
} // imprimirTemperatura()

/*
 * @brief imprime el símbolo de grado en el monitor serial.
 * @param no muestra nada.
 * @return no devuelve nada.
 * Escrito por Ahmad Shamshiri el 13 de julio de 2019.
 * Para el tutorial de Robojax: Robojax.com
 */
void printDegree()
{
    Serial.print("\\xC2");
    Serial.print("\\xB0");
}

Cosas que podrías necesitar

Recursos y referencias

Archivos📁

No hay archivos disponibles.