Skip to main content
ESPBoards

ESP32 SHT40 Temperature and Humidity Sensor

The SHT40 is a high-accuracy digital temperature and humidity sensor with a compact design and low power consumption. Its I²C interface and wide operating voltage range make it ideal for various environmental monitoring applications.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image

🔗 Quick Links

SHT40 Temperature and Humidity Sensor Datasheet ButtonSHT40 Temperature and Humidity Sensor Specs ButtonSHT40 Temperature and Humidity Sensor Specs ButtonSHT40 Temperature and Humidity Sensor Specs Button

ℹ️ About SHT40 Temperature and Humidity Sensor

The SHT40, part of Sensirion’s 4th-generation sensor platform, delivers high-accuracy temperature and humidity measurements with exceptional stability. Its compact design and low operating voltage make it ideal for HVAC systems, data loggers, and environmental monitoring.

Key Features #

  • Superior Accuracy – ±1.8% RH (humidity) and ±0.2°C (temperature).
  • Low Power & Wide Voltage Range – Operates from 1.08V to 3.6V, ideal for battery-powered devices.
  • I²C Communication – Simple integration with ESP32, Arduino, and embedded systems.
  • Compact & Reliable – Designed for long-term environmental sensing applications.

With its high precision and energy efficiency, the SHT40 is an excellent choice for next-generation climate monitoring solutions. 🚀

⚙️ SHT40 Sensor Technical Specifications

Below you can see the SHT40 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: I2C
  • Interface: I²C
  • Operating Voltage: 1.08V to 3.6V
  • Temperature Range: -40°C to +125°C
  • Temperature Accuracy: ±0.2°C
  • Humidity Range: 0% to 100% RH
  • Humidity Accuracy: ±1.8% RH
  • Resolution: 16-bit
  • Power Consumption: 0.4 µA (at 1 Hz measurement rate)
  • Response Time: Temperature: 2s (τ63%); Humidity: 4s (τ63%)
  • Dimensions: 1.5mm x 1.5mm x 0.5mm

🔌 SHT40 Sensor Pinout

Below you can see the pinout for the SHT40 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 SHT40 sensor has the following pins: VDD: Power supply (1.08V to 3.6V). GND: Ground. SDA: Serial Data line for I²C communication. SCL: Serial Clock line for I²C communication.

🧵 SHT40 Wiring with ESP32

Below you can see the wiring for the SHT40 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 SHT40 sensor to an ESP32: VDD: Connect to 3.3V on the ESP32. GND: Connect to GND on the ESP32. SDA: Connect to GPIO21 (default SDA). SCL: Connect to GPIO22 (default SCL). Note: Ensure that appropriate pull-up resistors (typically 10kΩ) are present on the SDA and SCL lines if not already included on the sensor module.

🛠️ SHT40 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.

❌ Sensor Initialization Failure

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

Solution: Ensure that the SDA and SCL lines are correctly connected to the corresponding GPIO pins on the microcontroller. Verify that the power supply voltage is within the specified range (1.08V to 3.6V). Check for proper pull-up resistors on the SDA and SCL lines if required by your specific setup. Confirm that the I²C address used in your code matches the sensor's default address (0x44).

⚠️ Incorrect Temperature or Humidity 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.

🔄 Intermittent I²C Communication

Issue: Communication with the sensor is intermittent or fails.

Solution: Verify that the correct I²C address (0x44) is used in your code. Ensure that the timing requirements for the I²C signals are met. Check the integrity of the SDA and SCL 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 SHT40 Temperature and Humidity Sensor with ESP32 in several frameworks:

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

Arduino Core Image

ESP32 SHT40 Arduino IDE Code Example

Example in Arduino IDE

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

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

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

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

void loop() {
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity.relative_humidity);
Serial.println(" %");
delay(2000);
}

This Arduino code initializes the SHT40 sensor using the Adafruit SHT4x library. In the setup() function, it sets up serial communication and attempts to initialize the sensor. If the sensor is not found, it prints an error message and halts execution. In the loop() function, it reads temperature and humidity data from the sensor and prints the values to the Serial Monitor every two seconds.

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

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

#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_FREQ_HZ 100000
#define SHT40_ADDR 0x44

static const char *TAG = "SHT40";

void app_main() {
ESP_LOGI(TAG, "Initializing SHT40...");
sht4x_dev_t dev;
sht4x_init(&dev, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO, I2C_MASTER_FREQ_HZ, SHT40_ADDR);

while (1) {
float temperature, humidity;
if (sht4x_read_temperature(&dev, &temperature) == ESP_OK &&
sht4x_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 SHT40 sensor");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}

This ESP-IDF code configures the ESP32 to communicate with the SHT40 sensor over I²C. The sht4x_init function initializes the sensor, while sht4x_read_temperature and sht4x_read_humidity 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 SHT40 ESPHome Code Example

Example in ESPHome (Home Assistant)

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

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

This ESPHome configuration sets up the SHT40 sensor to measure temperature and humidity over I²C. The sensor readings update every two seconds, and the default I²C address is 0x44.

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

PlatformIO Image

ESP32 SHT40 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
wire
monitor_speed = 115200

ESP32 SHT40 PlatformIO Example Code

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

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

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

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

void loop() {
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
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 initializes the SHT40 sensor using the Adafruit SHT4x library. The sensor communicates over I²C, and 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 SHT40 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHT40 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.