max6675: MAX6675 thermocouple-to-digital converter
Features
ESP-IDF driver for the MAX6675 cold-junction compensated thermocouple-to-digital converter with a type K thermocouple.
12-bit temperature, 0.25 °C per LSB, 0 to +1023.75 °C
detects an open thermocouple and refuses the frame instead of returning a meaningless number
validates the frame and the arguments
uses the SPI master driver, so the chip can share the bus with other devices (CS must be exclusive)
Wiring
MAX6675 |
ESP32 |
Notes |
|---|---|---|
1 GND |
GND |
|
2 T- |
thermocouple - |
must be grounded for the open detector |
3 T+ |
thermocouple + |
|
4 VCC |
3.3 V |
100 nF to GND close to the chip |
5 SCK |
|
SPI clock |
6 CS |
|
chip select |
7 SO |
|
data out |
8 N.C. |
The chip is read-only for the master: there is no MOSI line, and the driver configures the bus without one.
The T- lead must be connected to ground as close to the GND pin as
possible, otherwise the open thermocouple detector does not work.
Interface
There are no registers. The master pulls CS low and clocks out 16 bits, MSB first:
Bit |
Meaning |
|---|---|
D15 |
dummy sign bit, always 0 |
D14-D3 |
the compensated temperature, 0.25 °C per LSB, 0 = 0 °C, 4095 = +1023.75 °C |
D2 |
1 when the thermocouple input is open |
D1 |
device ID, 0 for the MAX6675 |
D0 |
three-state, not used |
The chip also compensates the cold junction, but it does not report the cold junction temperature: only the compensated thermocouple temperature is available. The cold junction itself must stay between -20 and +85 °C.
Timing
A conversion takes 170 ms typical and 220 ms maximum. Pulling CS low stops the conversion that is in progress: the frame that follows belongs to the previous conversion, or to the interrupted one, which is why the datasheet asks to wait.
The driver does not wait: read a value, then wait at least
MAX6675_CONVERSION_TIME_MS before the next read. Reading at a higher rate
returns stale values, not garbage, but they are not the current temperature.
The clock may run at up to 4.3 MHz; 1 MHz is more than enough for a chip that produces one 16-bit frame per 220 ms.
Faults
When the thermocouple is open, the frame reports it in D2. The number in the
same frame is meaningless, so max6675_read_temperature() reports the flag
through its open parameter, returns ESP_ERR_INVALID_RESPONSE and does not
touch the temperature variable. The same
happens when the dummy sign bit is set, which usually means the wiring is
wrong or the chip is not there.
Usage
#include <max6675.h>
spi_bus_config_t bus_cfg = {
.miso_io_num = 19,
.mosi_io_num = -1,
.sclk_io_num = 18,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus_cfg, SPI_DMA_CH_AUTO));
max6675_t dev;
ESP_ERROR_CHECK(max6675_init_desc(&dev, SPI2_HOST, GPIO_NUM_5, 1000000));
while (true)
{
float temperature;
bool open;
esp_err_t err = max6675_read_temperature(&dev, &temperature, &open);
if (err == ESP_OK)
ESP_LOGI(TAG, "%.2f °C", temperature);
else if (open)
ESP_LOGE(TAG, "The thermocouple is open");
else
ESP_LOGE(TAG, "Could not read: %s", esp_err_to_name(err));
vTaskDelay(pdMS_TO_TICKS(1000));
}
API
esp_err_t max6675_init_desc(max6675_t *dev, spi_host_device_t host, gpio_num_t cs_pin, int clock_speed_hz);
esp_err_t max6675_free_desc(max6675_t *dev);
esp_err_t max6675_read_temperature(max6675_t *dev, float *temperature, bool *open);
max6675_init_desc() validates the host, the clock speed (up to
MAX6675_MAX_CLOCK_SPEED_HZ) and the CS pin, and adds the device to the
bus. max6675_free_desc() removes it and clears the handle, so the second
call returns ESP_ERR_INVALID_ARG.
Thread safety
The SPI driver serialises the transfers, so the reads may be called from
several tasks. max6675_free_desc() is not protected: do not remove the
device while another task may be reading it.
Example
See examples/default.
API reference
Defines
- MAX6675_MAX_CLOCK_SPEED_HZ
The highest clock frequency the chip supports, 4.3 MHz.
- MAX6675_CONVERSION_TIME_MS
The maximum conversion time, in milliseconds.
- MAX6675_RESOLUTION
The temperature of one LSB of the frame, in degrees Celsius.
Functions
- esp_err_t max6675_init_desc(max6675_t *dev, spi_host_device_t host, gpio_num_t cs_pin, int clock_speed_hz)
Initialize the device descriptor.
Connects the chip to the SPI bus. SCK and SO are configured by the SPI driver, the CS pin is driven by the driver as well, so it must not be shared with another device on the same bus.
The CS pin is set up by this function, so the SPI bus must exist already.
- Parameters:
dev – Device descriptor
host – SPI host
cs_pin – GPIO pin connected to CS
clock_speed_hz – SPI clock frequency, 1 MHz is enough and MAX6675_MAX_CLOCK_SPEED_HZ is the limit
- Returns:
ESP_OK on success
ESP_ERR_INVALID_ARG if any of the arguments is invalid
ESP_ERR_NO_MEM if the SPI device could not be allocated
another error code from the SPI driver
- esp_err_t max6675_free_desc(max6675_t *dev)
Free the device descriptor.
Removes the device from the SPI bus. The descriptor is not freed, it belongs to the caller.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OK on success
ESP_ERR_INVALID_ARG if the descriptor is NULL or the device is already removed
another error code from the SPI driver
- esp_err_t max6675_read_temperature(max6675_t *dev, float *temperature, bool *open)
Read the thermocouple temperature.
Reads one 16-bit frame and converts it. Pulling CS low interrupts the conversion that may be in progress, so call this function no more often than once per MAX6675_CONVERSION_TIME_MS.
The open thermocouple flag is always reported, so that the caller can tell a broken thermocouple from a broken bus. The temperature is not written when the flag is set: the number in that frame is meaningless.
- Parameters:
dev – Device descriptor
temperature – Pointer to store the temperature, in degrees Celsius
open – Pointer to store the open thermocouple flag
- Returns:
ESP_OK on success
ESP_ERR_INVALID_ARG if the descriptor is NULL or not initialized, or any of the pointers is NULL
ESP_ERR_INVALID_RESPONSE if the frame is not a valid one: the dummy sign bit is set or the thermocouple is open
another error code from the SPI driver