ESP32 SHT25 Temperature and Humidity Sensor
The SHT25 sensor is a high-accuracy digital temperature and humidity sensor that utilizes Sensirion's CMOSens® technology. It provides calibrated, linearized sensor signals in digital I2C format, making it ideal for applications requiring precise and reliable environmental measurements.
Jump to Code Examples
Quick Links
SHT25 Price
About SHT25 Temperature and Humidity Sensor
The SHT25, developed by Sensirion, is a high-precision digital temperature and humidity sensor. It offers superior accuracy and reliability, making it suitable for demanding applications such as industrial monitoring, HVAC systems, and environmental data logging. For alternative options, consider the [SHT21](/blog/sht21/) or [SHT30](/blog/sht30/) sensors, which provide different levels of performance and features.SHT25 Sensor Technical Specifications
Below you can see the SHT25 Temperature and Humidity Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C
- Operating Voltage: 2.1V to 3.6V
- Temperature Range: -40°C to 125°C
- Humidity Range: 0% to 100% RH
- Temperature Accuracy: ±0.2°C
- Humidity Accuracy: ±1.8% RH
- Interface: I2C
- Dimensions: 3mm x 3mm x 1.1mm
SHT25 Sensor Pinout
Below you can see the pinout for the SHT25 Temperature and Humidity Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The SHT25 pinout is as follows:
- VDD: Power supply voltage (2.1V to 3.6V).
- GND: Ground.
- SDA: Serial Data Line for I2C communication.
- SCL: Serial Clock Line for I2C communication.
SHT25 Wiring with ESP32
Below you can see the wiring for the SHT25 Temperature and Humidity Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
- Connect VDD to the 3.3V pin on the ESP32.
- Connect GND to the ground (GND) of the ESP32.
- Connect SDA to the ESP32's GPIO21 (default I2C data pin).
- Connect SCL to the ESP32's GPIO22 (default I2C clock pin).
- Place pull-up resistors (10kΩ) between SDA and VDD, and SCL and VDD, to ensure reliable communication.
Code Examples
Below you can find code examples of SHT25 Temperature and Humidity Sensor with ESP32 in several frameworks:
ESP32 SHT25 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the SHT25 Temperature and Humidity Sensor:
#include <Wire.h>
#include "SHT2x.h"
SHT2x sht;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
float temperature = sht.GetTemperature();
float humidity = sht.GetHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
This Arduino sketch demonstrates how to interface with the SHT25 sensor using the SHT2x library. It initializes the sensor and reads temperature and humidity data every 2 seconds, printing the results to the Serial Monitor. Ensure that the SHT2x library is installed in your Arduino IDE.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 SHT25 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the SHT25 Temperature and Humidity Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
uint16_t raw_humidity = (data[0] << 8) | data[1];
uint8_t cmd_temp = 0xF3; // Command for temperature measurement
i2c_master_write_to_device(I2C_MASTER_NUM, SHT25_SENSOR_ADDR, &cmd_temp, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(50));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT25_SENSOR_ADDR, data, 3, pdMS_TO_TICKS(1000));
uint16_t raw_temperature = (data[0] << 8) | data[1];
float humidity = -6.0 + 125.0 * ((float)raw_humidity / 65536.0);
float temperature = -46.85 + 175.72 * ((float)raw_temperature / 65536.0);
printf("Temperature: %.2f °C, Humidity: %.2f %%\n", temperature, humidity);
}
void app_main() {
ESP_ERROR_CHECK(i2c_master_init());
while (1) {
read_sht25_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code demonstrates how to interface with the SHT25 sensor using the I2C protocol. It initializes the I2C master and communicates with the sensor to read temperature and humidity data. Raw sensor values are converted to human-readable units using the formulas provided in the SHT25 datasheet. Readings are printed to the console every 2 seconds.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 SHT25 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the SHT25 Temperature and Humidity Sensor
sensor:
- platform: sht3x
address: 0x40
temperature:
name: "Indoor Temperature"
humidity:
name: "Indoor Humidity"
update_interval: 60s
This ESPHome configuration defines the SHT25 sensor using the `sht3x` platform (compatible for earlier SHT2x series as well). The I2C address is set to 0x40. Two entities are created for temperature and humidity, labeled with user-friendly names like 'Indoor Temperature' and 'Indoor Humidity.' The `update_interval` is set to 60 seconds, meaning data is refreshed every minute.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 SHT25 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
"Sensirion/SHT2x"
monitor_speed = 115200
ESP32 SHT25 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the SHT25 Temperature and Humidity Sensor:
#include <Wire.h>
#include "SHT2x.h"
SHT2x sht;
void setup() {
Wire.begin();
Serial.begin(115200);
}
void loop() {
float temperature = sht.GetTemperature();
float humidity = sht.GetHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
This PlatformIO sketch demonstrates the usage of the SHT25 sensor with the ESP32 board. It uses the Sensirion SHT2x library to interface with the sensor. The code initializes the sensor, reads temperature and humidity data every 2 seconds, and prints the results to the Serial Monitor. Ensure the `Sensirion/SHT2x` library is included in the project dependencies.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 SHT25 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the SHT25 Temperature and Humidity Sensor with your ESP32.
from machine import I2C, Pin
from time import sleep
import sht21
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Initialize the SHT25 sensor
sensor = sht21.SHT21(i2c)
while True:
try:
temperature = sensor.read_temperature()
humidity = sensor.read_humidity()
print(f"Temperature: {temperature:.2f} °C")
print(f"Humidity: {humidity:.2f} %")
except Exception as e:
print(f"Error reading sensor: {e}")
sleep(2)
This MicroPython code uses the `sht21` library to communicate with the SHT25 sensor over I2C. It reads temperature and humidity data in a loop, prints the readings to the console, and handles any errors that occur during communication. The data is updated every 2 seconds.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of SHT25 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT25 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.