Skip to main content
ESPBoards

ESP32 SHT20 Temperature and Humidity Sensor

The SHT20 sensor is a digital temperature and humidity sensor that utilizes Sensirion's CMOSens® technology. It provides calibrated, linearized sensor signals in digital, I2C format, making it ideal for applications requiring accurate and reliable environmental measurements.

Jump to Code Examples

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

Quick Links

SHT20 Temperature and Humidity Sensor Datasheet ButtonSHT20 Temperature and Humidity Sensor Specs ButtonSHT20 Temperature and Humidity Sensor Specs ButtonSHT20 Temperature and Humidity Sensor Specs Button

SHT20 Price

Normally, the SHT20 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 SHT20 Temperature and Humidity Sensor

The SHT20, developed by Sensirion, is a digital temperature and humidity sensor known for its reliability and accuracy. It offers a cost-effective solution for various applications, including HVAC systems, data loggers, and consumer electronics. For enhanced performance, consider the [SHT21](/blog/sht21/) or [SHT30](/blog/sht30/) sensors, which provide improved accuracy and additional features.

SHT20 Sensor Technical Specifications

Below you can see the SHT20 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: ±3% RH
  • Interface: I2C
  • Dimensions: 3mm x 3mm x 1.1mm

SHT20 Sensor Pinout

Below you can see the pinout for the SHT20 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 SHT20 pinout 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.

Code Examples

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

Arduino Core Image

ESP32 SHT20 Arduino IDE Code Example

Example in Arduino IDE

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

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

DFRobot_SHT20 sht20;

void setup() {
Serial.begin(115200);
Wire.begin();
sht20.initSHT20();
delay(100);
sht20.checkSHT20();
}

void loop() {
float humidity = sht20.readHumidity();
float temperature = sht20.readTemperature();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(2000);
}

This Arduino sketch demonstrates how to interface with the SHT20 sensor using the DFRobot_SHT20 library. It initializes the sensor and reads temperature and humidity data every 2 seconds, printing the results to the Serial Monitor. Ensure that the DFRobot_SHT20 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 SHT20 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the SHT20 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 SHT20_SENSOR_ADDR 0x40 /*!< SHT20 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_sht20_sensor() {
uint8_t data[3];
uint8_t cmd = 0xF5; // Command for humidity measurement
i2c_master_write_to_device(I2C_MASTER_NUM, SHT20_SENSOR_ADDR, &cmd, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(50));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT20_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; // Command for temperature measurement
i2c_master_write_to_device(I2C_MASTER_NUM, SHT20_SENSOR_ADDR, &cmd, 1, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(50));
i2c_master_read_from_device(I2C_MASTER_NUM, SHT20_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, Humidity: %.2f %%\n", temperature, humidity);
}

void app_main() {
ESP_ERROR_CHECK(i2c_master_init());

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

This ESP-IDF code interfaces with the SHT20 sensor using the I2C protocol. The `read_sht20_sensor()` function reads raw temperature and humidity data by sending the appropriate commands to the sensor and processing the response. The results are converted into human-readable temperature (°C) and humidity (%) values and 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 SHT20 ESPHome Code Example

Example in ESPHome (Home Assistant)

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

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

This ESPHome configuration defines the SHT20 sensor using the `sht3x` platform. The I2C address of the sensor is set to 0x40, and two sensor entities are created: one for temperature and one for humidity. The `update_interval` is set to 60 seconds, meaning the sensor readings will be updated every minute.

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

PlatformIO Image

ESP32 SHT20 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/DFRobot_SHT20 @ ^1.0.0
monitor_speed = 115200

ESP32 SHT20 PlatformIO Example Code

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

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

DFRobot_SHT20 sht20;

void setup() {
Serial.begin(115200);
Wire.begin();
sht20.initSHT20();
delay(100);
sht20.checkSHT20();
}

void loop() {
float humidity = sht20.readHumidity();
float temperature = sht20.readTemperature();

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

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

delay(2000);
}

This PlatformIO code demonstrates how to interface with the SHT20 sensor using the DFRobot_SHT20 library. The code initializes the sensor, reads temperature and humidity data every 2 seconds, and prints the results to the Serial Monitor.

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

MicroPython Image

ESP32 SHT20 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the SHT20 Temperature and Humidity Sensor with your ESP32.

from machine import Pin, I2C
from time import sleep

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

# Function to read data from the SHT20
def read_sht20(cmd):
i2c.writeto(SHT20_ADDR, bytearray([cmd]))
sleep(0.05)
data = i2c.readfrom(SHT20_ADDR, 3)
raw = (data[0] << 8) | (data[1] & 0xFC)
return raw

while True:
# Read humidity
raw_humidity = read_sht20(0xF5)
humidity = -6.0 + 125.0 * (raw_humidity / 65536.0)

# Read temperature
raw_temperature = read_sht20(0xF3)
temperature = -46.85 + 175.72 * (raw_temperature / 65536.0)

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

This MicroPython code interfaces with the SHT20 sensor using I2C. It sends commands to the sensor to read raw temperature and humidity data, processes the raw data into human-readable values, and prints the results 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 SHT20 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT20 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.