Temperature Sensor

Overview

The ESP32-S2 has a built-in temperature sensor used to measure the chip’s internal temperature, and hard to measure the environmental temperature accurately. Being built-in means that the temperature sensor should work on any ESP32-S2 regardless of what board the chip is embedded in. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.

The conversion relationship is the first columns of the table below. Among them, offset = 0 is the main measurement option, and other values are extended measurement options.

offset

measure range(Celsius)

measure error(Celsius)

-2

50 ~ 125

< 3

-1

20 ~ 100

< 2

0

-10 ~ 80

< 1

1

-30 ~ 50

< 2

2

-40 ~ 20

< 3

Driver Usage

  1. Initialize the temperature sensor by calling the function temp_sensor_set_config() and pass to it a temp_sensor_config_t structure. The temp_sensor_config_t structure should contain all the required parameters. See the example below.

temp_sensor_config_t temp_sensor = {
    .dac_offset = TSENS_DAC_L2,
    .clk_div = 6,
};
temp_sensor_set_config(temp_sensor);
  1. Start the temp_sensor by calling :cpp:func:’temp_sensor_start’. The temperature sensor will now measure the temperature.

  2. To get the current temperature, take the example below as a reference, the value you get is in Celsius.

float tsens_out;
temp_sensor_read_celsius(&tsens_out);
  1. To stop the temperature sensor, please call :cpp:func:’temp_sensor_stop’.

备注

If you want dynamic reconfiguration, you need to stop the sensor first (temp_sensor_stop), set the new configuration (temp_sensor_set_config), then start the sensor again (temp_sensor_start).

Application Example

Temperature sensor reading example: peripherals/temp_sensor.

API Reference - Normal Temp Sensor

Functions

esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens)

Set parameter of temperature sensor.

参数

tsens

返回

  • ESP_OK Success

esp_err_t temp_sensor_get_config(temp_sensor_config_t *tsens)

Get parameter of temperature sensor.

参数

tsens

返回

  • ESP_OK Success

esp_err_t temp_sensor_start(void)

Start temperature sensor measure.

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG

esp_err_t temp_sensor_stop(void)

Stop temperature sensor measure.

返回

  • ESP_OK Success

esp_err_t temp_sensor_read_raw(uint32_t *tsens_out)

Read temperature sensor raw data.

参数

tsens_out – Pointer to raw data, Range: 0 ~ 255

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG tsens_out is NULL

  • ESP_ERR_INVALID_STATE temperature sensor dont start

esp_err_t temp_sensor_read_celsius(float *celsius)

Read temperature sensor data that is converted to degrees Celsius.

备注

Should not be called from interrupt.

参数

celsius – The measure output value.

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG ARG is NULL.

  • ESP_ERR_INVALID_STATE The ambient temperature is out of range.

Structures

struct temp_sensor_config_t

Configuration for temperature sensor reading.

Public Members

temp_sensor_dac_offset_t dac_offset

The temperature measurement range is configured with a built-in temperature offset DAC.

uint8_t clk_div

Default: 6

Macros

TSENS_CONFIG_DEFAULT()

temperature sensor default setting.

Enumerations

enum temp_sensor_dac_offset_t

temperature sensor range option.

Values:

enumerator TSENS_DAC_L0

offset = -2, measure range: 50℃ ~ 125℃, error < 3℃.

enumerator TSENS_DAC_L1

offset = -1, measure range: 20℃ ~ 100℃, error < 2℃.

enumerator TSENS_DAC_L2

offset = 0, measure range:-10℃ ~ 80℃, error < 1℃.

enumerator TSENS_DAC_L3

offset = 1, measure range:-30℃ ~ 50℃, error < 2℃.

enumerator TSENS_DAC_L4

offset = 2, measure range:-40℃ ~ 20℃, error < 3℃.

enumerator TSENS_DAC_MAX
enumerator TSENS_DAC_DEFAULT