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);
}
@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