Skip to main content
ESPBoards

ESP32 SHT11 Temperature and Humidity Sensor

The SHT11 is a high-precision digital temperature and humidity sensor that offers fully calibrated digital output. Its compact design, low power consumption, and excellent long-term stability make it ideal for a wide range of environmental monitoring applications.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image

🔗 Quick Links

SHT11 Temperature and Humidity Sensor Datasheet ButtonSHT11 Temperature and Humidity Sensor Specs ButtonSHT11 Temperature and Humidity Sensor Specs ButtonSHT11 Temperature and Humidity Sensor Specs Button

ℹ️ About SHT11 Temperature and Humidity Sensor

The SHT11, developed by Sensirion, is a high-precision digital temperature and humidity sensor known for its reliability and stability. It integrates sensor elements and signal processing in a compact design, providing fully calibrated digital output.

Key Features #

  • High Accuracy & Stability – Ensures precise humidity and temperature measurements.
  • CMOSens® Technology – Provides long-term reliability and fast response times.
  • Capacitive Humidity Sensing & Band-Gap Temperature Sensor – Delivers consistent and accurate data.
  • Ideal for Professional Applications – Used in HVAC systems, data loggers, and weather stations.

With its precision and durability, the SHT11 is a trusted choice for environmental monitoring in industrial and scientific applications. 🚀

⚙️ SHT11 Sensor Technical Specifications

Below you can see the SHT11 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.

  • Type: environment
  • Protocol: Sensibus
  • Interface: Sensibus (2-wire proprietary protocol)
  • Operating Voltage: 2.4V to 5.5V
  • Temperature Range: -40°C to +123.8°C
  • Temperature Accuracy: ±0.4°C
  • Humidity Range: 0% to 100% RH
  • Humidity Accuracy: ±3% RH
  • Resolution: Temperature: 14-bit; Humidity: 12-bit
  • Power Consumption: Measuring: 550 µA; Sleep: 2 µA
  • Response Time: Humidity: 8s (τ63%)
  • Dimensions: 7.5mm x 4.9mm x 2.5mm

🔌 SHT11 Sensor Pinout

Below you can see the pinout for the SHT11 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 SHT11 sensor has the following pins: 1 (GND): Ground. 2 (DATA): Serial data line for communication. 3 (SCK): Serial clock line for communication. 4 (VDD): Power supply (2.4V to 5.5V).

🧵 SHT11 Wiring with ESP32

Below you can see the wiring for the SHT11 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 connect the SHT11 sensor to an ESP32: VDD: Connect to 3.3V on the ESP32. GND: Connect to GND on the ESP32. DATA: Connect to a GPIO pin (e.g., GPIO21). SCK: Connect to another GPIO pin (e.g., GPIO22). Note: The SHT11 uses a proprietary 2-wire protocol (Sensibus), which is similar to I²C but not identical. Ensure that the chosen GPIO pins are configured correctly in your code.

🛠️ SHT11 Temperature and Humidity Sensor Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ Initialization Failure

Issue: The sensor fails to initialize, and no data is received.

Solution: Ensure that the DATA and SCK lines are correctly connected to the specified GPIO pins on the microcontroller. Verify that the power supply voltage is within the specified range (2.4V to 5.5V). Check for proper pull-up resistors on the DATA line if required by your specific setup.

⚠️ Incorrect Readings

Issue: The sensor provides inaccurate temperature or humidity readings.

Solution: Avoid placing the sensor near heat sources or in direct sunlight. Ensure that the sensor is not exposed to condensation or water droplets. Allow the sensor to stabilize after power-up, as recommended by the manufacturer. Calibration may be necessary for precise measurements.

🚫 Communication Errors

Issue: Communication with the sensor is intermittent or fails.

Solution: Verify that the correct communication protocol (Sensibus) is implemented in your code. Ensure that the timing requirements for the SCK signal are met. Check the integrity of the DATA and SCK connections and ensure that appropriate pull-up resistors are in place if not already included on the sensor module.

💻 Code Examples

Below you can find code examples of SHT11 Temperature and Humidity Sensor with ESP32 in several frameworks:

If you encounter issues while using the SHT11 Temperature and Humidity Sensor, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 SHT11 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the SHT11 Temperature and Humidity Sensor:

#include <SHT1x.h>

#define dataPin 10
#define clockPin 11

SHT1x sht1x(dataPin, clockPin);

void setup() {
Serial.begin(115200);
}

void loop() {
float tempC = sht1x.readTemperatureC();
float humidity = sht1x.readHumidity();

Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

delay(2000);
}

This Arduino code initializes the SHT11 sensor using the SHT1x library. In the setup() function, it sets up serial communication. In the loop() function, it reads the temperature in Celsius and humidity from the sensor and prints the values to the Serial Monitor every two seconds. The SHT1x library facilitates communication with the sensor over its proprietary 2-wire interface.

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 SHT11 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the SHT11 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 "sht1x.h"
#include "esp_log.h"

#define DATA_PIN GPIO_NUM_21
#define SCK_PIN GPIO_NUM_22

static const char *TAG = "SHT11";

void app_main() {
ESP_LOGI(TAG, "Initializing SHT11...");
sht1x_dev_t dev;
sht1x_init(&dev, DATA_PIN, SCK_PIN);

while (1) {
float temperature, humidity;
if (sht1x_read_temperature(&dev, &temperature) == ESP_OK &&
sht1x_read_humidity(&dev, &humidity) == ESP_OK) {
ESP_LOGI(TAG, "Temperature: %.2f°C", temperature);
ESP_LOGI(TAG, "Humidity: %.2f%%", humidity);
} else {
ESP_LOGE(TAG, "Failed to read data from SHT11 sensor");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}

This ESP-IDF code configures the ESP32 to communicate with the SHT11 sensor using its proprietary Sensibus interface. The sht1x_init function initializes the sensor, while the sht1x_read_temperature and sht1x_read_humidity functions fetch temperature and humidity data. The values are logged to the console every two seconds, and errors are reported if data retrieval fails.

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 SHT11 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the SHT11 Temperature and Humidity Sensor

sensor:
- platform: sht1x
data_pin: GPIO21
clock_pin: GPIO22
temperature:
name: "SHT11 Temperature"
humidity:
name: "SHT11 Humidity"
update_interval: 2s

This ESPHome configuration sets up the SHT11 sensor to measure temperature and humidity. The sensor communicates over GPIO pins 21 and 22 using its Sensibus protocol. The readings update every two seconds.

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

PlatformIO Image

ESP32 SHT11 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 =
panStamp/SHT1x Library
wire
monitor_speed = 115200

ESP32 SHT11 PlatformIO Example Code

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

#include <SHT1x.h>

#define dataPin 21
#define clockPin 22

SHT1x sht1x(dataPin, clockPin);

void setup() {
Serial.begin(115200);
}

void loop() {
Serial.print("Temperature: ");
Serial.print(sht1x.readTemperatureC(), 1);
Serial.println(" °C");

Serial.print("Humidity: ");
Serial.print(sht1x.readHumidity(), 1);
Serial.println(" %");

delay(2000);
}

This PlatformIO code initializes the SHT11 sensor using the SHT1x library. The sensor communicates over GPIO pins 21 and 22 using its proprietary Sensibus interface. The program continuously reads and prints temperature and humidity values to the Serial Monitor every two seconds.

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

Conclusion

We went through technical specifications of SHT11 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT11 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.