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

Skip to content

Commit 798fb80

Browse files
committed
esp32/modesp32.c: Add mcu_temperature() function for C3/S2/S3 devices.
For ESP32C3/S2/S3 IDFv5 exposes new internal temperature API which is different than 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 d014c82 commit 798fb80

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

docs/esp32/quickref.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ working with this board it may be useful to get an overview of the microcontroll
1818
general.rst
1919
tutorial/index.rst
2020

21+
Note that there are several varieties of ESP32- ESP32, ESP32C3, ESP32S2, ESP32S3 supported by MicroPython,
22+
with some differences in functionality between them.
23+
2124
Installing MicroPython
2225
----------------------
2326

@@ -58,12 +61,19 @@ The :mod:`esp32` module::
5861
import esp32
5962

6063
esp32.raw_temperature() # read the internal temperature of the MCU, in Fahrenheit
61-
esp32.ULP() # access to the Ultra-Low-Power Co-processor
64+
esp32.ULP() # access to the Ultra-Low-Power Co-processor, not on ESP32C3
6265

6366
Note that the temperature sensor in the ESP32 will typically read higher than
6467
ambient due to the IC getting warm while it runs. This effect can be minimised
6568
by reading the temperature sensor immediately after waking up from sleep.
6669

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

ports/esp32/modesp32.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ 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+
// IDF 5 exposes new internal temperature interface, and the ESP32C3/S2/S3
173+
// now have calibrated temperature settings in 5 discrete ranges.
174+
#include "driver/temperature_sensor.h"
175+
176+
STATIC mp_obj_t esp32_mcu_temperature(void) {
177+
static temperature_sensor_handle_t temp_sensor = NULL;
178+
float tvalue;
179+
if (temp_sensor == NULL) {
180+
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
181+
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
182+
}
183+
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
184+
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tvalue));
185+
ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor));
186+
return mp_obj_new_int((int)(tvalue + 0.5));
187+
}
188+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature);
189+
171190
#endif
172191

173192
STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) {
@@ -202,6 +221,8 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
202221
{ MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) },
203222
#if CONFIG_IDF_TARGET_ESP32
204223
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
224+
#else
225+
{ MP_ROM_QSTR(MP_QSTR_mcu_temperature), MP_ROM_PTR(&esp32_mcu_temperature_obj) },
205226
#endif
206227
{ MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) },
207228

0 commit comments

Comments
 (0)
0