ESP32 with SHT41: Get Temperature and Humidity (Arduino IDE)

Learn how to interface the SHT41 temperature and humidity sensor with the ESP32 programmed using the Arduino IDE. This is one of the sensors with the best price performance ratio on the market. It is part of Sensirion’s 4th generation SHT4x series, a high accuracy, low power sensor with an I2C interface.

We’ll show you a quick introduction to the sensor, how to wire it to the ESP32, and program it to get sensor data. This guide is also compatible with the SHT40 and SHT45 sensors.

ESP32 with SHT41: Get Temperature and Humidity (Arduino IDE)

New to the ESP32? Check out our eBook: Learn ESP32 with Arduino IDE (eBook).

Table of Contents

In this guide, we’ll cover the following topics:

Introducing the SHT41 Temperature and Humidity Sensor

The SHT41 is a 4th generation, high accuracy, ultra low power temperature and relative humidity sensor from the SHT4x series (by Sensirion).

Check SHT4x Sensor Version SHT41 Temperature Humidity Sensor

It has a typical temperature accuracy of +/-0.2°C and relative humidity accuracy of +/-1.8% RH. The datasheet specifies an average current consumption of 0.4uA. However, when using the sensor on a breakout board like the one in our case, the power consumption will be higher. The sensor is factory calibrated. Here’s the sensor specs:

  • Temperature accuracy: +/-0.2°C
  • Humidity accuracy: +/- 1.8%RH
  • Temperature range: -40 to 125°C
  • Humidity range: 0 to 100 %RH
  • Supply voltage: 1.08 to 3.6V
  • Interface: I2C
  • I2C address: 0x44
  • Average current: 0.4uA (bare chip)

The sensor also comes with a power controlled internal heater that can be used at three heating levels to enable sensor operation in demanding environments. For example, it can help prevent condensation on the sensor in high humidity conditions.

Interfacing this sensor with the ESP32 or any other microcontroller is quite straightforward, and it uses the I2C communication protocol.

For our projects, it’s easier to use the SHT41 on a breakout board. There are different breakout boards available with the SHT4x sensor series. We’re using the one shown in the next picture.

SHT41 Temperature Humidity Sensor Overview Pinout Connectors

In our case, it comes with two STEMMA QT / Qwiic compatible connectors for I2C. One is used to connect the board to the microcontroller, and the other can be used to connect additional I2C sensors or modules if needed for your project. There’s also the possibility of soldering header pins and connecting “regular” jumper wires or using a breadboard.

Where to Buy?

You can check our Maker Advisor Tools Page to compare the SHT41 sensor module price in different stores.

This guide is also compatible with:

SHT40 vs SHT41 VS SHT45

The SHT40, SHT41, and SHT45 all belong to the same fourth generation of temperature and relative humidity sensors by Sensirion. They work in a similar manner, and the code is the same.

SHT4x Sensor Versions SHT40 SHT41 SHT45

They differ in terms of accuracy. The SHT40 and SHT41 have similar typical accuracy (the SHT41 maintains that accuracy better when humidity gets closer to the extremes), while the SHT45 offers higher accuracy.

SHT40SHT41SHT45
Typical Temperature Accuracy+/-0.2 °C+/-0.2 °C+/-0.1 °C
Typical Humidity Accuracy+/-1.8 %RH+/-1.8 %RH+/-1.0 %RH
Temperature Range−40 to 125 °C−40 to 125 °C−40 to 125 °C
Humidity Range0 to 100 %RH0 to 100 %RH0 to 100 %RH
Supply Voltage1.08 to 3.6 V1.08 to 3.6 V1.08 to 3.6 V
InterfaceI2CI2CI2C
Average Current0.4uA0.4uA0.4uA
Temperature Response Time2s2s2s
Humidity Tesponse Time4s4s4s
Where to BuyBuy SHT40Buy SHT41Buy SHT45

SHT4x Pinout

