The ESPCPUTemp library provides a simple and unified interface to read the internal CPU temperature sensor on Espressif ESP32-based microcontrollers. It supports both legacy and new temperature sensor drivers, ensuring compatibility across various ESP32 variants, including ESP32, ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, and ESP32-H2.
- Unified Interface: Seamlessly supports both legacy (ESP32) and new (ESP32-S2/S3/C3/C6/H2) temperature sensor drivers.
- Automatic Chip Detection: Automatically detects the ESP32 chip model and selects the appropriate driver.
- Error Handling: Robust error handling with detailed Serial output for debugging.
- Lightweight: Minimal memory footprint, optimized for Arduino environments.
- Cross-Platform: Compatible with multiple ESP32 variants using the Arduino core.
| Chip Model | Driver Type | Sensor Status |
|---|---|---|
| ESP32-D0WD | Legacy | not found |
| ESP32-D0WDQ6 | Legacy | not found |
| ESP32-D0WD-V3 | Legacy | not found |
| ESP32-S2 | New | working |
| ESP32-S3 | New | working |
| ESP32-C3 | New | working |
| ESP32-C6 | New | working |
| ESP32-H2 | New | working |
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for
ESPCPUTemp. - Click Install to download and install the library.
- Download the latest release from the GitHub Releases page.
- Extract the
.zipfile. - Move the
ESPCPUTempfolder to your Arduino libraries directory:- Windows:
C:\Users\<YourUsername>\Documents\Arduino\libraries - MacOS/Linux:
~/Documents/Arduino/libraries
- Windows:
- Restart the Arduino IDE.
- Arduino core for ESP32: Version 2.0.0 or higher (recommended: 3.2.0 or later).
- Install via the Arduino Boards Manager by searching for
esp32and selecting the Espressif Systems package.
- Install via the Arduino Boards Manager by searching for
- No additional external libraries are required.
The following example demonstrates how to initialize the temperature sensor and read the CPU temperature:
#include <ESPCPUTemp.h>
ESPCPUTemp tempSensor;
void setup() {
Serial.begin(115200);
delay(100);
if (tempSensor.begin()) {
Serial.println("Temperature sensor initialized successfully");
} else {
Serial.println("Failed to initialize temperature sensor");
}
}
void loop() {
if (tempSensor.tempAvailable()) {
float temp = tempSensor.getTemp();
if (!isnan(temp)) {
Serial.print("CPU Temperature: ");
Serial.print(temp);
Serial.println(" °C");
} else {
Serial.println("Failed to read temperature");
}
}
delay(5000); // Read every 5 seconds
}Temperature sensor initialized successfully
CPU Temperature: 45.2 °C
CPU Temperature: 46.1 °C
-
Constructor:
ESPCPUTemp()- Initializes the temperature sensor object.
-
Destructor:
~ESPCPUTemp()- Cleans up resources and disables the sensor.
-
begin():
bool begin()- Initializes the temperature sensor based on the detected chip model.
- Returns
trueif successful,falseotherwise.
-
tempAvailable():
bool tempAvailable() const- Checks if the temperature sensor is available.
- Returns
trueif the sensor is initialized,falseotherwise.
-
getTemp():
float getTemp()- Reads the current CPU temperature in °C.
- Returns the temperature as a
floatorNANif the reading fails.
The library provides detailed Serial output for debugging, including:
- Chip model detection.
- Driver availability (legacy or new).
- Initialization errors with ESP error codes.
- Read errors during temperature measurement.
The library uses preprocessor directives to enable/disable driver support:
NEW_DRIVER_AVAILABLE: Defined for ESP32-S2, S3, C3, C6, and H2 chips when the new driver is available (requires<driver/temperature_sensor.h>).LEGACY_DRIVER_AVAILABLE: Defined for ESP32 variants when the legacy driver is available (requires<driver/temp_sensor.h>).
These directives are automatically handled by the Arduino core based on the selected board.
- Temperature Range: The sensor is configured for -10°C to 80°C. Modify the
range_minandrange_maxvalues ininitTempSensorNew()ordac_offsetininitTempSensorLegacy()if a different range is needed. - Accuracy: The internal temperature sensor provides approximate values. For precise measurements, consider an external sensor.
- Arduino Core Compatibility: Ensure you are using a recent version of the ESP32 Arduino core (e.g., 3.2.0 or later) for full compatibility with newer chips like ESP32-C6 and H2.
- "Failed to initialize temperature sensor":
- Check if the correct board is selected in the Arduino IDE (e.g., "ESP32 Dev Module" for ESP32, "ESP32C3 Dev Module" for ESP32-C3).
- Ensure the Arduino core is up-to-date.
- "Legacy/New driver not available":
- Verify that the required driver headers (
<driver/temp_sensor.h>or<driver/temperature_sensor.h>) are available in your Arduino core.
- Verify that the required driver headers (
- "Unsupported chip model":
- The library only supports ESP32-based chips. Check the chip model using
ESP.getChipModel().
- The library only supports ESP32-based chips. Check the chip model using
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Make your changes and commit (
git commit -m "Add your feature"). - Push to the branch (
git push origin feature/your-feature). - Open a Pull Request.
Please ensure your code follows the existing style and includes appropriate documentation.
This library is licensed under the MIT License. See the LICENSE file for details.
For questions or support, please open an issue on the GitHub repository.