Connection of Thermocouple (K-Type) to ESP8266/ESP32/Arduino

Several types of converters are available on Aliexpress to connect thermocouples. Thermocouples allow measuring very low and very high temperatures.

The MAX6675 Module for connecting the K-Type thermocouple to the 
SPI Bus Microcontroller

MAX6675 module for connecting the K-Type thermocouple to the microcontroller SPI bus

The inexpensive module MAX6675 send the data taken from the thermocouple via the SPI bus. It has 12-bit ADC on the board. It measures only positive temperatures from 0 to + 700 oC and can detect the breakage of the thermocouple. The price is about 1.5 USD.

MAX31855 Module for connecting the K-Type thermocouple to the microcontroller on the SPI bus

MAX31855 Module for connecting the K-Type thermocouple to the microcontroller on the SPI bus

A modern and more expensive module MAX31855, replacing MAX6675 also send data via the SPI bus. The features are better than MAX6675:

  • 14-bit ADC.
  • Apart from the definition of the breakage, the thermocouple output is further determined by VCC or GND.
  • The range of measured temperatures from -270 to +1768 oC (max ranges).
  • The price is about 1.9 USD with delivery to Russia.
Connecting the MAX31855 to the thermocouple
Connecting the MAX31855 to the thermocouple

There’s one problem with that board. The screw connector does not allow to fasten thermocouple terminals as it is intended by developers. It is necessary to clamp screws one of half of a terminal but holds normally.

On Aliexpress available at least three variants of boards with MAX31855. The most expensive comes with extensive strapping. Costs about 8 USD. I did not know the difference between the inexpensive option. Except that there are a separate entrance 3V and Vin. I used the 1.9 USD board and supply with 3.3V. In the description of the Chinese comparison, expensive board with the budget did not find.

In one of the articles, I wrote about circuitry for automatic cheese cooking. To measure the temperature of the milk and the temperature of the liquid in the steam bath just used a thermocouple.

Wemos D1 min and MAX6675K

MAX6675KWemos D1 Mini
Vcc 3.3 V (I used BP)
Gnd Gnd (from BP and Wemos D1 Mini)
SO D6 (GPIO12)
SS/CS D7 (GPIO13)
Csk D8 (GPIO15)

The Earth (GND) MAX6675 and (GND) Wemos D1 Mini must be connected if the MAX6675 is powered by a separate power source.

The SPI Interface uses 4 lines for data exchange:

  • SCLK-Serial Clock: Clock signal (from lead) Ot
    her designations: SCK, CLK
  • MOSI-Master Output, Slave Input: Data from leading to Vedomo
    mudrugie notation: SDI, DI, SI
  • MISO — Master Input, Slave Output: Data from the Vedushhemudrug
    ie-driven notation: SDO, do, SO
  • SS-Slave Select: Slave selection; is established by the
    Other designations: nCS, CS, CSB, CSN, nSS, STE

The https://github.com/YuriiSalimov/MAX6675_Thermocouple library is used To remove the thermocouple data from the MAX6675 card. It is based on the older Library of Adafruit Max6675 Library

#include "MAX6675_Thermocouple. H"//https://github.com/YuriiSalimov/MAX6675_Thermocouple

#define SCK_PIN 15//D8
#define CS_PIN 13//D7
#define SO_PIN 12//D6

/**
   How Many readings are taken to determine a mean temperature.
   The more values, the longer a calibration is performed,
   But the readings will be more accurate.
*/
#define READINGS_NUMBER 20

/**
   Delay time between a temperature readings
   From the temperature sensor (ms).
*/
#define DELAY_TIME 20


MAX6675_Thermocouple * Thermocouple = NULL;
 
void Setup ()
{
 Serial. Begin (9600);

 Thermocouple = new MAX6675_Thermocouple (SCK_PIN, CS_PIN, SO_PIN, Readings_number, DELAY_TIME);
}
 
void Loop ()
{
  Const double Celsius = thermocouple-> readCelsius ();
  Const DOUBLE Kelvin = thermocouple-> readKelvin ();
  Const double Fahrenheit = thermocouple-> readFahrenheit ();
  Serial. Print ("Temperature:");
  Serial. Print (String (Celsius) + "C,");
  Serial. Print (String (Kelvin) + "K,");
  Serial. println (String (Fahrenheit) + "F");
  Delay (500);
}

The Result of the program:

