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
/*
* این شِفر (کود) آردوینو برای ترموکوپل K-Type MAX6675 با ریلی و نمایشگر میباشد.
* پایه خروجی ۱۰ به ریلی متصل است.
* زمانی که دما به مقدار مطلوب میرسد، پایه ۱۰ ریلی را روشن میکند.
*
* این شِفر (کود) در ویدیوی ما در https://youtu.be/cD5oOu4N_AE توضیح داده شده است.
*
* نوشته شده توسط احمد نجربی برای ویدیو روبوجکس
* تاریخ: ۹ دسامبر ۲۰۱۷، در اِجَکس، انتاریو، کانادا
* اجازه داده شده است که این شِفر (کود) به اشتراک گذاشته شود به شرط آنکه این
* یادداشت همراه شِفر (کود) نگهداشته شود.
* سلب مسئولیت: این شِفر (کود) به صورت "همانطور که هست" و فقط برای مقاصد آموزشی ارائه شده است.
*/
#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);
// پایههای آردوینو را استفاده کنید
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
pinMode(10, OUTPUT); // پایه ۱۰ را به عنوان خروجی تنظیم کنید
Serial.println("Robojax: MAX6675 test");
// منتظر بمانید تا چیپ MAX تثبیت شود
delay(500);
}
void loop() {
// امتحان قرائت پایه، فقط دمای فعلی را چاپ کن
Serial.print("C = ");
Serial.println(thermocouple.readCelsius());
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());
// اگر دما بالای 80.0 درجه سانتیگراد برود، ریلی را روشن کنید.
if(thermocouple.readCelsius() > 80.00){
digitalWrite(10, LOW); // پایه 10 را LOW تنظیم کن
}else{
digitalWrite(10, HIGH); // پایه 10 را HIGH تنظیم کن
}
delay(1000);
}
++
/*
* کتابخانه از: // www.ladyada.net/learn/sensors/thermocouple
*
* این کد آردوینو برای اندازهگیری سنسور ترموکوپل نوع K مدل MAX6675 و کنترل رله به عنوان گرمکن یا سردکن است.
*
* آموزش ویدیویی این کد را تماشا کنید: https://youtu.be/dVh77wT-4Ao
*
* نوشته شده توسط احمد شمشیری در 20 مه 2020 در آژاکس، انتاریو، کانادا
* www.robojax.com
*
* این کد و سایر کدهای آردوینو را از Robojax.com دریافت کنید.
*
* آردوینو را گام به گام در یک دوره ساختار یافته با تمام مطالب، نمودارهای سیمکشی و کتابخانهها یاد بگیرید
*
* همه در یک مکان. دوره من را در Udemy.com خریداری کنید http://robojax.com/L/?id=62
*
* اگر این آموزش را مفید یافتید، لطفاً از من حمایت کنید تا بتوانم به ایجاد محتوایی مانند این ادامه دهم. میتوانید در Patreon از من حمایت کنید http://robojax.com/L/?id=63
*
* یا با استفاده از PayPal http://robojax.com/L/?id=64 کمک مالی کنید.
*
* ویدیوهای مرتبط:
*
* معرفی MAX6675 K-Type: https://youtu.be/VGqONmUinqA
* استفاده از ترموکوپل MAX6675 K-Type با نمایشگر LED: https://youtu.be/cD5oOu4N_AE
* استفاده از ترموکوپل MAX6675 K-Type با LCD1602-I2C: https://youtu.be/BlhpktgPdKs
* استفاده از 2 یا بیشتر ترموکوپل MAX6675 K-Type: https://youtu.be/c90NszbNG8c
* استفاده از ترموکوپل MAX6675 K-Type به عنوان کنترلکننده بخاری یا کولر: [این ویدیو]
*
* * این کد "همانطور که هست" است و هیچ ضمانت یا مسئولیتی ندارد. تا زمانی که این یادداشت را دست نخورده نگه دارید، استفاده از آن رایگان است.*
*
* این کد از Robojax.com دانلود شده است.
*
* این برنامه یک نرمافزار رایگان است: میتوانید آن را تحت شرایط مجوز عمومی گنو که توسط بنیاد نرمافزار آزاد منتشر شده است، چه نسخه ۳ مجوز، یا (به انتخاب شما) هر نسخه بعدی، مجدداً توزیع و/یا اصلاح کنید.
*
* این برنامه به امید مفید بودن توزیع شده است،
*
* اما بدون هیچ گونه ضمانتی؛ حتی بدون ضمانت ضمنی قابلیت فروش یا مناسب بودن برای یک هدف خاص. برای جزئیات بیشتر به مجوز عمومی گنو مراجعه کنید.
*
* شما باید یک نسخه از مجوز عمومی گنو را همراه با این برنامه دریافت کرده باشید. در غیر این صورت، به <https://www.gnu.org/licenses/> مراجعه کنید.
*/
#include "max6675.h"
// بخاری/کولر Robojax.com با ترموکوپل 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; // تغییر نکن
const int relayOFF = HIGH; // تغییر نکن
int relayState = relayOFF; // حالت اولیه رله
const int TEMPERATURE_UNIT =1; // 1=سانتیگراد، 2=فارنهایت، 3=کلوین
const float START_TEMPERATURE = 80.0; // واحد فوق
const float STOP_TEMPERATURE = 100.0; // واحد فوق
const int CONTROL_TYPE = 1; // ۱= بخاری، ۲= کولر
float temperature;
void setup() {
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
Serial.begin(9600);
Serial.println("MAX6675 test with relay");
// استفاده از پینهای آردوینو
pinMode(relayPin, OUTPUT); // پین برای رله
digitalWrite(relayPin, relayState);
pinMode(VCCpin, OUTPUT);
digitalWrite(VCCpin, HIGH);
pinMode(GNDpin, OUTPUT);
digitalWrite(GNDpin, LOW);
// صبر کنید تا تراشه MAX تثبیت شود
delay(500);
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
}
void loop() {
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
readTemperature();
printTemperature();
loadControl();
if(temperature >=89.5)
{
// /
}
delay(1000);
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
} // حلقه
/*
* loadControl()
* @brief بار را بر اساس بار کنترل میکند.
* @param state میتواند یکی از دو حالت relayON یا relayOFF باشد.
* @return مقدار none را برمیگرداند.
* نوشته شده توسط احمد شمشیری برای robojax.com
* در تاریخ 20 می 2020، ساعت 15:23 در آژاکس، انتاریو، کانادا
*/
void loadControl()
{
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
// Serial.print("شروع: ");
// چاپ سریال (شروع دما)؛
// Serial.print("توقف: ");
// تابع Serial.println(STOP_TEMPERATURE) را اجرا میکند.
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 با ترموکوپل MAX6675
} // بارگذاری کنترل()
/*
* relayControl(int state))
* @brief رله را روشن یا خاموش میکند
* @param state در بالای کد "relayON" یا "relayOFF" تعریف شده است
* @return هیچ مقداری را برنمیگرداند
* نوشته شده توسط احمد شمشیری برای robojax.com
* در 20 مه 2020، ساعت 15:23 در آژاکس، انتاریو، کانادا
*/
void relayControl(int state)
{
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
if(state ==relayON)
{
digitalWrite(relayPin, relayON);
Serial.println("Relay ON");
}else{
digitalWrite(relayPin, relayOFF);
Serial.println("Relay OFF");
}
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
} // رله کنترل ()
/*
* تابع readTemperature()
* @brief دما را بر اساس TEMPERATURE_UNIT میخواند.
* @param average temperature
* @return یکی از مقادیر بالا را برمیگرداند.
* نوشته شده توسط احمد شمشیری برای robojax.com
* در تاریخ 20 می 2020، ساعت 15:23 در آژاکس، انتاریو، کانادا
*/
void readTemperature()
{
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
if(TEMPERATURE_UNIT ==2)
{
temperature = thermocouple.readFahrenheit(); // تبدیل به فارنهایت
}else if(TEMPERATURE_UNIT ==3)
{
temperature = thermocouple.readCelsius() + 273.15; // تبدیل به کلوین
}else{
temperature = thermocouple.readCelsius(); // بازگشت سانتیگراد
}
// بخاری/کولر Robojax.com با ترموکوپل MAX6675
} // تابع readTemperature()
/*
* تابع printTemperature()
* @brief دما را روی مانیتور سریال چاپ میکند.
* @param نوع کاراکتری
* @param "type" کاراکتری است.
* C = سانتیگراد
* K = کلوین
* F = فارنهایت
* @return none
* نوشته شده توسط احمد شمشیری برای robojax.com
* در تاریخ ۸ می ۲۰۲۰، ساعت ۰۲:۴۵ در آژاکس، انتاریو، کانادا
*/
void printTemperature()
{
// بخاری/کولر Robojax.com با ترموکوپل 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 با ترموکوپل MAX6675
} // تابع چاپ دما ()
/*
* @brief نماد درجه را روی مانیتور سریال چاپ میکند.
* @param none
* @return چیزی برنمیگرداند.
* نوشته شده توسط احمد شمشیری در ۱۳ جولای ۲۰۱۹
* برای آموزش روبوجاکس Robojax.com
*/
void printDegree()
{
Serial.print("\\xC2");
Serial.print("\\xB0");
}
مواردی که ممکن است به آنها نیاز داشته باشید
-
علیاکسپرسیک ریلی 5 ولت کانال 1 مدل KY-019 از علیاکسپرس خریداری کنیدs.click.aliexpress.com
-
بنگ گودخرید حساس(حس کننده) K-Type MAX6675 از بنگودbanggood.com
منابع و مراجع
-
خارجیدیتاشیت MAX6675 (PDF)datasheets.maximintegrated.com
فایلها📁
هیچ فایلی موجود نیست.