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..
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 () {
}