19:14:01.820-> Temperature: 28.50 C, 301.65 K, 83.30 F
19:14:05.538-> Temperature: 28.75 C, 301.90 K, 83.75 F
19:14:09.278-> Temperature: 28.50 C, 301.65 K, 83.30 F
19:14:13.025-> Temperature: 28.50 C, 301.65 K, 83.30 F
19:14:16.730-> Temperature: 28.75 C, 301.90 K, 83.75 F

Sensors should be calibrated because the temperature in the room was not 28.5 Oc, but lower.

ESP32 DevKit and MAX6675K

This fee is used as The ESP32 board.

MAX6675K ESP32 DevKit
Vcc 3.3 V (I used BP)
Gnd Gnd (from BP and Wemos D1 Mini)
SO 12 (IO12)
SS/CS 13 (IO13)
Csk 15 (IO15)

The Code unchanged works on ESP32.

#include "MAX6675_Thermocouple. H"//https://github.com/YuriiSalimov/MAX6675_Thermocouple

#define SCK_PIN 15
#define CS_PIN 13
#define SO_PIN 12

/**
   How Many readings are taken to determine a mean temperature.
   The more values, the longer a calibration is performed,
   But the readings will be more accurate.
*/
#define READINGS_NUMBER 20

/**
   Delay time between a temperature readings
   From the temperature sensor (ms).
*/
#define DELAY_TIME 20


MAX6675_Thermocouple * Thermocouple = NULL;
 
void Setup ()
{
 Serial. Begin (9600);

 Thermocouple = new MAX6675_Thermocouple (SCK_PIN, CS_PIN, SO_PIN, Readings_number, DELAY_TIME);
}
 
void Loop ()
{
  Const double Celsius = thermocouple-> readCelsius ();
  Const DOUBLE Kelvin = thermocouple-> readKelvin ();
  Const double Fahrenheit = thermocouple-> readFahrenheit ();
  Serial. Print ("Temperature:");
  Serial. Print (String (Celsius) + "C,");
  Serial. Print (String (Kelvin) + "K,");
  Serial. println (String (Fahrenheit) + "F");
  Delay (500);
}

Result:

21:16:42.882-> Temperature: 26.25 C, 299.40 K, 79.25 F
21:16:46.554-> Temperature: 26.75 C, 299.90 K, 80.15 F
21:16:50.256-> Temperature: 26.50 C, 299.65 K, 79.70 F
21:16:53.926-> Temperature: 26.25 C, 299.40 K, 79.25 F
21:17:30.707-> Temperature: 26.50 C, 299.65 K, 79.70 F

Strange, but ESP32 reads a more correct temperature than ESP8266. The Temperature in the room is just somewhere around 26.5 Oc.

ESP32 and MAX31855

No problem at ESP32 earned library from Adafruit C software emulation. GPIO used the same as for the example with MAX6675.

#include < SPI. h.
#include "Adafruit_MAX31855. H"//https://github.com/adafruit/Adafruit-MAX31855-library/

Default connection is using software SPI, but comment and uncomment one of
The two examples below to switch between software SPI and hardware SPI:

Example creating a Thermocouple instance with software SPI on any three
Digital IO pins.
#define MAXDO 12
#define MAXCS 13
#define MAXCLK 15

Initialize the Thermocouple
Adafruit_MAX31855 thermocouple (MAXCLK, MAXCS, MAXDO);

Example creating a Thermocouple instance with hardware SPI
On a given CS pin.
#define MAXCS 10
Adafruit_MAX31855 thermocouple (MAXCS);

void Setup () {
  Serial. Begin (115200);
 
  Serial. println ("MAX31855 test");  Wait for MAX chip to stabilize
  Delay (500);
}

void Loop () {
  Basic readout test, just print the current temp
   Serial. Print ("Temperature:");

   Double c = thermocouple. readCelsius ();
   
   if (isNaN (c)) {
     Serial. println ("Something wrong with thermocouple!");
   Else
     Serial. Print (String (c) + "C,");
     Serial. Print (String (thermocouple. readFarenheit ()) + "F,");
     Serial. println (String (thermocouple. readInternal ()) + "C (internal)");
   }
    Delay (1000);
}

Result:

15:39:44.805-> Temperature: 24.25 C, 77.90 F, 25.94 C (internal)
15:39:45.977-> Temperature: 24.25 C, 77.45 F, 25.88 C (internal)
15:39:47.190-> Temperature: 25.50 C, 77.90 F, 25.94 C (internal)
15:39:48.396-> Temperature: 25.25 C, 77.90 F, 25.94 C (internal)
15:39:49.618-> Temperature: 25.00 C, 78.35 F, 25.94 C (internal)

