arduino-esp32
arduino-esp32 copied to clipboard
Chip Temperature with ESP.getChipTemp()
Related area
System/Chip
Hardware specification
ESP32-S3
Is your feature request related to a problem?
no
Describe the solution you'd like
i propose a new getter function to the ESP class to get the chip internal temperature.
here is an example function to read the chip internal temperature sensor:
#include "soc/adc_periph.h"
void init_temperature_sensor()
{
*((uint32_t*) SENS_SAR_TSENS_CTRL_REG) |= SENS_TSENS_POWER_UP_FORCE | SENS_TSENS_POWER_UP;
}
float temprature_sens_read()
{
*((uint32_t*) SENS_SAR_TSENS_CTRL_REG) |= SENS_TSENS_DUMP_OUT;
ulong time0 = millis();
while(true)
{
if(millis() - time0 > 250)
return -1;
bool ready = *((uint32_t*) SENS_SAR_TSENS_CTRL_REG) & SENS_TSENS_READY;
if(ready)
{
uint8_t value = *((uint32_t*) SENS_SAR_TSENS_CTRL_REG) & SENS_TSENS_OUT;
float temp = 0.4386 * value - 20.52;
*((uint32_t*) SENS_SAR_TSENS_CTRL_REG) &= ~SENS_TSENS_DUMP_OUT;
return temp;
}
}
}
Improvements might be to check the offset value as described in TRM on page 1164
Describe alternatives you've considered
to my knowledge the is no hal function implemented to read the internal temperature sensor from the main processor
I have checked existing list of Feature requests and the Contribution Guide
- [X] I confirm I have checked existing list of Feature requests and Contribution Guide.
Thanks @savejeff - We will analyze and consider your proposal! Feel free to add a PR if you wish so.
@P-R-O-C-H-Y, would you like to evaluate it?
Thanks @savejeff - We will analyze and consider your proposal! Feel free to add a PR if you wish so.
Thx ;] Im not familiar enough with the code structure to implement this myself good enough. I investigated how to read the internal chip temp and only found "this is not implemented yet" so i thought i put out an example implementation.
There is improvement potential with the timeout. I have not checked what the real "time to ready" is and if it is a fixed time that can just be waited for to avoid deadlock without extra variables and code.
Hi @savejeff,
you can take a look into file esp32-hal-mics.c where the function temperatureRead() is. Maybe what we can improve, is an option to be able to set the range (offset) for the chip temperature reading. Now the default is used an d no option to change that.
default used: offset = 0, measure range:-10℃ ~ 80℃, error < 1℃
Where have you read "this is not implemented yet" please? :)
Hi!
I did some web research before implementing it and found no information. some threads I read suggested it was not yet implemented.
when trying to use the temperatureRead function the compilation failed giving me this error:
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio/build/BOARDS_ESP32_S3/libFrameworkArduino.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x0): undefined reference to `temp_sensor_set_config'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio/build/BOARDS_ESP32_S3/libFrameworkArduino.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x4): undefined reference to `temp_sensor_start'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio/build/BOARDS_ESP32_S3/libFrameworkArduino.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x8): undefined reference to `temp_sensor_read_celsius'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio/build/BOARDS_ESP32_S3/libFrameworkArduino.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0xc): undefined reference to `temp_sensor_stop'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: .pio/build/BOARDS_ESP32_S3/libFrameworkArduino.a(esp32-hal-misc.c.o): in function `temperatureRead':
C:/Users/Save_/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-misc.c:71: undefined reference to `temp_sensor_set_config'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:/Users/Save_/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-misc.c:72: undefined reference to `temp_sensor_start'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:/Users/Save_/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-misc.c:73: undefined reference to `temp_sensor_read_celsius'
c:/users/save_/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:/Users/Save_/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-misc.c:74: undefined reference to `temp_sensor_stop'
the code likely does not work out of the box for arduino
My original proposal was about adding a new function to the ESP class to make it easier to access the function as well as make code more clean. The code i proposed was only an example to possibly help with an implementation.
@savejeff What version of ESP32-Arduino core do you use?
I originally test this on PlatformIO with the latest core version (equivalent to Arduino 2.0.4)
But i tested this also with Arduino IDE with the latest core version 2.0.4 and I'm getting the same linker error:
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: core\core.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x4): undefined reference to `temp_sensor_set_config'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: core\core.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x8): undefined reference to `temp_sensor_start'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: core\core.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0xc): undefined reference to `temp_sensor_read_celsius'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: core\core.a(esp32-hal-misc.c.o):(.literal.temperatureRead+0x10): undefined reference to `temp_sensor_stop'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: core\core.a(esp32-hal-misc.c.o): in function `temperatureRead':
C:\Users\Save_\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\cores\esp32/esp32-hal-misc.c:71: undefined reference to `temp_sensor_set_config'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:\Users\Save_\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\cores\esp32/esp32-hal-misc.c:72: undefined reference to `temp_sensor_start'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:\Users\Save_\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\cores\esp32/esp32-hal-misc.c:73: undefined reference to `temp_sensor_read_celsius'
c:/users/save_/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld.exe: C:\Users\Save_\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\cores\esp32/esp32-hal-misc.c:74: undefined reference to `temp_sensor_stop'
collect2.exe: error: ld returned 1 exit status
exit status 1
here the scetch:
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.printf("Temp: %f\n", temperatureRead());
}
Even when temperatureRead works, i don't think that is a good solution for Arduino users, as the function name is very un-specific and does not suggest that the cpu/chip temperature is read. A new member function to the ESP class like i suggested in the title of the issue would be much better.
Yes you are right, I see same compilation issue on 2.0.4. However on master branch, its already fixed. So in 2.0.5 it will be available for ESP32S3. But I am getting some weird temperatures read, I will test it more further and find the issue. If you can, please retest it in Arduino IDE with ESP32 core master branch :)
What board are you using? temperature readings seem to not work with my devkit boards but with the wroom module i have on a customer layout.
about the master branch, i don't use the Arduino IDE (only to check before i start an issue on github). Thus i also don't know how to switch the arduino branch and also would like to avoid breaking the Arduino IDE. as far as i know there is no way to switch to a newer version in PlatformIO before a new release
It needs this IDF commit or later for building the Arduino libs to get correct S3 temp readings
Imho, this can be closed. Internal temp sensor is measuring the Chip temperature. More code will just add bit changed temperatur "numbers". For chip temperature it is precise enough as it is now! If the environment temperature should be measured there is ALWAYS an external sensor needed.
jup if the code is includede in the latest release. Would be cool to have the ESP.getCPUTemperature() member function, but its okay this way