All three sensors SHT40, SHT41, and SHT45 communicate via I2C protocol. The pinout is the same for all of them.

ESP32 with SHT41 Temperature Humidity Sensor Circuit
  • VIN: power pins. Most breakout boards come with a voltage regulator. So you can connect the same power logic level as your microcontroller (for example, 5V for Arduino and 3.3V for the ESP32)
  • 3Vo: the output from the voltage regulator (3.3V). Some breakout boards don’t have this pin
  • GND: GND pin
  • SCL: I2C clock pin
  • SDA: I2C data pin

Wiring the SHT4x to the ESP32

You can connect the SHT41 to the ESP32 using the connector if your board comes with one, or you can connect it to the ESP32’s default I2C pins.

SHT41ESP32 DevKitESP32-S3
VIN3V33V3
3VoDo not connectDo not connect
GNDGNDGND
SCLGPIO 22GPIO 9
SDAGPIO 21GPIO 8

Parts Required:

The ESP32 I2C pins might be different depending on the ESP32 board model. So, adjust accordingly. Recommended reading:

ESP32-S3

ESP32-S3 Devkit SHT41 Sensor Circuit Wiring Diagram SHT40 SHT45

ESP32 Devkit

ESP32 Devkit SHT41 Sensor Circuit Wiring Diagram SHT40 SHT45

Preparing Arduino IDE

Before proceeding, make sure you have the Arduino IDE installed and the ESP32 boards. Follow the next tutorial if you haven’t already:

Installing the SHT4x Library

There are several libraries to interface with the SHT41 sensor. We’ll use the Adafruit SHT4x library. It is compatible with SHT40, SHT41, and SHT45 sensors. Alternatively, you can also use the library provided by Sensirion.

In your Arduino IDE, open the Library Manager. Search for adafruit sht4x and install the Adafruit SHT4x Library by Adafruit (install any other recommended dependencies)

install sht4x adafruit library arduino IDE

Code – ESP32 with SHT41: Get Temperature and Relative Humidity

The library provides a simple example showing how to get temperature and relative humidity from the sensor. The code is already quite simple and straightforward, so here we’re using the original example provided by the library (with a few modifications). It is compatible with the SHT40, SHT41, and SHT45.

This example shows how to get temperature and relative humidity, how to select different precisions (three different options), and how to select different settings to use the internal heater if needed.

/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-sht41-temperature-humidity-arduino/
  Based on the example provided by the Adafruit_SHT4x Library
*/
#include "Adafruit_SHT4x.h"

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

void setup() {
  Serial.begin(115200);

  Serial.println("SHT4x test");
  if (!sht4.begin()) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(1);
  }
  Serial.println("Found SHT4x sensor");
  Serial.print("Serial number 0x");
  Serial.println(sht4.readSerial(), HEX);

  // You can have 3 different precisions, higher precision takes longer
  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  switch (sht4.getPrecision()) {
     case SHT4X_HIGH_PRECISION: 
       Serial.println("High precision");
       break;
     case SHT4X_MED_PRECISION: 
       Serial.println("Med precision");
       break;
     case SHT4X_LOW_PRECISION: 
       Serial.println("Low precision");
       break;
  }

  // You can have 6 different heater settings higher heat and 
  // longer times uses more power and reads will take longer too
  sht4.setHeater(SHT4X_NO_HEATER);
  switch (sht4.getHeater()) {
     case SHT4X_NO_HEATER: 
       Serial.println("No heater");
       break;
     case SHT4X_HIGH_HEATER_1S: 
       Serial.println("High heat for 1 second");
       break;
     case SHT4X_HIGH_HEATER_100MS: 
       Serial.println("High heat for 0.1 second");
       break;
     case SHT4X_MED_HEATER_1S: 
       Serial.println("Medium heat for 1 second");
       break;
     case SHT4X_MED_HEATER_100MS: 
       Serial.println("Medium heat for 0.1 second");
       break;
     case SHT4X_LOW_HEATER_1S: 
       Serial.println("Low heat for 1 second");
       break;
     case SHT4X_LOW_HEATER_100MS: 
       Serial.println("Low heat for 0.1 second");
       break;
  }
}

void loop() {
  sensors_event_t humidity, temp;
  
  uint32_t timestamp = millis();
  // Get the latest temperature and humidity readings
  sht4.getEvent(&humidity, &temp);
  timestamp = millis() - timestamp;

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

  Serial.print("Humidity: "); 
  Serial.print(humidity.relative_humidity);
  Serial.println("% rH");
  
  // Convert temperature to Fahrenheit
  float tempF = 1.8 * temp.temperature + 32;
  Serial.print("Temperature: ");
  Serial.print(tempF);
  Serial.println(" degrees F");

  Serial.print("Read duration (ms): ");
  Serial.println(timestamp);
  Serial.println(" ");

  delay(1000);
}

View raw code

How Does the Code Work?

Start by including the Adafruit_SHT4x library.

#include "Adafruit_SHT4x.h"

Create an Adafruit_SHT4x object called sht4 to refer to the sensor.

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

In the setup(), initialize the Serial monitor.

void setup() {
  Serial.begin(115200);

The following lines initialize the sensor.

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

Every SHT4x sensor comes with a unique serial number. You can get it using the readSerial() function on the sht4 object as shown in the following lines.

Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);

As we mentioned previously, you can select different precisions to get data from the sensor. Take into account that higher precision takes longer. Choose according to the needs of your project.

You can choose from these options:

  • SHT4X_HIGH_PRECISION
  • SHT4X_MED_PRECISION
  • SHT4X_LOW_PRECISION

We’re selecting high precision.

sht4.setPrecision(SHT4X_HIGH_PRECISION);

The code then shows how to set the heater settings. There are six possible options:

  • SHT4X_NO_HEATER
  • SHT4X_HIGH_HEATER_1S
  • SHT4X_HIGH_HEATER_100MS
  • SHT4X_MED_HEATER_1S
  • SHT4X_MED_HEATER_100MS
  • SHT4X_LOW_HEATER_1S
  • SHT4X_LOW_HEATER_100MS

In the example, we’re not using the heater.

sht4.setHeater(SHT4X_NO_HEATER);

In the loop() is where we’ll get the temperature readings.

We need to create objects of type sensor_event_t where we’ll save the temperature and humidity readings.

sensors_event_t humidity, temp;

The code also calculates how much time it takes to get readings. It is saved in the timestamp variable.

uint32_t timestamp = millis();
(...)
timestamp = millis() - timestamp;

To populate the temp and humidity objects with new data from the sensor, we need to do as follows:

sht4.getEvent(&humidity, &temp);

Finally, after getting the temperature and humidity in the corresponding objects, we can get them by calling temp.temperature and humidity.relative_humidity as shown below (the readings are printed in the Serial Monitor):

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

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

We also have the option to print the temperature in Fahrenheit.

float tempF = 1.8 * temp.temperature + 32;
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" degrees F");

We also print how long it takes to get new readings. If you change the precision and heater definitions, you’ll see that it changes.

Serial.print("Read duration (ms): ");
Serial.println(timestamp);

Demonstration

Upload the previous code to your ESP32 board. Make sure you select the right board and COM port.

After uploading, open the Serial Monitor at a baud rate of 115200 and press the RST button.

After a few seconds, it should start displaying the current temperature and relative humidity, as well as the time it takes to get readings.

ESP32 Get Temperature and Humidity from SHT41 Sensor - display on Serial Monitor

ESP32 with SHT41: Display Temperature and Relative Humidity on OLED Display

The library also comes with another example to display the sensor readings on the OLED display.

ESP32 with SHT41 Temperature Humidity Sensor OLED Arduino IDE Circuit

Parts Required:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring Diagrams

The OLED display also uses I2C communication protocol. You can connect it to the same I2C pins.

ESP32-S3

ESP32S3 connected to the SHT41 humidity and temperature sensor and an OLED display SHT40 SHT45

ESP32 Devkit

ESP32 Devkit connected to the SHT41 humidity and temperature sensor and an OLED display SHT40 SHT45

Install Required Libraries

For this project, you also need to install the following libraries if you haven’t already:

You can install them by searching for their names in the Arduino IDE Library Manager and you must also install all other suggested dependencies.

adafruit SHT4X install dependencies

Code – Display SHT41 Readings on OLED Display

The following code displays the temperature and relative humidity readings on the OLED display. The code was slightly modified from the example provided by the library.

/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-sht41-temperature-humidity-arduino/
  Based on the example provided by the Adafruit_SHT4x Library
*/
#include <Adafruit_SH110X.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_SHT4x.h"
#include <Fonts/FreeSans9pt7b.h>

#define SCREEN_WIDTH 128    // OLED display width (pixels)
#define SCREEN_HEIGHT 64    // OLED display height (pixels)

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1   // Reset pin (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Declaration for the SHT4X connected to the default I2C pins
Adafruit_SHT4x sht4 = Adafruit_SHT4x();

void setup() {
  Serial.begin(115200);

  // Initialize OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    for (;;);
  }

  // Clear the display
  display.clearDisplay();
  display.display();
  
  // Initialize SHT4x sensor
  Serial.println("Initializing SHT4x");
  if (! sht4.begin(&Wire)) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(1);
  }
  Serial.println("Found SHT4x sensor");
  Serial.print("Serial number 0x");
  Serial.println(sht4.readSerial(), HEX);

  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  sht4.setHeater(SHT4X_NO_HEATER);
  
  display.setFont(&FreeSans9pt7b);
  display.setTextColor(SSD1306_WHITE);  
}

void loop() {
  sensors_event_t humidity, temp;
  
  uint32_t timestamp = millis();
  // Get the latest temperature and humidity readings
  sht4.getEvent(&humidity, &temp);
  // Calculate the time it takes to get a new reading
  timestamp = millis() - timestamp;

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

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

  // Convert temperature to Fahrenheit
  float tempF = 1.8 * temp.temperature + 32;
  Serial.print("Temperature: ");
  Serial.print(tempF);
  Serial.println(" degrees F");

  Serial.print("Read duration (ms): ");
  Serial.println(timestamp);

  display.clearDisplay();
  display.setCursor(0, 20);
  display.println("SHT41 Sensor");
  display.setCursor(0, 40);

  display.print("Temp: "); 
  display.print(temp.temperature,1); 
  display.println(" C");

  // Display temperature in Fahrenheit
  //display.print("Temp: "); 
  //display.print(tempF,1);
  //display.println(" F");

  display.print("Hum: "); 
  display.print(humidity.relative_humidity,1); 
  display.println(" %");
  display.display();
  yield();
  delay(100);
}

View raw code

If you’ve ever worked with an SSD1306 OLED display, the code is quite easy to understand. To learn more about how to use and program an OLED display with the ESP32, check out our guide:

Demonstration

After uploading the code, the OLED will display the current temperature and relative humidity as shown in the picture below.

ESP32 with SHT41 Temperature Humidity Sensor OLED Arduino IDE Demonstration

Wrapping Up

In this guide, you learned how to use the SHT41 temperature and relative humidity sensor with the ESP32 programmed with Arduino IDE. This tutorial is also compatible with SHT40 and SHT45. The wiring and code are exactly the same.

The SHT41 is one of the best sensors when it comes to price performance ratio, so it is a great choice for your hobbyist IoT and home automation projects.

We have tutorials for other popular temperature/humidity sensors:

We hope you’ve found this guide useful. What other guides/tutorials would you like to see covering the SHT4x sensors? Let us know in the comments section.

If you want to learn more about the ESP32, check out our resources:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.