Skip to content

Chip Temperature with ESP.getChipTemp() #7185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
savejeff opened this issue Aug 26, 2022 · 12 comments
Closed
1 task done

Chip Temperature with ESP.getChipTemp() #7185

savejeff opened this issue Aug 26, 2022 · 12 comments
Assignees
Labels
Status: To be implemented Selected for Development Type: Feature request Feature request for Arduino ESP32
Milestone

Comments

@savejeff
Copy link

savejeff commented Aug 26, 2022

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

  • I confirm I have checked existing list of Feature requests and Contribution Guide.
@savejeff savejeff added the Type: Feature request Feature request for Arduino ESP32 label Aug 26, 2022
@SuGlider
Copy link
Collaborator

Thanks @savejeff - We will analyze and consider your proposal!
Feel free to add a PR if you wish so.

@SuGlider
Copy link
Collaborator

@P-R-O-C-H-Y, would you like to evaluate it?

@savejeff
Copy link
Author

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.

@P-R-O-C-H-Y
Copy link
Member

P-R-O-C-H-Y commented Sep 2, 2022

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? :)

@savejeff
Copy link
Author

savejeff commented Sep 6, 2022

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.

@P-R-O-C-H-Y
Copy link
Member

@savejeff What version of ESP32-Arduino core do you use?

@savejeff
Copy link
Author

savejeff commented Sep 7, 2022

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.

@P-R-O-C-H-Y
Copy link
Member

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 :)

@savejeff
Copy link
Author

savejeff commented Sep 7, 2022

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

@Jason2866
Copy link
Collaborator

It needs this IDF commit or later for building the Arduino libs to get correct S3 temp readings

@VojtechBartoska VojtechBartoska added the Status: To be implemented Selected for Development label Sep 21, 2022
@P-R-O-C-H-Y P-R-O-C-H-Y moved this from Todo to In Progress in Arduino ESP32 Core Project Roadmap Oct 17, 2022
@P-R-O-C-H-Y P-R-O-C-H-Y moved this from In Progress to Todo in Arduino ESP32 Core Project Roadmap Oct 17, 2022
@VojtechBartoska VojtechBartoska moved this from Todo to In Review in Arduino ESP32 Core Project Roadmap Nov 16, 2022
@Jason2866
Copy link
Collaborator

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.

@savejeff
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: To be implemented Selected for Development Type: Feature request Feature request for Arduino ESP32
Projects
Development

No branches or pull requests

5 participants