Learn how to interface the SHT41 with the ESP32/ESP8266 programmed with MicroPython. This is also compatible with other SHT4x sensors (SHT40, and SHT45). The SHT41 is part of Sensirion’s fourth-generation SHT4x series, a high-accuracy, ultra-low-power sensor with an I2C interface.

New to MicroPython? Check out our eBook: MicroPython Programming with ESP32 and ESP8266 eBook (2nd Edition)
Prerequisites
To follow this tutorial, you need MicroPython firmware installed on your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:
- Thonny IDE:
- uPyCraft IDE:
- Getting Started with uPyCraft IDE
- Install uPyCraft IDE (Windows, Mac OS X, Linux)
- Flash/Upload MicroPython Firmware to ESP32 and ESP8266
Introducing the SHT41 Temperature and Humidity Sensor
The SHT41 is a fourth-generation, high-accuracy, ultra-low-power relative humidity and temperature sensor from the SHT4x series (by Sensirion).

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.4 uA. 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.
- 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)
It 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 SHT41 sensor. We’re using the one shown in the picture.

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.
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.

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.
| SHT40 | SHT41 | SHT45 | |
| Typical Temperature Accuracy | +/-0.2 °C | +/-0.2 °C | +/-0.1 °C |
| Typical Humidity Accuracy | +/-1.8 %RH | +/-1.8 %RH | +/-1.0 %RH |
| Humidity accuracy (max. error) | +/-3.5 %RH | +/-2.5 %RH | +/-2.0 %RH |
| Temperature Range | −40 to 125 °C | −40 to 125 °C | −40 to 125 °C |
| Humidity Range | 0 to 100 %RH | 0 to 100 %RH | 0 to 100 %RH |
| Supply Voltage | 1.08 to 3.6 V | 1.08 to 3.6 V | 1.08 to 3.6 V |
| Interface | I2C | I2C | I2C |
| Average current | 0.4uA | 0.4uA | 0.4uA |
| Temperature response time | 2s | 2s | 2s |
| Humidity response time | 4s | 4s | 4s |
Pinout
All three sensors SHT40, SHT41, and SHT45 communicate via I2C protocol. The pinout is the same for all of them.
- 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.
- SCL: I2C clock pin
- SDA: I2C data pin
Wiring the SHT4x to the ESP32/ESP8266
You can connect the SHT41 to the ESP32 using the connector if your board comes with one, or you can connect it to the ESP default I2C pins.
| SHT41 | ESP32 DevKit | ESP32-S3 | ESP8266 |
| GND | GND | GND | GND |
| VCC | 3V3 | 3V3 | 3V3 |
| SDA | GPIO 21 | GPIO 8 | GPIO 4 |
| SCL | GPIO 22 | GPIO 9 | GPIO 5 |
| 3Vo | don’t connect | don’t connect | don’t connect |
The ESP I2C pins might be different depending on the board model. So, adjust accordingly.
Recommended reading:
- ESP32-S3 DevKitC Pinout Reference Guide: GPIOs Explained
- ESP32 Pinout Reference: Which GPIO pins should you use?
- ESP8266 Pinout Reference: Which GPIO pins should you use?
ESP32-S3

ESP8266

SHT4x MicroPython Module
To make it easier to interface with the SHT41, we’ll use a MicroPython module adapted from the SHT4x CircuitPython module.
Follow the next steps to install it.
- Click here to download our version of the sht4x.py file.
- Copy the code to a file in Thonny IDE;
- Go to File > Save as… and select MicroPython Device;
- Save the file with the name sht4x.py (don’t change the name).

