ESP32 SHT40 Temperature and Humidity Sensor
Overview
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.
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. 🚀
Technical Specifications
Pinout Configuration
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.
Wiring with ESP32
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.
Troubleshooting Guide
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.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
#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.
ESP-IDF Example
#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.
ESPHome Example
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
.
PlatformIO Example
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit SHT4x Library
wire
monitor_speed = 115200
PlatformIO Example Code
#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.
Conclusion
The ESP32 SHT40 Temperature and Humidity Sensor is a powerful environment sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.