Let’s try to run the library with hardware SPI:

  • SPI MOSI-GPIO23
  • SPI MISO-GPIO19
  • SPI SCK-GPIO18
  • SPI SS (CS)-GPIO5

The Code is much simpler, but everything works fine.

#include < SPI. h.
#include "Adafruit_MAX31855. H"//https://github.com/adafruit/Adafruit-MAX31855-library/

#define MAXCS 5
Adafruit_MAX31855 thermocouple (MAXCS);

void Setup () {
  Serial. Begin (115200);
 
  Serial. println ("MAX31855 test");  Wait for MAX chip to stabilize
  Delay (500);
}

void Loop () {
  Basic readout test, just print the current temp
   Serial. Print ("Temperature:");

   Double c = thermocouple. readCelsius ();
   
   if (isNaN (c)) {
     Serial. println ("Something wrong with thermocouple!");
   Else
     Serial. Print (String (c) + "C,");
     Serial. Print (String (thermocouple. readFarenheit ()) + "F,");
     Serial. println (String (thermocouple. readInternal ()) + "C (internal)");
   }
    Delay (1000);
}

The https://github.com/SV-Zanshin/MAX31855/Library works fine with the SPI hardware.

#include < MAX31855. h >//https://github.com/SV-Zanshin/MAX31855/
/*******************************************************************************************************************
* * Declare All Program Constants * *
*******************************************************************************************************************/
Const uint32_t SERIAL_SPEED = 115200; < Set the baud rate for Serial I/O
Const uint8_t SPI_CHIP_SELECT = 5;    < Chip-Select PIN for SPI
Const uint8_t SPI_MISO = MISO; < Master-In, Slave-Out PIN for SPI
Const uint8_t SPI_SYSTEM_CLOCK = SCK;  < System Clock PIN for SPI

/*******************************************************************************************************************
* * Declare Global variables and instantiate classes * *
*******************************************************************************************************************/
MAX31855_Class MAX31855; < Create an instance of MAX31855

/***************************************************************************************************************//*!
* @brief Arduino method called once at startup to initialize the system
* @details This is an Arduino IDE method which is called first upon boot or restart. It is only called one time
* And then control goes to the main "loop ()" method, from which control never returns
* @return void
*******************************************************************************************************************/
void Setup ()
{
  Serial. Begin (SERIAL_SPEED);
  #ifdef __Avr_atmegea32u4__//If This is a 32U4 processor, then wait 3 seconds for the interface to initialize
    Delay (3000);
  #endif
  Serial. println (F ("Starting software SPI demo program for MAX31855"));
  Serial. Print (F ("Initializing MAX31855 ensorn"));
  /********************************************************************************************
  * * Uncomment out either the hardware or software SPI call, depending upon which is in use * *
  ********************************************************************************************/
  while (! MAX31855. Begin (SPI_CHIP_SELECT))//Hardware SPI for MAX31855
while (! MAX31855. Begin (SPI_CHIP_SELECT, SPI_MISO, SPI_SYSTSEM_CLOCK))//Software SPI for MAX31855
  {
    Serial. println (F ("Unable to start MAX31855. Waiting 3 seconds. "));
    Delay (3000);
  }//of loop until device is located
 Serial. println ();
}//Of method Setup ()

/***************************************************************************************************************//*!
* @brief Arduino method for the main program loop
* @details This is the main program for the Arduino IDE, it is an infinite loop and keeps on repeating.
* @return void
*******************************************************************************************************************/
void Loop ()
{
  int32_t ambientTemperature = MAX31855. readAmbient (); Retrieve MAX31855 die Ambient temperature
  int32_t probeTemperature = MAX31855. readProbe ();   Retrieve Thermocouple Probe Temp
  uint8_t faultCode = MAX31855. Fault ();       Retrieve any error codes
  if (faultCode)//Display error code if present
  {
    Serial. Print ("Fault Code");
    Serial. Print (faultCode);
    Serial. println ("returned.");
  }
  Else
  {
    Serial. Print ("Ambient Temperature is");
    Serial. Print ((float) ambientTemperature/1000,3);
    Serial. println ("xC2xB0" "C");
    Serial. Print ("Probe Temperature is");
    Serial. Print ((float) probeTemperature/1000,3);
    Serial. println ("xC2xB0" "Cn");
  }//Of If-then-else an error occurred
  Delay (5000);
}//Of method Loop ()

Useful Links

Spread the love
This entry was posted in IT Recipies. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *