Measuring Temperature with a Thermocouple, Using the MAX6675 with STM32 as an Example.
Recently I needed a reflow soldering oven. For those who don't know what it is, I'll briefly describe how it works and what it's for. I have a dozen boards that need to be assembled, and the components on them are mostly SMD. Right now the boards are assembled as follows: first I apply solder paste to the pads with a syringe, then I place the components. Next the solder paste has to be heated so that the balls it consists of fuse together and with the thin layer of tin the pads are coated with. Nothing tricky, but heating with a hot air gun takes time and attention, and I'd like this process to happen without my involvement. Special ovens are used for exactly this purpose; you can find information about them by googling "reflow soldering".
I just checked AliExpress: the cheapest oven costs $400 plus another $160 for shipping. That's a lot, and if you also factor in customs duties of around 25%, it comes to about $700, which is too much. So I'll try to make one myself from a toaster oven.
The first thing to learn is how to measure temperature.
The graph above shows that the maximum temperature during reflow can reach 260 degrees, so the popular ds18b20 won't work for this purpose, but a thermocouple definitely will.

The thermocouple shown in Figure 1 consists of two conductors of dissimilar metals joined together at one end, called the measuring ("hot") junction. The other end, where the conductors are not joined, is connected to the traces of the signal processing circuit, usually made of copper. This transition between the thermocouple metals and the copper traces is called the reference ("cold") junction.
So when measuring temperature with a thermocouple, we get a thermoelectric voltage made up of the sum of the thermoelectric voltages of the hot and cold junctions. (actually the difference, since their potentials have opposite signs)
One question that may arise: why are there two connections where the thermocouple conductors meet the copper traces, yet we count them as one junction?
To answer this question, we need to recall the law of intermediate metals:
So essentially we can assume that there are no extension wires!!! and the connection points are joined together and form the cold junction!!!!.
I think it's already clear that when measuring temperature with a thermocouple, the influence of the cold junction must be taken into account, and this can be done in two ways.
Once you have the thermoelectric voltage measured with the thermocouple, which is essentially the sum of the thermoelectric voltages of the hot and cold junctions, you can calculate the combined temperature of the two junctions. Then, using a sensor that measures absolute temperature, for example a ds18b20, measure the cold junction temperature and subtract it from the combined temperature of the two junctions. This process is known as reference junction compensation (cold junction compensation).
But the same cold junction compensation can be done more simply: subtract the thermoelectric voltage of the cold junction from the total thermoelectric voltage, then calculate the temperature.
The question is how to obtain the thermoelectric voltage of the cold junction.
This can be done with a low-power silicon diode, whose p-n junction voltage drop depends on absolute temperature and is about 2mV/degree. Then, by amplifying the diode voltage drop by the required factor (depending on the thermocouple type), we get the thermoelectric voltage of the cold junction. We subtract it from the total voltage, thereby obtaining the thermoelectric voltage of the hot junction, and then its temperature.
That's it for the theory; let's move on to practice. For this, I bought a shield based on the MAX6675, with built-in cold junction compensation for a K-type thermocouple and an SPI interface.

The output is 12 bits with a resolution of 0.25 degrees, i.e. it can measure temperatures from 0 to 1024 degrees.
The read procedure is very simple: after the CS pin is pulled low, you can apply the clock signal and read the temperature on the falling edge; it couldn't be simpler.
The frame format looks like this.

Here too everything is simple: bits 3 to 14 hold the temperature value. If bit two is zero, the thermocouple is connected; otherwise it is disconnected. I don't understand what bit one is for.
As promised, the code for the stm32f103vet6.
To display the temperature in a separate window, as in the screenshot above, you need to enable SWD debugging using the SWO pin. You can read how to do this here.
I bought the shield here.
I just checked AliExpress: the cheapest oven costs $400 plus another $160 for shipping. That's a lot, and if you also factor in customs duties of around 25%, it comes to about $700, which is too much. So I'll try to make one myself from a toaster oven.
The first thing to learn is how to measure temperature.
The graph above shows that the maximum temperature during reflow can reach 260 degrees, so the popular ds18b20 won't work for this purpose, but a thermocouple definitely will.

The thermocouple shown in Figure 1 consists of two conductors of dissimilar metals joined together at one end, called the measuring ("hot") junction. The other end, where the conductors are not joined, is connected to the traces of the signal processing circuit, usually made of copper. This transition between the thermocouple metals and the copper traces is called the reference ("cold") junction.
So when measuring temperature with a thermocouple, we get a thermoelectric voltage made up of the sum of the thermoelectric voltages of the hot and cold junctions. (actually the difference, since their potentials have opposite signs)
One question that may arise: why are there two connections where the thermocouple conductors meet the copper traces, yet we count them as one junction?
To answer this question, we need to recall the law of intermediate metals:
A conductor of any metal connected in series in a thermocouple circuit and kept at a uniform temperature does not affect the measured thermoelectric voltage. This law makes it possible to extend thermocouple wires with a homogeneous metal by soldering.
So essentially we can assume that there are no extension wires!!! and the connection points are joined together and form the cold junction!!!!.
I think it's already clear that when measuring temperature with a thermocouple, the influence of the cold junction must be taken into account, and this can be done in two ways.
Once you have the thermoelectric voltage measured with the thermocouple, which is essentially the sum of the thermoelectric voltages of the hot and cold junctions, you can calculate the combined temperature of the two junctions. Then, using a sensor that measures absolute temperature, for example a ds18b20, measure the cold junction temperature and subtract it from the combined temperature of the two junctions. This process is known as reference junction compensation (cold junction compensation).
But the same cold junction compensation can be done more simply: subtract the thermoelectric voltage of the cold junction from the total thermoelectric voltage, then calculate the temperature.
The question is how to obtain the thermoelectric voltage of the cold junction.
This can be done with a low-power silicon diode, whose p-n junction voltage drop depends on absolute temperature and is about 2mV/degree. Then, by amplifying the diode voltage drop by the required factor (depending on the thermocouple type), we get the thermoelectric voltage of the cold junction. We subtract it from the total voltage, thereby obtaining the thermoelectric voltage of the hot junction, and then its temperature.
That's it for the theory; let's move on to practice. For this, I bought a shield based on the MAX6675, with built-in cold junction compensation for a K-type thermocouple and an SPI interface.

The output is 12 bits with a resolution of 0.25 degrees, i.e. it can measure temperatures from 0 to 1024 degrees.
The read procedure is very simple: after the CS pin is pulled low, you can apply the clock signal and read the temperature on the falling edge; it couldn't be simpler.
The frame format looks like this.

Here too everything is simple: bits 3 to 14 hold the temperature value. If bit two is zero, the thermocouple is connected; otherwise it is disconnected. I don't understand what bit one is for.
As promised, the code for the stm32f103vet6.
Show / Hide text
#include "stm32f10x.h"
#include <stdio.h>//for SWO
#define CS_LOW GPIOB->BSRR = GPIO_BSRR_BR10;
#define CS_HIGH GPIOB->BSRR = GPIO_BSRR_BS10;
//for SWO
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
return(ch);
}
#define DWT_CYCCNT *(volatile unsigned long *)0xE0001004
#define DWT_CONTROL *(volatile unsigned long *)0xE0001000
#define SCB_DEMCR *(volatile unsigned long *)0xE000EDFC
void delay_ms(uint32_t ms)
{
int32_t ms_count_tick = ms * (SystemCoreClock/1000);
//enable the cycle counter
SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
//reset the counter register
DWT_CYCCNT = 0;
//start the counter
DWT_CONTROL|= DWT_CTRL_CYCCNTENA_Msk;
while(DWT_CYCCNT < ms_count_tick);
//stop the counter
DWT_CONTROL &= ~DWT_CTRL_CYCCNTENA_Msk;
}
void Spi_Init(void)
{
//enable clocking of port B and alternate functions
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
//pins 13(SCK) and 15(MOSI) - alternate function push pull, pin 14(MISO) - Input floating, pin 10(CS) - output, push-pull
GPIOB->CRH &= ~(GPIO_CRH_CNF13_0 | GPIO_CRH_CNF15_0 | GPIO_CRH_CNF10_0);
GPIOB->CRH |= GPIO_CRH_CNF13_1 | GPIO_CRH_CNF15_1;
GPIOB->CRH |= GPIO_CRH_MODE10_0 | GPIO_CRH_MODE13_1 | GPIO_CRH_MODE15_1;
//enable SPI2 clocking
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
SPI2->CR1 |= SPI_CR1_BR; //Baud rate = Fpclk/256
SPI2->CR1 &= ~SPI_CR1_CPOL; //Polarity cls signal CPOL = 0;
SPI2->CR1 |= SPI_CR1_CPHA; //Sampled on the falling edge
SPI2->CR1 |= SPI_CR1_DFF; //16 bit data
SPI2->CR1 &= ~SPI_CR1_LSBFIRST; //MSB will be first
SPI2->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI; //Software slave management & Internal slave select
SPI2->CR1 |= SPI_CR1_MSTR; //Mode Master
SPI2->CR1 |= SPI_CR1_SPE; //Enable SPI2
}
uint16_t Spi_Write_Data(uint16_t data)
{
//wait until the Tx buffer is empty
while(!(SPI2->SR & SPI_SR_TXE));
//activate Chip Select
CS_LOW
//send data
SPI2->DR = data;
//wait for the response
while(!(SPI2->SR & SPI_SR_RXNE));
//read the received data
data = SPI2->DR;
//deactivate Chip Select
CS_HIGH
//return what we read
return data;
}
int main (void)
{
float data;
Spi_Init();
while(1)
{
data = (Spi_Write_Data(0)>>3)*0.25;
printf("temp = %f \\r", data); //for SWO
delay_ms(1000);
}
}
To display the temperature in a separate window, as in the screenshot above, you need to enable SWD debugging using the SWO pin. You can read how to do this here.
I bought the shield here.