# MicroPython driver for Sensirion SHT4x (SHT40 / SHT41 / SHT45)
# https://randomnerdtutorials.com/micropython-esp32-esp8266-sht41/
# Adapted from Adafruit CircuitPython SHT4x API: https://docs.circuitpython.org/projects/sht4x/en/stable/api.html
from micropython import const
import time
import struct
_SHT4X_DEFAULT_ADDR = const(0x44)
_SHT4X_READSERIAL = const(0x89)
_SHT4X_SOFTRESET = const(0x94)
class Mode:
"""Measurement modes (heater + precision)"""
NOHEAT_HIGHPRECISION = 0xFD
NOHEAT_MEDPRECISION = 0xF6
NOHEAT_LOWPRECISION = 0xE0
HIGHHEAT_1S = 0x39
HIGHHEAT_100MS = 0x32
MEDHEAT_1S = 0x2F
MEDHEAT_100MS = 0x24
LOWHEAT_1S = 0x1E
LOWHEAT_100MS = 0x15
string = {
NOHEAT_HIGHPRECISION: "No heater, high precision",
NOHEAT_MEDPRECISION: "No heater, med precision",
NOHEAT_LOWPRECISION: "No heater, low precision",
HIGHHEAT_1S: "High heat, 1 second",
HIGHHEAT_100MS: "High heat, 0.1 second",
MEDHEAT_1S: "Med heat, 1 second",
MEDHEAT_100MS: "Med heat, 0.1 second",
LOWHEAT_1S: "Low heat, 1 second",
LOWHEAT_100MS: "Low heat, 0.1 second",
}
delay = {
NOHEAT_HIGHPRECISION: 0.010,
NOHEAT_MEDPRECISION: 0.005,
NOHEAT_LOWPRECISION: 0.002,
HIGHHEAT_1S: 1.100,
HIGHHEAT_100MS: 0.110,
MEDHEAT_1S: 1.100,
MEDHEAT_100MS: 0.110,
LOWHEAT_1S: 1.100,
LOWHEAT_100MS: 0.110,
}
@classmethod
def is_valid(cls, value):
return value in cls.string
class SHT4x:
def __init__(self, i2c, address=_SHT4X_DEFAULT_ADDR):
self.i2c = i2c
self.address = address
self._buffer = bytearray(6)
self.reset()
self._mode = Mode.NOHEAT_HIGHPRECISION
def _write_command(self, cmd):
self.i2c.writeto(self.address, bytes([cmd]))
def _read_data(self, length=6):
return self.i2c.readfrom(self.address, length)
@property
def serial_number(self):
"""Unique 32-bit serial number"""
self._write_command(_SHT4X_READSERIAL)
time.sleep_ms(10)
data = self._read_data(6)
ser1 = data[0:2]
ser1_crc = data[2]
ser2 = data[3:5]
ser2_crc = data[5]
if ser1_crc != self._crc8(ser1) or ser2_crc != self._crc8(ser2):
raise RuntimeError("Invalid CRC calculated")
return (ser1[0] << 24) | (ser1[1] << 16) | (ser2[0] << 8) | ser2[1]
def reset(self):
"""Soft reset – restores power-on defaults"""
self._write_command(_SHT4X_SOFTRESET)
time.sleep_ms(1)
@property
def mode(self):
return self._mode
@mode.setter
def mode(self, new_mode):
if not Mode.is_valid(new_mode):
raise ValueError("mode must be a Mode constant")
self._mode = new_mode
@property
def relative_humidity(self):
"""Relative humidity in %RH (0–100)"""
return self.measurements[1]
@property
def temperature(self):
"""Temperature in degrees Celsius"""
return self.measurements[0]
@property
def temperature_f(self):
"""Temperature in degrees Fahrenheit"""
return self.temperature * 9 / 5 + 32
@property
def measurements(self):
"""Returns a tuple (temperature °C, relative humidity %RH)"""
command = self._mode
self._write_command(command)
time.sleep(Mode.delay[self._mode])
data = self._read_data(6)
temp_data = data[0:2]
temp_crc = data[2]
humidity_data = data[3:5]
humidity_crc = data[5]
if temp_crc != self._crc8(temp_data) or humidity_crc != self._crc8(humidity_data):
raise RuntimeError("Invalid CRC calculated")
temp_raw = struct.unpack(">H", temp_data)[0]
temperature = -45.0 + 175.0 * temp_raw / 65535.0
hum_raw = struct.unpack(">H", humidity_data)[0]
humidity = -6.0 + 125.0 * hum_raw / 65535.0
humidity = max(0.0, min(100.0, humidity))
return (temperature, humidity)
@staticmethod
def _crc8(data):
crc = 0xFF
for byte in data:
crc ^= byte
for _ in range(8):
if crc & 0x80:
crc = (crc << 1) ^ 0x31
else:
crc <<= 1
return crc & 0xFF
With the module loaded to the board, you can now use the library functionalities in your code to interface with the SHT41 sensor (or other SHT4x sensors).
SHT41: Getting Temperature and Humidity (MicroPython)
The following code is a simple example showing how to initialize the sensor and get temperature and humidity readings.
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/micropython-esp32-esp8266-sht41/
from machine import SoftI2C, Pin
import time
from sht4x import SHT4x, Mode
# Create I2C bus (adjust pins for your board)
i2c = SoftI2C(scl=Pin(9), sda=Pin(8), freq=100000) # ESP32-S3 example
#i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000) # ESP32 example
#i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100000) # ESP8266 example
sht = SHT4x(i2c)
print("Serial number:", hex(sht.serial_number))
# Optional: change measurement mode
sht.mode = Mode.NOHEAT_HIGHPRECISION
# sht.mode = Mode.LOWHEAT_100MS # example with heater
print("Current mode:", Mode.string[sht.mode])
while True:
temp_c = sht.temperature
temp_f = sht.temperature_f
rh = sht.relative_humidity
print(f"Temperature: {temp_c:.1f} °C")
print(f"Temperature: {temp_f:.1f} °F")
print(f"Humidity: {rh:.1f} %")
print()
time.sleep(10)
Now, let’s take a quick look at how the code works to understand how to use the library functions to get data from the SHT41 sensor. This is also compatible with other SHT4x sensors.
Start by including the required libraries.
from machine import I2C, Pin
import time
from sht4x import SHT4x, Mode
Start an I2C bus on the ESP32 I2C pins. We’re using an ESP32-S3. Adjust for the board model you’re using.
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000) # ESP32-S3 example
Initialize the sensor.
sht = SHT4x(i2c)
Every SHT4x sensor comes with a unique serial number. You can get it using the serial_number() function on the sht object, as shown below.
print("Serial number:", hex(sht.serial_number))
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:
- NOHEAT_HIGHPRECISION
- NOHEAT_MEDPRECISION
- NOHEAT_LOWPRECISION
We’re selecting high precision.
sht.mode = Mode.NOHEAT_HIGHPRECISION
The code then shows how to set the heater settings. There are six possible options (we’re not using the heater, but it is commented to show how to use it):
# sht.mode = Mode.LOWHEAT_100MS # example with heater
These are the options:
- HIGHHEAT_1S
- HIGHHEAT_100MS
- MEDHEAT_1S
- MEDHEAT_100MS
- LOWHEAT_1S
- LOWHEAT_100MS
To check what the current settings are, you can use the following line.
print("Current mode:", Mode.string[sht.mode])
Getting temperature and humidity is as easy as using the following methods:
temp_c = sht.temperature
temp_f = sht.temperature_f
rh = sht.relative_humidity
Finally, we print the results in the MicroPython Shell:
print(f"Temperature: {temp_c:.1f} °C")
print(f"Temperature: {temp_f:.1f} °F")
print(f"Humidity: {rh:.1f} %")
print()
time.sleep(10)
Demonstration
Run or upload (File > Save as > Micropython device > save the file with the name main.py) the code to your board using Thonny IDE.

Don’t forget that you must have uploaded the sht4x.py library previously.
In the MicroPython Shell, the current temperature and relative humidity readings are displayed.

ESP32/ESP8266 with SHT41: Display Data on OLED Display (MicroPython)
After learning how to get data from the sensor, it’s easy to build a simple project with an OLED to display the temperature and relative humidity.

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

Upload OLED MicroPython Module
For this project, we’ll use the Adafruit SSD1306 MicroPython module to interface with the OLED display. Follow the next instructions:
1. Create a new file in Thonny IDE and copy the library code. The OLED library code can be found here.
2. Go to File > Save as and select MicroPython device.

3. Name the file ssd1306.py and click OK to save the file on the ESP Filesystem.
And that’s it. The library was uploaded to your board. Now, you can use the library functionalities in your code by importing the library.
At this moment, you should have the ssd1306.py and sht4x.py files in you board filesystem.

Code
The following code displays the temperature and relative humidity readings on the OLED display.
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/micropython-esp32-esp8266-sht41/
from machine import Pin, SoftI2C
from time import sleep
import ssd1306
from sht4x import SHT4x, Mode
# ESP32 default pins
i2c = SoftI2C(scl=Pin(9), sda=Pin(8), freq=100000) # ESP32-S3
#i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000) # ESP32 example
# i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100000) # ESP8266 example
# OLED setup
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# SHT4x sensor
sht = SHT4x(i2c)
sht.mode = Mode.NOHEAT_HIGHPRECISION
print("Serial number:", hex(sht.serial_number))
while True:
temp_c = sht.temperature
temp_f = sht.temperature_f
rh = sht.relative_humidity
oled.fill(0)
oled.text("SHT41 Sensor", 0, 0)
# Temperature Celsius
oled.text("Temp: {:.1f} C".format(temp_c), 0, 16)
# Uncomment for fahrenheit
# oled.text("Temp: {:.1f} F".format(temp_f), 0, 28)
# Humidity
oled.text("Hum: {:.1f} %".format(rh), 0, 28)
# Update the OLED
oled.show()
print(f"Temperature: {temp_c:.1f} °C")
print(f"Temperature: {temp_f:.1f} °F")
print(f"Humidity: {rh:.1f} %")
print()
sleep(2)
If you’ve worked with an SSD1306 OLED display before, the code is quite easy to understand. Learn more about how to use and program an SSD1306 OLED display with the ESP boards using MicroPython:
Demonstration
Now, to test this example:
1. Copy the code to a file in Thonny IDE;
2. Go to File > Save as…

3. Select save to “MicroPython“ device:

4. Save the file with the name main.py (don’t change the name). When you upload code named main.py, it runs automatically when the board starts.
Don’t forget that you must have uploaded the ssd1306.py, and sht4x.py libraries first.
You’ll get the current temperature in Celsius and relative humidity on the OLED display.

Note: if you want this project running when it’s not connected to Thonny IDE, you must upload the code to your board with the name main.py (running it with the green Run button just runs the code and doesn’t upload it to the board).
If you want to display the temperature in Fahrenheit, uncomment the following lines in the code and comment the ones referring to Celsius degrees.
# Uncomment for fahrenheit
# oled.text("Temp: {:.1f} F".format(temp_f), 0, 28)
Wrapping Up
In this tutorial, you learned how to interface the SHT41 (or SHT40 or SHT45) with the ESP32/ESP8266 programmed with MicroPython.
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. You can follow this guide to program the SHT4x with the ESP32 using Arduino IDE.
We have more guides for other temperature and humidity sensors that you may like:
- MicroPython: DS18B20 Temperature Sensor with ESP32 and ESP8266
- MicroPython: ESP32/ESP8266 with DHT11/DHT22 Temperature and Humidity Sensor
- MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)
- MicroPython: BME680 with ESP32 and ESP8266 (Temperature, Humidity, Pressure, Gas)
- Comparison Guide: Temperature and Humidity Sensors for ESP32, RPi Pico and Similar Boards
Learn more about MicroPython with our resources:




