Software
ESP32 SHT3x Driver for ESP-IDF
A driver for Sensirion SHT3x temperature and humidity sensors on the ESP32, packaged as an ESP-IDF component with an I²C example.
- ESP32
- ESP-IDF
- 2 min read
A driver for Sensirion SHT3x temperature and humidity sensors on the ESP32, written for ESP-IDF. The sensor is read over I²C in single-shot mode, which saves power, and the example does a complete measurement with one call.
The component
| Property | Value |
|---|---|
| Name and version | esp32-sht3x, 1.0.8 |
| Requires | ESP-IDF 5.0 or newer |
| Targets | ESP32, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-C61, ESP32-S2 and ESP32-S3 |
| Licence | MIT |
The repository is packaged as an ESP-IDF component. One way to use it is as a dependency in the idf_component.yml of your project:
dependencies:
esp32-sht3x:
git: https://github.com/AchimPieters/esp32-sht3x.git
The header sht3x.h has two functions:
| Function | What it does |
|---|---|
| sht3x_init(address) | Initialises the sensor at the given I²C address |
| sht3x_read_temperature_humidity(address, &temperature, &humidity) | Does a complete measurement and returns the temperature and the relative humidity as floating point numbers |
The header also defines SHT3X_I2C_PORT as I2C_NUM_0, the soft reset command 0x30A2 and the high repeatability measurement command 0x2400. The I²C address is set with CONFIG_SHT3X_I2C_ADDRESS.
Wiring
Connect SCL and SDA to the pins below, with suitable pull-up resistors. The defaults depend on the chip, and both are set in menuconfig, as CONFIG_EXAMPLE_I2C_MASTER_SCL and CONFIG_EXAMPLE_I2C_MASTER_SDA.
| Chip | SCL | SDA |
|---|---|---|
| ESP8266 | GPIO5 | GPIO4 |
| ESP32-C3 | GPIO6 | GPIO5 |
| ESP32, ESP32-S2 and ESP32-S3 | GPIO19 | GPIO18 |
To learn more about the sensor itself, its addresses and its accuracy, read the SHT sensor series guide.
Example code
The example sets up the I²C bus, initialises the sensor, and then reads it in a loop. This is the high-level mode, selected by CONFIG_EXAMPLE_SHT3X_DEMO_HL, which is on by default:
float temperature = 0.0f, humidity = 0.0f;
if (sht3x_init(sht3x_address) != ESP_OK) {
printf("Failed to initialize SHT3X sensorn");
return;
}
while (1) {
if (sht3x_read_temperature_humidity(sht3x_address, &temperature, &humidity) == ESP_OK) {
printf("Temperature: %.2f C, Humidity: %.2f %%RHn", temperature, humidity);
} else {
printf("Failed to read data from SHT3X sensorn");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
The I²C bus is set up as a master at 100 kHz, with the internal pull-ups enabled.
Also in the sunshade
The SHT3x is also one of the optional sensors of the ESP32 HomeKit Sunshade, where it shows temperature and humidity in the Home app.
Good to know
- The repository is under the MIT licence.
- A first release, 1.0.0, was made on 24 November 2024, and the component has moved on to version 1.0.8 since.
Everything is in the esp32-sht3x repository on GitHub.
More projects
Keep building.
Electronics, 3D printing and CNC builds, documented from the first idea to the last screw.
All projects
