Home / Tutorials / ESP32 Tutorial / Using the ESP32 Internal Temperature Sensor with Arduino
pcbway
ESP32 C3 Mini

Using the ESP32 Internal Temperature Sensor with Arduino

The ESP32, a popular microcontroller, features an internal temperature sensor. While primarily intended for monitoring the chip’s temperature rather than ambient conditions, it can still be useful in certain applications, such as monitoring device performance or debugging. This tutorial will walk you through how to use the ESP32 internal temperature sensor with Arduino, providing two methods to access its readings.

Availability of the Internal Temperature Sensor

Not all ESP32 variants include an internal temperature sensor. Here is an overview of the variants that do:

  • ESP32-S2: Features an internal temperature sensor for chip temperature monitoring.
  • ESP32-S3: Includes a built-in temperature sensor for internal readings.
  • ESP32-C3: Equipped with an internal temperature sensor.
  • ESP32-C6: Features an internal temperature sensor.
  • ESP32-H2: Also includes an internal temperature sensor.

For ESP32 variants not listed above, the temperature sensor feature might not be available, and attempts to use it may result in default or erroneous readings.

Method 1: Direct Reading Using the temprature_sens_read() Function

This method directly reads the internal temperature sensor using the temprature_sens_read() function. The raw value is processed to obtain the temperature in Celsius. Here’s the code:

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "your SSID";
const char* password = "your pass";

// Internal temperature sensor function declaration
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif

uint32_t n; // Number of measurements
uint32_t timer; // Time from start
uint32_t tm1; // Start moment

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());

  // Table headers in the serial monitor
  Serial.print("Number\t");
  Serial.print("time\t");
  Serial.print("temp,C*\n");

  n = 0; // Reset measurement number
  tm1 = millis(); // Set start moment
}

void loop() {
  if ((temprature_sens_read()) != 128) { // Ignore default 53.33°C reading
    n++;
    Serial.print(n);
    Serial.print("\t");

    timer = millis() - tm1; // Measurement time
    Serial.print(timer / 60000.0);
    Serial.print("\t");

    Serial.print((temprature_sens_read() - 32) / 1.8); // Convert to Celsius
    Serial.print("\n");

    delay(1000); // Read temperature every second
  }

  delay(1000);
}

Explanation

  • The temprature_sens_read() function provides the raw temperature value.
  • The conversion formula (raw – 32) / 1.8  converts it to Celsius.
  • Wi-Fi activity influences the readings, as the chip’s power consumption increases.

Method 2: Using temperatureRead()

The Arduino core provides a simpler method to access the internal temperature sensor with the temperatureRead() function. Here’s the code:

void setup() {
  Serial.begin(115200);
}

void loop() {
  float temp_celsius = temperatureRead();

  Serial.print("Temp onBoard ");
  Serial.print(temp_celsius);
  Serial.println("°C");

  delay(1000);
}

Explanation

  • The temperatureRead() function abstracts the sensor’s hardware interactions, making it easier to use.
  • This method is ideal for quick implementations where detailed control over the sensor is unnecessary.

Important Considerations

  • Accuracy: The internal temperature sensor is designed for monitoring the ESP32’s chip temperature and may not provide accurate ambient readings.
  • Power Influence: Activities like Wi-Fi usage can affect readings due to increased power consumption and heat generation.
  • External Sensors: For precise ambient temperature measurements, consider using external sensors like the DS18B20 or DHT22.

Conclusion

The ESP32’s internal temperature sensor is a handy feature for monitoring chip performance. Depending on your application, you can choose between the detailed control of temprature_sens_read() or the simplicity of temperatureRead(). Remember to verify whether your ESP32 variant supports this feature before implementation. Happy coding!

 

Check Also

ESP32 C3 Mini

ESP32-C3: IoT Microcontroller Overview

The ESP32-C3 is a standout microcontroller in the ESP32 series, developed by Espressif Systems, designed …