8000 esp32/modesp32: Add mcu_temperature() function for C3/S2/S3 devices. · micropython/micropython@63c30a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 63c30a2

Browse files
ricksorensendpgeorge
authored andcommitted
esp32/modesp32: Add mcu_temperature() function for C3/S2/S3 devices.
For ESP32C3/S2/S3 IDFv5 exposes new internal temperature API which is different to the base ESP32, IDFv4. Thanks to @robert-hh for cleaner code and testing sensor capability in these devices. See discussion #10443. Signed-off-by: Rick Sorensen <rick.sorensen@gmail.com>
1 parent 595f861 commit 63c30a2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docs/esp32/quickref.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Note that the temperature sensor in the ESP32 will typically read higher than
6767
ambient due to the IC getting warm while it runs. This effect can be minimised
6868
by reading the temperature sensor immediately after waking up from sleep.
6969

70+
ESP32C3, ESP32S2, and ESP32S3 also have an internal temperature sensor available.
71+
It is implemented a bit differently to the ESP32 and returns the temperature in
72+
Celsius::
73+
74+
esp32.mcu_temperature() # read the internal temperature of the MCU, in Celsius
75+
7076
Networking
7177
----------
7278

ports/esp32/modesp32.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ static mp_obj_t esp32_raw_temperature(void) {
168168
}
169169
static MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature);
170170

171+
#else
172+
173+
// IDF 5 exposes new internal temperature interface, and the ESP32C3/S2/S3
174+
// now have calibrated temperature settings in 5 discrete ranges.
175+
#include "driver/temperature_sensor.h"
176+
177+
static mp_obj_t esp32_mcu_temperature(void) {
178+
static temperature_sensor_handle_t temp_sensor = NULL;
179+
float tvalue;
180+
if (temp_sensor == NULL) {
181+
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
182+
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
183+
}
184+
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
185+
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tvalue));
186+
ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor));
187+
return mp_obj_new_int((int)(tvalue + 0.5));
188+
}
189+
static MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature);
190+
171191
#endif
172192

173193
static mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) {
@@ -202,6 +222,8 @@ static const mp_rom_map_elem_t esp32_module_globals_table[] = {
202222
{ MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) },
203223
#if CONFIG_IDF_TARGET_ESP32
204224
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
225+
#else
226+
{ MP_ROM_QSTR(MP_QSTR_mcu_temperature), MP_ROM_PTR(&esp32_mcu_temperature_obj) },
205227
#endif
206228
{ MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) },
207229

0 commit comments

Comments
 (0)
0