Sensors/ Environment/ SHT21 / HTU21 / GY-21 / SI7021

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.

SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor image
SHT21 / HTU21 / GY-21 / SI7021 · I2C
I2C
Interface
4pins
Connections
2.1-3.6V
Supply
±0.3°C
Accuracy
±2% RH
Humidity accuracy
$5
Typical price
On this page

SHT21 / HTU21 / GY-21 / SI7021 pinout

4 pins · I2C

The SHT21/HTU21/GY-21/SI7021 sensors use standard I²C communication with 4 pins.

View:
SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor pinout
PinTypeDescriptionNotes
VCCPowerPower supply input (2.1V to 3.6V)Low voltage operation for battery applications
GNDPowerGround connectionConnect to ESP32 ground
SDACommunicationI²C data lineBidirectional data communication
SCLCommunicationI²C clock lineClock signal from master device
  • Standard I²C interface for easy integration

  • Default I²C address is 0x40

  • Multiple sensor names (SHT21/HTU21/GY-21/SI7021) - same chip

  • Pull-up resistors (10kΩ) recommended on SDA/SCL

  • High accuracy: ±0.3°C temperature, ±3% humidity

Wiring the SHT21 / HTU21 / GY-21 / SI7021 to ESP32

4 connections · all required

Connect the SHT21/HTU21/GY-21/SI7021 using standard I²C interface.

SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor wiring with ESP32
SHT21 / HTU21 / GY-21 / SI7021 pinESP32 pinPurpose
VCC3.3VPower supply (2.1V to 3.6V)
GNDGNDGround connection
SDAGPIO21I²C data line (default SDA)
SCLGPIO22I²C clock line (default SCL)
  • GPIO21/22 are default I²C pins on ESP32

  • I²C address is 0x40 (standard for this sensor)

  • Add 10kΩ pull-up resistors on SDA/SCL if needed

  • Compatible with SHT20 libraries and code

SHT21 / HTU21 / GY-21 / SI7021 code examples

5 platforms
Platform:

SHT21 / HTU21 / GY-21 / SI7021 Arduino example

Copy
// Requires library: "SHT2x"
#include <Wire.h>
#include <SHT2x.h>

SHT2x sht;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if (!sht.begin()) {
    Serial.println("Could not find SHT21 sensor, check wiring!");
    while (1) delay(10);
  }
}

void loop() {
  if (sht.read()) {
    Serial.print("Temperature: ");
    Serial.print(sht.getTemperature());
    Serial.println(" C");
    Serial.print("Humidity: ");
    Serial.print(sht.getHumidity());
    Serial.println(" %");
  } else {
    Serial.println("Failed to read from SHT21 sensor");
  }
  delay(2000);
}

This example uses Rob Tillaart's SHT2x library (install "SHT2x" from the Library Manager), which covers the whole SHT2x family including the SHT21. read() performs a blocking measurement, after which getTemperature() and getHumidity() return the converted values. The sensor sits on the default I2C pins (SDA GPIO21, SCL GPIO22) at address 0x40.

SHT21 / HTU21 / GY-21 / SI7021 ESP-IDF example

Copy
#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.

SHT21 / HTU21 / GY-21 / SI7021 ESPHome example

Copy
i2c:
  sda: GPIO21
  scl: GPIO22

sensor:
  - platform: htu21d
    temperature:
      name: "Room Temperature"
    humidity:
      name: "Room Humidity"
    update_interval: 60s

The SHT21 belongs to Sensirion's SHT2x generation, which ESPHome serves with the htu21d platform (the HTU21D is the same die; the platform also covers Si7021 and SHT21) - not with sht3x-style platforms, which speak a different command set. The sensor sits at the family's fixed address 0x40 on the default I2C pins.

SHT21 / HTU21 / GY-21 / SI7021 PlatformIO example

Copy
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    robtillaart/SHT2x @ ^0.5.0
src/main.cppCopy
#include <Arduino.h>
#include <Wire.h>
#include <SHT2x.h>

SHT2x sht;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if (!sht.begin()) {
    Serial.println("Could not find SHT21 sensor, check wiring!");
    while (1) delay(10);
  }
}

