ESP32-S2 built-in temperature sensor

Hello, does anyone know how to read ESP32-S2 built-in temperature sensor?

There is an official documentation with and example available at: Temperature Sensor - ESP32-S2 - — ESP-IDF Programming Guide v5.2.1 documentation

but Arduino does not find #include file driver/temperature_sensor.h

Hm, it looks like this feature hasn't been implemented yet. It is planned to be added in IDF v5.0: ESP32S3 Temperature Sensor undefined reference (IDFGH-6427) · Issue #8086 · espressif/esp-idf · GitHub

Now the IDF 5.x is out so the code compiles, but calling temperature_sensor_install only blocks the controller. Did anyone manage to get it working already?

This works on an ESP32-C3.
Not sure if it's useful.
It hovers around 50-60C, which is the internal chip temp.
Goes down to believable temps when WiFi is turned off.
Leo..

#include "driver/temp_sensor.h"

void initTempSensor(){
    temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
    temp_sensor.dac_offset = TSENS_DAC_L2;  // TSENS_DAC_L2 is default; L4(-40°C ~ 20°C), L2(-10°C ~ 80°C), L1(20°C ~ 100°C), L0(50°C ~ 125°C)
    temp_sensor_set_config(temp_sensor);
    temp_sensor_start();
}

void setup() {
  Serial.begin(115200);
  initTempSensor();
}

void loop() {
  Serial.print("Temperature: ");
  float result = 0;
  temp_sensor_read_celsius(&result);
  Serial.print(result);
  Serial.println(" °C");
  delay(5000);
}

Thank you. Unfortunately it doesn't work for me (ESP32 S2). The code compiles but stops execution at initialization.

Which ESP core version are you using.
I think I was using 2.0.17
Leo..

It looks that the problems I had were somehow related to different tasks I was using to run the code. I still haven't found out what is wrong with the tasks, but if I'm using only setup () and loop () the following code does work.


#include "driver/temperature_sensor.h"


temperature_sensor_handle_t temp_handle = NULL;
temperature_sensor_config_t temp_sensor = {
    .range_min = 20,
    .range_max = 50
};


void setup () {

    Serial.begin (115200);
    delay (3000);

    ESP_ERROR_CHECK (temperature_sensor_install (&temp_sensor, &temp_handle));

}


void loop () {

    // Enable temperature sensor
    ESP_ERROR_CHECK (temperature_sensor_enable(temp_handle));
    // Get converted sensor data
    float tsens_out;
    ESP_ERROR_CHECK (temperature_sensor_get_celsius (temp_handle, &tsens_out));
    Serial.printf ("Temperature in %f °C\n", tsens_out);
    // Disable the temperature sensor if it is not needed and save the power
    ESP_ERROR_CHECK (temperature_sensor_disable (temp_handle));

    delay (3000);
}

Well, having different tasks doesn't cause problems. I must have had some other interference in my original code which I still haven't found out. I'm marking this thread as solved.


#include "driver/temperature_sensor.h"


temperature_sensor_handle_t temp_handle = NULL;
temperature_sensor_config_t temp_sensor = {
    .range_min = -10,
    .range_max = 80
};


void setup () {

    Serial.begin (115200);
    delay (3000);

    ESP_ERROR_CHECK (temperature_sensor_install (&temp_sensor, &temp_handle));

    xTaskCreate ([] (void * pvParameters) {

        while (true) {

            // Enable temperature sensor
            ESP_ERROR_CHECK (temperature_sensor_enable(temp_handle));
            // Get converted sensor data
            float tsens_out;
            ESP_ERROR_CHECK (temperature_sensor_get_celsius (temp_handle, &tsens_out));
            Serial.printf ("Temperature in %f °C\n", tsens_out);
            // Disable the temperature sensor if it is not needed and save the power
            ESP_ERROR_CHECK (temperature_sensor_disable (temp_handle));

            delay (3000);

        }

    }, "taskLoop", 4 * 1024, NULL, 1, NULL);

}


void loop () {

}

I was surprised about how hot the chip gets too. Once I was able to measure the temperature I made a short test. Lowering CPU speed somewhat helps:

240 MHz ... 49 ℃
160 MHz ... 46 ℃
80 MHz ... 44 ℃

But what helped the most was extending WiFi beacon intervals by calling esp_wifi_set_ps (WIFI_PS_MAX_MODEM).

The temperature went down to 28 ℃.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.