ESP32 SHT30 Temperature and Humidity Sensor
The SHT30 sensor is a high-precision digital temperature and humidity sensor that utilizes Sensirion's CMOSens® technology. It provides fully calibrated, linearized, and temperature-compensated digital output, making it ideal for applications requiring precise and reliable environmental measurements.
Jump to Code Examples
Quick Links
SHT30 Price
About SHT30 Temperature and Humidity Sensor
The SHT30 is a digital temperature and humidity sensor developed by Sensirion. It offers high reliability and long-term stability, making it suitable for various applications, including HVAC systems, data loggers, and weather stations. The SHT30 provides accurate measurements with a typical humidity accuracy of ±2% RH and a temperature accuracy of ±0.3°C.SHT30 Sensor Technical Specifications
Below you can see the SHT30 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.3°C
- Humidity Accuracy: ±2% RH
- Interface: I2C
- Dimensions: 2.5mm x 2.5mm x 0.9mm
SHT30 Sensor Pinout
Below you can see the pinout for the SHT30 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 SHT30 pinout is as follows:
- VDD: Power supply voltage (2.4V to 5.5V).
- 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 SHT30 Temperature and Humidity Sensor with ESP32 in several frameworks:
ESP32 SHT30 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the SHT30 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
if (!sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
}
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (!isnan(t) && !isnan(h)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.print(t); Serial.print(" ");
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read from SHT31 sensor");
}
delay(1000);
}
This Arduino sketch demonstrates how to interface with the SHT30 sensor using the Adafruit SHT31 library. It initializes the sensor and reads temperature and humidity data every second, printing the results to the Serial Monitor. The sensor's I2C address is set to 0x44 by default; if your sensor uses the alternate address (0x45), adjust the initialization accordingly.
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.
ESP32 SHT30 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the SHT30 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 SHT30_SENSOR_ADDR 0x44 /*!< SHT30 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_sht30_sensor() {
uint8_t data[6];
i2c_master_write_read_device(I2C_MASTER_NUM, SHT30_SENSOR_ADDR, NULL, 0, data, sizeof(data), 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_sht30_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code demonstrates how to interface with the SHT30 sensor using the I2C interface. It initializes the I2C master on GPIO21 (SDA) and GPIO22 (SCL) and reads temperature and humidity data from the SHT30. The sensor's raw data is processed and converted into human-readable temperature and humidity values. The readings are 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.
ESP32 SHT30 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the SHT30 Temperature and Humidity Sensor
i2c:
sda: GPIO21
scl: GPIO22
scan: true
sensor:
- platform: sht3x
address: 0x44
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
update_interval: 60s
The ESPHome configuration uses the `sht3x` platform for interfacing with the SHT30 sensor. The `i2c` section specifies the SDA (GPIO21) and SCL (GPIO22) pins, enabling communication with the sensor. The `address` key indicates the I2C address of the SHT30 sensor (default `0x44`). Two sensor entities are defined: one for temperature and one for humidity, with user-friendly names such as 'Room Temperature' and 'Room Humidity.' The readings are updated every 60 seconds.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 SHT30 PlatformIO Code Example
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 SHT31 Library @ ^1.1.0
monitor_speed = 115200
ESP32 SHT30 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the SHT30 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(115200);
if (!sht31.begin(0x44)) {
Serial.println("Couldn't find SHT30 sensor. Check wiring!");
while (1);
}
Serial.println("SHT30 initialized.");
}
void loop() {
float temperature = sht31.readTemperature();
float humidity = sht31.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println("Failed to read from SHT30 sensor!");
}
delay(2000);
}
This PlatformIO code initializes the SHT30 sensor using the Adafruit library. The setup checks for the sensor's presence at the default I2C address `0x44`. If successful, the loop continuously reads temperature and humidity values, printing them to the Serial Monitor every 2 seconds. Any errors in reading are reported in the console.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 SHT30 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the SHT30 Temperature and Humidity Sensor with your ESP32.
from machine import Pin, I2C
from time import sleep
import sht31
# Initialize I2C (SDA=21, SCL=22)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
sensor = sht31.SHT31(i2c)
print("SHT30 Sensor Example")
while True:
temperature, humidity = sensor.measure()
print("Temperature: {:.2f} °C".format(temperature))
print("Humidity: {:.2f} %".format(humidity))
sleep(2)
This MicroPython code uses the `sht31` library to interface with the SHT30 sensor. The I2C bus is initialized on GPIO21 (SDA) and GPIO22 (SCL). The `sensor.measure()` function retrieves temperature and humidity readings, which are formatted and printed to the console every 2 seconds. The code is lightweight and ideal for microcontroller projects.
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 SHT30 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT30 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.