Skip to main content
ESPBoards

ESP32 SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor

The SHT21, HTU21, GY-21, and SI7021 sensors utilize I2C for reliable communication and provide calibrated, linearized temperature and humidity readings. Their compact form factor and low power consumption make them ideal for precise environmental monitoring.

Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

Quick Links

SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor Datasheet ButtonSHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor Specs ButtonSHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor Specs ButtonSHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor Specs Button

SHT21 / HTU21 / GY-21 / SI7021 Price

Normally, the SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor costs around Approximately $5 per unit.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

About SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor

The SHT21, HTU21, GY-21, and SI7021 sensors share similar functionalities, providing high-accuracy digital temperature and humidity measurements. These sensors are compact and use the I2C communication protocol, making them versatile for applications like HVAC systems, data loggers, weather stations, and consumer electronics. For other options in the SHT series, consider the [SHT20](/blog/sht20/), [SHT25](/blog/sht25/), or [SHT30](/blog/sht30/), which offer variations in performance and features.

SHT21 / HTU21 / GY-21 / SI7021 Sensor Technical Specifications

Below you can see the SHT21 / HTU21 / GY-21 / 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.1V to 3.6V
  • Temperature Range: -40°C to 125°C
  • Humidity Range: 0% to 100% RH
  • Temperature Accuracy: ±0.3°C
  • Humidity Accuracy: ±2% RH
  • Interface: I2C
  • Dimensions: 3mm x 3mm x 1.1mm

SHT21 / HTU21 / GY-21 / SI7021 Sensor Pinout

Below you can see the pinout for the SHT21 / HTU21 / GY-21 / 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 Pinout for these sensors is as follows:

  • VCC: Power supply voltage (2.1V to 3.6V).
  • GND: Ground.
  • SDA: Serial Data Line for I2C communication.
  • SCL: Serial Clock Line for I2C communication.

SHT21 / HTU21 / GY-21 / SI7021 Wiring with ESP32

Below you can see the wiring for the SHT21 / HTU21 / GY-21 / 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.

To interface with an ESP32:
  • Connect VCC 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 VCC, and SCL and VCC, to ensure reliable communication.

Code Examples

Below you can find code examples of SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor with ESP32 in several frameworks:

Arduino Core Image

ESP32 SHT21 / HTU21 / GY-21 / SI7021 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor:

#include <Wire.h>
#include "SHT21.h"

SHT21 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 these sensors using the SHT21 library. It initializes the sensor and reads temperature and humidity data every 2 seconds, printing the results to the Serial Monitor. Ensure that the SHT21 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.

ESP-IDF Image

ESP32 SHT21 / HTU21 / GY-21 / SI7021 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the SHT21 / HTU21 / GY-21 / 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 SHT21_SENSOR_ADDR 0x40 /*!< I2C address */

void read_sensor() {
uint8_t data[3];
uint8_t cmd = 0xF5; // Humidity command
i2c_master_write_to_device(I2C_MASTER_NUM, SHT21_SENSOR_ADDR, &cmd, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(50));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT21_SENSOR_ADDR, data, 3, pdMS_TO_TICKS(1000));

uint16_t raw_humidity = (data[0] << 8) | (data[1] & 0xFC);
float humidity = -6.0 + 125.0 * (raw_humidity / 65536.0);

cmd = 0xF3; // Temperature command
i2c_master_write_to_device(I2C_MASTER_NUM, SHT21_SENSOR_ADDR, &cmd, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(50));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT21_SENSOR_ADDR, data, 3, pdMS_TO_TICKS(1000));

uint16_t raw_temperature = (data[0] << 8) | (data[1] & 0xFC);
float temperature = -46.85 + 175.72 * (raw_temperature / 65536.0);

printf("Temperature: %.2f °C\n", temperature);
printf("Humidity: %.2f %%\n", humidity);
}

void app_main() {
// Initialize I2C
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,
};
i2c_param_config(I2C_MASTER_NUM, &conf);
i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);

while (1) {
read_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}

This ESP-IDF code demonstrates how to read temperature and humidity data from the SHT21-compatible sensors. It sends commands to initiate measurements, retrieves the raw data over I2C, and calculates the actual values based on datasheet formulas. Results 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.

ESPHome Image

ESP32 SHT21 / HTU21 / GY-21 / SI7021 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor

sensor:
- platform: sht3x
address: 0x40
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
update_interval: 60s

This ESPHome configuration uses the `sht3x` platform for SHT21-compatible sensors. It defines two sensor entities for temperature and humidity, with human-readable names. The `update_interval` is set to 60 seconds, ensuring periodic updates of the readings.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 SHT21 / HTU21 / GY-21 / SI7021 PlatformIO Code Example

Example in PlatformIO Framework

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 =
DFRobot/SHT21 @ ^1.0.0
monitor_speed = 115200

ESP32 SHT21 / HTU21 / GY-21 / SI7021 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor:

#include <Wire.h>
#include "SHT21.h"

SHT21 sht;

void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("SHT21 Sensor Example");
}

void loop() {
float temperature = sht.getTemperature();
float humidity = sht.getHumidity();

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 sensor");
}

delay(2000);
}

This PlatformIO example uses the DFRobot SHT21 library to interact with the sensor. It reads temperature and humidity every 2 seconds and prints the values to the Serial Monitor, ensuring easy integration with ESP32 and other Arduino-compatible boards.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 SHT21 / HTU21 / GY-21 / SI7021 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor with your ESP32.

from machine import Pin, I2C
from time import sleep

SENSOR_ADDR = 0x40
CMD_TEMP = 0xF3
CMD_HUM = 0xF5

def read_sensor(i2c, cmd):
i2c.writeto(SENSOR_ADDR, bytearray([cmd]))
sleep(0.05)
data = i2c.readfrom(SENSOR_ADDR, 3)
return (data[0] << 8 | data[1]) & 0xFFFC

def calc_temperature(raw_temp):
return -46.85 + 175.72 * (raw_temp / 65536.0)

def calc_humidity(raw_hum):
return -6.0 + 125.0 * (raw_hum / 65536.0)

i2c = I2C(0, scl=Pin(22), sda=Pin(21))

while True:
raw_temp = read_sensor(i2c, CMD_TEMP)
raw_hum = read_sensor(i2c, CMD_HUM)

temperature = calc_temperature(raw_temp)
humidity = calc_humidity(raw_hum)

print("Temperature: {:.2f} °C".format(temperature))
print("Humidity: {:.2f} %".format(humidity))
sleep(2)

This MicroPython script interfaces with SHT21-compatible sensors over I2C. It retrieves raw temperature and humidity data, processes it using datasheet equations, and displays the results on 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 SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.