void loop() {
  if (sht.read()) {
    Serial.print("Temperature: ");
    Serial.print(sht.getTemperature());
    Serial.println(" C");
    Serial.print("Humidity: ");
    Serial.print(sht.getHumidity());
    Serial.println(" %");
  } else {
    Serial.println("Failed to read from SHT21 sensor");
  }
  delay(2000);
}

This PlatformIO example uses Rob Tillaart's SHT2x library (pinned as robtillaart/SHT2x in lib_deps - note that no "DFRobot SHT21" package exists on the registry), which covers the whole SHT2x family. read() performs a blocking measurement, then getTemperature() and getHumidity() return the converted values; the sensor sits at the family's fixed address 0x40 on the default I2C pins.

SHT21 / HTU21 / GY-21 / SI7021 MicroPython example

Copy
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.

SHT21 / HTU21 / GY-21 / SI7021 specifications

From the datasheet
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

About the SHT21 / HTU21 / GY-21 / SI7021

The SHT21 is the standard-accuracy tier of Sensirion’s SHT2x line, sitting between the budget SHT20 and the high-end SHT25: the same fixed I2C address 0x40 and 2.1 V to 3.6 V supply, with accuracy improved to ±0.3 degC / ±2 %RH. Silicon Labs designed its Si7021 as a deliberate footprint- and command-compatible second source for this exact chip, which is why so many “GY-21” breakout boards mix genuine Sensirion SHT21, TE Connectivity’s HTU21D, and Si7021 silicon under one listing and one driver.

Sensirion has marked the SHT21 not recommended for new designs and names the SHT41 as its replacement - a broadly similar humidity accuracy number on paper, but one the SHT41 guarantees across the sensor’s full 0-100 %RH range rather than just the comfortable middle band, along with lower power draw and a lower voltage floor.

Either way, an existing SHT21 design keeps working as it is; a new one gets a meaningfully tighter guaranteed accuracy for a similar bill-of-materials cost by starting with the SHT41 instead.

SHT21 / HTU21 / GY-21 / SI7021 troubleshooting

4 common issues

Compilation Errors with SHT21 Library

Issue: When compiling code that interfaces with the SHT21 sensor using the LibHumidity library, errors such as 'class TwoWire' has no member named 'send' and 'class TwoWire' has no member named 'receive' are encountered.

Possible causes include the use of outdated functions in the library that are incompatible with the current Wire library, which now uses write() and read() methods instead of send() and receive().

Solution: Update the LibHumidity library by replacing instances of send() with write() and receive() with read(). Alternatively, consider using a more recent library that supports the SHT21 sensor and is compatible with the current Wire library implementation.

Permission Denied Error on Raspberry Pi

Issue: When running a Python script to read data from the SHT21 sensor on a Raspberry Pi, the error [Errno 13] Permission denied: '/dev/i2c-1' is encountered.

Possible causes include insufficient permissions to access the I2C bus device file.

Solution: Execute the Python script with elevated privileges by prefixing the command with sudo. For example, run sudo python sht21.py to grant the necessary permissions to access the I2C bus.

Incorrect Temperature and Humidity Readings

Issue: The SHT21 sensor returns incorrect temperature and humidity values, such as 988 instead of the expected readings.

Possible causes include improper wiring, incorrect I2C address configuration, or sensor initialization issues.

Solution: Verify that the sensor is correctly wired to the microcontroller, ensuring proper connections for power, ground, and I2C data lines. Confirm that the correct I2C address (typically 0x40) is specified in your code. Ensure that the sensor is properly initialized in the software, and consider using a reliable library compatible with the SHT21 sensor.

Interfacing Issues with SHT21 Sensor

Issue: Difficulty in establishing I2C communication with the SHT21 sensor, leading to unsuccessful data retrieval.

Possible causes include incorrect I2C initialization, improper sensor configuration, or timing issues in the communication protocol.

Solution: Ensure that the I2C bus is correctly initialized with the appropriate settings, including clock speed and addressing. Review the sensor's datasheet to confirm proper configuration and command sequences. Implement necessary delays as specified in the sensor's communication protocol to accommodate timing requirements.

Where to buy the SHT21 / HTU21 / GY-21 / SI7021

SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor
SHT21 / HTU21 / GY-21 / SI7021 Temperature and Humidity Sensor
$5per unit, typical
Amazon and AliExpress links are affiliate links - buying through them supports the site at no extra cost to you.

Resources