diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index efdd6c1be289c..a1a87c07bd1e0 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -44,6 +44,23 @@ Functions Read the raw value of the internal temperature sensor, returning an integer. + Only available on ESP32. + +.. function:: temperature() + + Read the value of the internal temperature sensor in celsius, returning a float. + + Available on ESP32-S2, S3 and C3. + +.. Note:: + The temperature sensor is designed primarily to measure the temperature changes inside + the chip. The temperature value depends on factors like microcontroller clock frequency + or I/O load. Generally, the chip’s internal temperature might be higher than the ambient temperature. + +.. function:: hall_sensor() + + Read the raw value of the internal Hall sensor, returning an integer. + .. function:: idf_heap_info(capabilities) Returns information about the ESP-IDF heap memory regions. One of them contains diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index ef3ad0b76d459..56b6d2ddb020f 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -170,6 +170,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperatur #endif +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 + +#include "driver/temperature_sensor.h" + +STATIC mp_obj_t esp32_temperature(void) { + + temperature_sensor_handle_t temp_sensor = NULL; + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(0, 50); + temperature_sensor_install(&temp_sensor_config, &temp_sensor); + temperature_sensor_enable(temp_sensor); + float tsens_out; + temperature_sensor_get_celsius(temp_sensor, &tsens_out); + temperature_sensor_disable(temp_sensor); + temperature_sensor_uninstall(temp_sensor); + + return mp_obj_new_float(tsens_out); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_temperature_obj, esp32_temperature); +#endif + STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { mp_int_t cap = mp_obj_get_int(cap_in); multi_heap_info_t info; @@ -203,6 +223,9 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, #endif + #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 + { MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&esp32_temperature_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, { MP_ROM_QSTR(MP_QSTR_NVS), MP_ROM_PTR(&esp32_nvs_type) },
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: