ESP32 SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor
The SHT31 series sensors utilize Sensirion's CMOSens® technology to deliver accurate, stable, and linear temperature and humidity data. Their robust calibration and long-term reliability make them ideal for applications that require consistent performance under varying conditions.
Jump to Code Examples
Quick Links
SHT31 / SHT31-D / GY-SHT31 / SI7021 Price
About SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor
The SHT31, SHT31-D, GY-SHT31, and SI7021 sensors share a similar architecture, providing high-accuracy digital temperature and humidity measurements. Building upon the capabilities of its predecessor, the [SHT30](/blog/sht30/), the SHT31 series offers enhanced performance and reliability, making them versatile for applications in HVAC systems, environmental monitoring, data loggers, and IoT projects.SHT31 / SHT31-D / GY-SHT31 / SI7021 Sensor Technical Specifications
Below you can see the SHT31 / SHT31-D / GY-SHT31 / SI7021 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.15V to 5.5V
- Temperature Range: -40°C to 125°C
- Humidity Range: 0% to 100% RH
- Temperature Accuracy: ±0.2°C
- Humidity Accuracy: ±2% RH
- Interface: I2C
- Dimensions: 2.5mm x 2.5mm x 0.9mm
SHT31 / SHT31-D / GY-SHT31 / SI7021 Sensor Pinout
Below you can see the pinout for the SHT31 / SHT31-D / GY-SHT31 / SI7021 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 SHT31 pinout is as follows:
- VDD: Power supply voltage (2.15V to 5.5V).
- GND: Ground.
- SDA: Serial Data Line for I2C communication.
- SCL: Serial Clock Line for I2C communication.
SHT31 / SHT31-D / GY-SHT31 / SI7021 Wiring with ESP32
Below you can see the wiring for the SHT31 / SHT31-D / GY-SHT31 / SI7021 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 SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor with ESP32 in several frameworks:
ESP32 SHT31 / SHT31-D / GY-SHT31 / SI7021 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
if (!sht31.begin(0x44)) { // Set to 0x45 for alternate I2C address
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
}
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (!isnan(t) && !isnan(h)) { // Check if readings are valid
Serial.print("Temp *C = "); Serial.print(t); Serial.print(" ");
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read from SHT31 sensor");
}
delay(1000);
}
This Arduino sketch demonstrates how to interface with the SHT31 series sensors using the Adafruit SHT31 library. It initializes the sensor and reads temperature and humidity data every second, printing the results to the Serial Monitor. If your sensor uses the alternate I2C address (0x45), adjust the initialization accordingly.
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 SHT31 / SHT31-D / GY-SHT31 / SI7021 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the SHT31 / SHT31-D / GY-SHT31 / SI7021 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:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#define I2C_MASTER_SCL_IO 22 /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO 21 /*!< GPIO number used for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C master I2C port number */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
#define SHT31_SENSOR_ADDR 0x44 /*!< SHT31 I2C address */
static esp_err_t i2c_master_init(void) {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
esp_err_t err = i2c_param_config(I2C_MASTER_NUM, &conf);
if (err != ESP_OK) {
return err;
}
return i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
}
void read_sht31_sensor() {
uint8_t data[6];
i2c_master_write_read_device(I2C_MASTER_NUM, SHT31_SENSOR_ADDR, NULL, 0, data, sizeof(data), pdMS_TO_TICKS(1000));
uint16_t temp_raw = (data[0] << 8) | data[1];
uint16_t hum_raw = (data[3] << 8) | data[4];
float temperature = -45 + 175 * ((float)temp_raw / 65535.0);
float humidity = 100 * ((float)hum_raw / 65535.0);
printf("Temperature: %.2f °C, Humidity: %.2f %%\n", temperature, humidity);
}
void app_main() {
ESP_ERROR_CHECK(i2c_master_init());
while (1) {
read_sht31_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code demonstrates how to interface with SHT31-compatible sensors using the I2C protocol. It initializes I2C communication, retrieves raw temperature and humidity data, converts it to human-readable values, and prints the results 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 SHT31 / SHT31-D / GY-SHT31 / SI7021 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor
sensor:
- platform: sht3x
address: 0x44
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
update_interval: 60s
This ESPHome configuration uses the `sht3x` platform to integrate SHT31 sensors. It reads and updates temperature and humidity values every 60 seconds, with descriptive names like 'Room Temperature' and 'Room Humidity.'
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 SHT31 / SHT31-D / GY-SHT31 / SI7021 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 =
adafruit/Adafruit SHT31 Library @ ^2.0.0
monitor_speed = 115200
ESP32 SHT31 / SHT31-D / GY-SHT31 / SI7021 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
if (!sht31.begin(0x44)) { // Default I2C address for SHT31
Serial.println("Couldn't find SHT31 sensor!");
while (1) delay(1);
}
}
void loop() {
float temperature = sht31.readTemperature();
float humidity = sht31.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println("Failed to read from SHT31 sensor");
}
delay(2000);
}
This PlatformIO example initializes the SHT31 sensor using the Adafruit library, reads temperature and humidity values, and prints the results every 2 seconds. It ensures compatibility with ESP32 and Arduino-based projects.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 SHT31 / SHT31-D / GY-SHT31 / SI7021 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor with your ESP32.
from machine import I2C, Pin
from time import sleep
import sht31
# Initialize I2C interface
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Initialize the SHT31 sensor
sensor = sht31.SHT31(i2c, addr=0x44)
print("SHT31 Sensor Example")
while True:
try:
temperature, humidity = sensor.measure()
print("Temperature: {:.2f} °C".format(temperature))
print("Humidity: {:.2f} %".format(humidity))
except Exception as e:
print("Error reading from SHT31: ", e)
sleep(2)
This MicroPython script demonstrates how to use the SHT31 sensor with an I2C interface. It reads temperature and humidity data, converts it into human-readable values, and prints the results to the console 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 SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT31 / SHT31-D / GY-SHT31 / SI7021 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.