Skip to content

Instantly share code, notes, and snippets.

@xxlukas42
Last active April 21, 2024 04:16
Show Gist options
  • Save xxlukas42/7e7e18604f61529b8398f7fcc5785251 to your computer and use it in GitHub Desktop.
Save xxlukas42/7e7e18604f61529b8398f7fcc5785251 to your computer and use it in GitHub Desktop.
Arduino source code for ESP32 internal temperature sensor and hall sensor
void setup() {
Serial.begin(115200);
}
void loop() {
int measurement = 0;
measurement = hallRead();
Serial.print("Hall sensor measurement: ");
Serial.println(measurement);
delay(1000);
}
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print("Temperature: ");
// Convert raw temperature in F to Celsius degrees
Serial.print((temprature_sens_read() - 32) / 1.8);
Serial.println(" C");
delay(5000);
}
@maaami98
Copy link

but only 53.33 C writes

@LDavidFr
Copy link

but only 53.33 C writes

Same here. the function returns 128, sensor not present ?!

@ankitsynx
Copy link

yes, this value is returned when the sensor is absent. Heard it's discontinued on newer ESP32s.

@DhruvaG2000
Copy link

ESP32 DevKit ESP32-WROOM Definitely does not have an on-board temperature sensor. Purchase Date: Jan 2021

@toldcodger
Copy link

Hi, I had an Adafruit HUZZAH32 – ESP32 Feather Board given as a present in August 2020.
I’m new to this device and fairly new to the Arduino IDE.
I’ve been trying to test the internal temperature sensor. I found thus script and I get the same 53.33 C as you but I’m not seeing the “128, function not present’. Have I missed something or do I need to do more ?
Regards

@ShawMerlin
Copy link

I also get 53C, but then I took a heat gun and hit the top of the ESP32 and got it over 70C. I believe 53C is the working temperature of the ESP32 Module. This is basically a dual core CPU without a fan so I think 53C is pretty accurate for how hot it runs.

Max temperature is 125C. But for all those that think it's not working, put a hair dryer or heat gun on it. I think it works perfectly.

@gotsila
Copy link

gotsila commented Jun 20, 2021

with this code it doesn't work. with tasmota webserver 9.5.0 it shows the temp. selftested on my esp32cam. and wondering why?

@hansvz-lg
Copy link

Hi, I had an Adafruit HUZZAH32 – ESP32 Feather Board given as a present in August 2020.
I’m new to this device and fairly new to the Arduino IDE.
I’ve been trying to test the internal temperature sensor. I found thus script and I get the same 53.33 C as you but I’m not seeing the “128, function not present’. Have I missed something or do I need to do more ?
Regards

128 converted to C = 53.33
53.33 * 1.8 + 32 = 128

@kickass42zer0
Copy link

kickass42zer0 commented Feb 26, 2022

For anyone who is trying to use this sensor even though it's unreliable and inaccurate.
This is the work-around implementation I coded.

It seems to give fairly accurate readings based on feeling the cpu temp by hand.
No, I did not have a laser thermo laying around, so the accuracy is pure speculative on my part.

Maybe someone else can test it and give a good report on how accurate this implementation is.
Also maybe give some insights on why this could work, or not work.
github-example-ESP32 CPU Temp

// Onboard Sensors
#ifdef __cplusplus
extern "C" { // CPU Temp Sensor
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
float CPU_temp_array[50];
float real_CPU_temp = 0;
void CPU_temp_calc(void * pvParameters) {
// Populate the array first, while Initializing
for (int i = 0; i < 50; i=i) {
uint8_t t = temprature_sens_read();
while ( t == 128 ) { t = temprature_sens_read(); }
CPU_temp_array[i] = (t - 32)*5/9;
i++;
}
// Start endless loop here
for (;;) {
//move each array value up 1 to prepare for the new insert
for (int i = 0; i < 49; i++) {
CPU_temp_array[i+1] = CPU_temp_array[i];
}
uint8_t t = temprature_sens_read();
while ( t == 128 ) { t = temprature_sens_read(); }
CPU_temp_array[0] = (t-32)*5/9;
float total = 0;
for (int i = 0; i < 50; i++) {
total += CPU_temp_array[i];
}
real_CPU_temp = total/50;
vTaskDelay(500);
}
}
TaskHandle_t WiFi_Man_Handle;
void setup() {
xTaskCreatePinnedToCore(
CPU_temp_calc
, "Calc Real CPU temp" // A name just for humans
, 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 0 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, &calcCPUTemp_Handle
, 1);
//Rest of setup code here
}

PS: You know for Github they REALLY HAVE A TERRIBLE comment system. How in the hell am I supposed to comment good looking code?
Is there something I'm missing. The code section is just a "`"

Guthub is forcing me to upload a photo now ....pffff

Also, this means the max measureable CPU temp is 53.333 degrees, but since mine is running at around 33 (and spikes to 50-51), i'm not too concearned about that. Another interesting thing is the noticeable temp change when WiFi disconnects compared to when its connected.

Notice how I use unsigned uint8_t on the temprature_sens_read() but still get a max value of 128, so the hardware reports in int8_t (pretty stupid if u ask me). When is a CPU ever gonna reach less then -17.78 degrees celcius????? Are you nitrogen cooling your ESP CPU? Oh year, gotta overclock that MoFo.

EDIT Changed back to signed int and added extra conditional for check if the reported value is less then 0, cuz it looks like it reports -127 for sensor not available too

@Resner
Copy link

Resner commented Mar 2, 2022

@kickass42zer0 Thanks for your implementation! To format code in Markdown, you need to enclose your code in between 3 backticks.

```
like so
second line etc.
```

@An-Toha
Copy link

An-Toha commented Aug 31, 2022

Hello, I have change the Arduino sketch for internal temperature sensor ESP32 CPU little bit.
https://gist.github.com/An-Toha/5a53f4fbe571c1b9cf02b362a83e93df
(Don't know how properly put code here)
The main ideas are:
to ignore 53.3 (128 before converting to Celsius);
WiFi must be connected, if else you'll get 53.3 (128) only.
Tested in AI Thinker ESP32 Cam module. Can be add to standard camera example in Arduino.
While ESP32 Cam module was idle - the temperature was around 47C. One value per 10-15 sec while idle.
While camera video stream was on - the temperature was rising to around 57
C.
See temperature values in serial monitor at 115200.
Best regards, Anton S
ESP32_selftemp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment