Skip to main content
ESPBoards

ESP32 SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor

The SHT41 sensor is part of Sensirion's SHT4x series and offers high-accuracy temperature and humidity measurements. It is factory calibrated and provides digital I2C output for easy integration. The SHT41 is a robust and reliable solution for applications requiring precise environmental monitoring.

Jump to Code Examples

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

Quick Links

SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor Datasheet ButtonSHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor Specs ButtonSHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor Specs ButtonSHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor Specs Button

SHT41 / SHT41-D / GY-SHT41-D Price

Normally, the SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor costs around Approximately $10 per unit.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

About SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor

The SHT41, SHT41-D, and GY-SHT41-D sensors, developed by Sensirion, are highly precise temperature and humidity sensors based on the SHT4x series. They provide fully calibrated, linearized, and temperature-compensated digital outputs. These sensors are ideal for demanding applications, including environmental monitoring, industrial processes, and IoT devices.

SHT41 / SHT41-D / GY-SHT41-D Sensor Technical Specifications

Below you can see the SHT41 / SHT41-D / GY-SHT41-D 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.4V to 5.5V
  • Temperature Range: -40°C to 125°C
  • Humidity Range: 0% to 100% RH
  • Temperature Accuracy: ±0.1°C
  • Humidity Accuracy: ±1.5% RH
  • Interface: I2C
  • Dimensions: 2.5mm x 2.5mm x 0.9mm

SHT41 / SHT41-D / GY-SHT41-D Wiring with ESP32

Below you can see the wiring for the SHT41 / SHT41-D / GY-SHT41-D 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 the SHT41 sensor with an ESP32:
  • 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 SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor with ESP32 in several frameworks:

Arduino Core Image

ESP32 SHT41 / SHT41-D / GY-SHT41-D Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor:

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

Adafruit_SHT4x sht4;

void setup() {
Serial.begin(115200);
if (!sht4.begin()) {
Serial.println("Couldn't find SHT41 sensor!");
while (1) delay(10);
}
sht4.setPrecision(SHT4X_HIGH_PRECISION);
Serial.println("SHT41 initialized");
}

void loop() {
sensors_event_t humidity, temp;
if (!sht4.getEvent(&humidity, &temp)) {
Serial.println("Failed to read sensor data");
return;
}
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" °C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println(" %");
delay(2000);
}

This Arduino sketch demonstrates how to use the SHT41 sensor with the Adafruit SHT4x library. It initializes the sensor, configures it for high precision, and reads temperature and humidity values every 2 seconds. The readings are printed to the Serial Monitor.

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 SHT41 / SHT41-D / GY-SHT41-D ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the SHT41 / SHT41-D / GY-SHT41-D 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 for I2C master clock */
#define I2C_MASTER_SDA_IO 21 /*!< GPIO number for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C master port */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
#define SHT41_SENSOR_ADDR 0x44 /*!< SHT41 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_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
return i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
}

void read_sht41_sensor() {
uint8_t data[6];
uint8_t cmd[] = {0xFD}; // High precision measurement command
i2c_master_write_to_device(I2C_MASTER_NUM, SHT41_SENSOR_ADDR, cmd, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(10));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT41_SENSOR_ADDR, data, 6, 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_sht41_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}

This ESP-IDF code initializes the I2C master, sends a command to the SHT41 sensor to perform high-precision measurements, reads the raw temperature and humidity data, and converts it into human-readable values. The results are printed 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 SHT41 / SHT41-D / GY-SHT41-D ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor

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

This ESPHome configuration defines the SHT41 sensor using the `sht4x` platform. It configures the sensor to read and update temperature and humidity values every 60 seconds, with user-friendly names like 'Room Temperature' and 'Room Humidity.'

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

PlatformIO Image

ESP32 SHT41 / SHT41-D / GY-SHT41-D 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 =
adafruit/Adafruit SHT4x Library @ ^1.0.0
monitor_speed = 115200

ESP32 SHT41 / SHT41-D / GY-SHT41-D PlatformIO Example Code

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

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

Adafruit_SHT4x sht4;

void setup() {
Serial.begin(115200);
if (!sht4.begin()) {
Serial.println("Couldn't find SHT41 sensor!");
while (1) delay(10);
}
sht4.setPrecision(SHT4X_HIGH_PRECISION);
Serial.println("SHT41 initialized.");
}

void loop() {
sensors_event_t temp, humidity;
if (!sht4.getEvent(&humidity, &temp)) {
Serial.println("Failed to read sensor data");
return;
}
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity.relative_humidity);
Serial.println(" %");
delay(2000);
}

This PlatformIO code uses the Adafruit SHT4x library to interface with the SHT41 sensor. The sensor is initialized in high-precision mode, and temperature and humidity readings are obtained and printed every 2 seconds.

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

MicroPython Image

ESP32 SHT41 / SHT41-D / GY-SHT41-D MicroPython Code Example

Example in Micro Python Framework

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

from machine import I2C, Pin
from time import sleep

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

# SHT41 I2C address
SHT41_ADDR = 0x44

# Command for high-precision measurement
MEASURE_CMD = b'\xFD'

while True:
try:
i2c.writeto(SHT41_ADDR, MEASURE_CMD)
sleep(0.01) # Wait for measurement to complete
data = i2c.readfrom(SHT41_ADDR, 6)
raw_temp = (data[0] << 8) | data[1]
raw_hum = (data[3] << 8) | data[4]
temperature = -45 + 175 * (raw_temp / 65535.0)
humidity = 100 * (raw_hum / 65535.0)
print("Temperature: {:.2f} °C".format(temperature))
print("Humidity: {:.2f} %".format(humidity))
except Exception as e:
print("Error reading SHT41: ", e)
sleep(2)

This MicroPython script interfaces with the SHT41 sensor using I2C. It sends a command for high-precision measurement, reads the raw data, processes it, and prints the temperature and humidity values 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 SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT41 / SHT41-D / GY-SHT41-D Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.