Components · 8 min read

SHT31 Arduino Guide: Accurate Temperature and Humidity Sensor

SHT31 Arduino guide: I2C address 0x44/0x45, ±2% RH accuracy, built-in heater, comparison with DHT22, AHT20 and BME280, code for Uno, ESP32 and Pico.

The SHT31 is the choice for Arduino projects that need accurate readings: a digital temperature and humidity sensor from Sensirion that talks over I2C. With a typical accuracy of ±2 % relative humidity and about ±0.2–0.3 °C, it's far ahead of the DHT11 and DHT22, and because it sends a CRC checksum byte with every measurement, you also catch corrupted data. On top of that it has a small built-in heater, so it can dry itself out when condensation soaks it.

In this article we cover the features of the SHT31 and its sibling the SHT30, its two I2C addresses (0x44 and 0x45), wiring to the Arduino Uno, ESP32 and Raspberry Pi Pico, reading it with the Adafruit library and with library-free MicroPython, when to turn on the heater and the waterproof probe version. An accuracy comparison with the DHT22, AHT20 and BME280 is in the table.

What is the SHT31 and how does it work?

The SHT3x family offers three accuracy grades in the same package: the SHT30 is the budget one, the SHT31 the standard one and the SHT35 the most accurate. Inside the chip there's a capacitive humidity sensor and a band-gap temperature sensor; it digitises the readings at 16 bits, corrects them with factory calibration coefficients and sends them over I2C. All you do is send a measurement command, wait about 15 milliseconds and read 6 bytes. The chip runs from 2.15–5.5 V, so it works in both 3.3 V and 5 V systems, and most ready-made modules include the I2C pull-up resistors.

FeatureSHT30SHT31
Humidity accuracy (typical)±2–3 % RH (depending on datasheet revision)±2 % RH
Temperature accuracy (typical)±0.2–0.3 °C±0.2–0.3 °C
Measuring range−40…125 °C, 0–100 % RH−40…125 °C, 0–100 % RH
Resolution0.01 °C / 0.01 % RH0.01 °C / 0.01 % RH
Supply2.15–5.5 V2.15–5.5 V
Measurement time (high repeatability)Up to ~15 msUp to ~15 ms
InterfaceI2C, 0x44 or 0x45I2C, 0x44 or 0x45
ExtrasHeater, CRC, ALERT pinHeater, CRC, ALERT pin

SHT31 vs DHT22, AHT20 and BME280: accuracy comparison

DHT22 temperature and humidity sensor
The DHT22 from the comparison: it uses a single wire and measures every 2 seconds; the SHT31 is faster and more accurate over I2C.
SensorHumidity accuracyTemperature accuracyInterfaceFastest readingNote
DHT11±5 % RH±2 °CSingle wire1 sCheapest, rough readings
DHT22 (AM2302)±2–5 % RH±0.5 °CSingle wire2 sTiming-sensitive protocol
AHT20±2 % RH±0.3 °CI2C 0x38Several per secondCheap, single address
BME280±3 % RH±1 °CI2C 0x76/0x77FastAlso measures pressure, can self-heat
SHT31±2 % RH±0.2–0.3 °CI2C 0x44/0x45Several per secondCRC and heater

The values in the table are the manufacturers' typical figures; placement also decides the real accuracy. The BME280 is popular in weather stations because it also measures pressure, but the heat of the board and the chip itself can push the temperature up by up to a degree. The AHT20 is good for its price but has only one address. Three things make the SHT31 stand out: the CRC, the second address option and the heater. For a general comparison of sensors see Temperature Sensor Comparison, and for a cheaper alternative AHT10 / AHT20.

I2C addresses: 0x44 and 0x45

If the SHT31's ADDR pin is low the address is 0x44; if it's tied to VDD it's 0x45. On most modules this pin is pulled to GND through a resistor, so the default address is 0x44; connect the pin marked ADR to the module's VIN and it switches to 0x45. That lets you put two SHT31s on the same I2C bus and, for example, measure indoors and outdoors at the same time. If you're unsure about the address, check it with an I2C scanner; we explain how I2C works in What Is I2C.

SHT31 wiring: Uno, ESP32 and Pico

SHT31 module pinArduino UnoESP32Raspberry Pi Pico
VIN5V or 3.3V3V33V3 (OUT)
GNDGNDGNDGND
SCLA5GPIO 22GP5
SDAA4GPIO 21GP4
ADROpen (0x44) or VIN (0x45)Open or 3V3Open or 3V3
ALR / ALERTNot usedNot usedNot used
The I2C pull-up resistors on the module are usually tied to VIN. If you power the module from 5 V and connect SDA and SCL to an ESP32 or Pico, the lines are pulled to 5 V and stress the 3.3 V pins. With the ESP32 and Pico, always power the module from the 3V3 pin.

Step by step: your first reading with Arduino

  1. In the Arduino IDE open the Library Manager, search for Adafruit SHT31 and install it; accept Adafruit BusIO when it asks about dependencies.
  2. Wire the module as in the table: VIN to 5V, GND to GND, SDA to A4, SCL to A5.
  3. Upload the code below and open the Serial Monitor at 9600 baud.
  4. Breathe gently on the sensor; the humidity should rise within a few seconds and slowly drop back.
C++ (Arduino IDE)
1#include <Wire.h>
2#include <Adafruit_SHT31.h>
3
4Adafruit_SHT31 sht31;
5
6void setup() {
7  Serial.begin(9600);
8  // Önce 0x44 (ADDR boşta/GND), olmazsa 0x45 (ADDR -> VCC) dene
9  // Try 0x44 first (ADDR floating/GND), then 0x45 (ADDR -> VCC)
10  if (!sht31.begin(0x44) && !sht31.begin(0x45)) {
11    Serial.println("SHT31 bulunamadi / not found");
12    while (true) delay(10);
13  }
14  Serial.print("Isitici / heater: ");
15  Serial.println(sht31.isHeaterEnabled() ? "acik / on" : "kapali / off");
16}
17
18void loop() {
19  float t, h;
20  if (sht31.readBoth(&t, &h)) {           // tek ölçümde ikisi birden / both in one measurement
21    Serial.print("Sicaklik / Temp: ");
22    Serial.print(t, 2);
23    Serial.print(" C   Nem / RH: ");
24    Serial.print(h, 1);
25    Serial.println(" %");
26  } else {
27    Serial.println("Okuma hatasi (CRC) / read error (CRC)");
28  }
29  delay(1000);
30}

readBoth reads temperature and humidity in a single measurement and returns false if the CRC check fails, so it's more efficient and safer than calling readTemperature and readHumidity separately. The code tries 0x44 first and then 0x45, so it works even if you don't know the state of the ADR pin. In the breath test the humidity reacts within seconds; the sensor's humidity response time is about 8 seconds, so wait for the value to settle after sudden changes.

From raw data to temperature and humidity: commands and CRC

You can also talk to the SHT31 without a library. For a single measurement you send a two-byte command, and when the sensor is done it returns 6 bytes: two bytes of temperature and their CRC, two bytes of humidity and their CRC. The conversion formulas are in the datasheet: T = −45 + 175 × raw / 65535 and RH = 100 × raw / 65535. The CRC-8 uses polynomial 0x31 and initial value 0xFF; for example, the CRC of the bytes 0xBE 0xEF is 0x92.

CommandBytesPurpose
Single shot, high repeatability0x24 0x00Starts a measurement, read 6 bytes after ~15 ms
Heater on0x30 0x6DTurns on the built-in heater
Heater off0x30 0x66Turns the heater off
Soft reset0x30 0xA2Restarts the sensor
Read status register0xF3 0x2DHeater state and error bits

The SHT31 also has a periodic measurement mode: the sensor measures by itself at a rate you pick between 0.5 and 10 times per second, and you just read the latest result. In battery-powered projects the single-shot mode is a better fit, because the sensor draws almost no current between measurements. The Adafruit library uses single-shot mode as well.

Two SHT31s on one bus: indoor and outdoor

C++ (Arduino IDE)
1#include <Wire.h>
2#include <Adafruit_SHT31.h>
3
4Adafruit_SHT31 indoor;    // ADDR boşta ya da GND -> 0x44 / ADDR floating or GND
5Adafruit_SHT31 outdoor;   // ADDR -> VCC -> 0x45
6
7void printSensor(const char* name, Adafruit_SHT31& s) {
8  float t, h;
9  Serial.print(name);
10  if (s.readBoth(&t, &h)) {
11    Serial.print(t, 1);
12    Serial.print(" C  ");
13    Serial.print(h, 0);
14    Serial.println(" %");
15  } else {
16    Serial.println("hata / error");
17  }
18}
19
20void setup() {
21  Serial.begin(9600);
22  if (!indoor.begin(0x44)) Serial.println("0x44 yok / missing");
23  if (!outdoor.begin(0x45)) Serial.println("0x45 yok / missing");
24}
25
26void loop() {
27  printSensor("Ic / Indoor:  ", indoor);
28  printSensor("Dis / Outdoor: ", outdoor);
29  Serial.println();
30  delay(2000);
31}

You just create a separate object for each sensor and pass its own address to begin; both share the same SDA and SCL wires. If you move the outdoor sensor a few metres away, follow the cable tips in the waterproof probe section below. With this setup you can also compute the difference between two spots: if indoor humidity is much higher than outdoors, it's time to ventilate, and if the indoor temperature drops quickly towards the outdoor value, a window may have been left open. If you need a third SHT31, you've run out of addresses and need an I2C multiplexer (such as the TCA9548A).

The built-in heater: getting rid of condensation (ESP32)

In places where humidity goes above 95 %, such as a bathroom, greenhouse or egg incubator, water droplets can form on the sensor surface. The sensor then shows 100 % for a long time and is very slow to return to the real value. The SHT31's heater warms the chip by a few degrees to evaporate the water. While the heater is on, the measured temperature reads high and the humidity low, so the code below ignores readings during heating and for a 30-second cool-down afterwards. Don't leave the heater on permanently; run it briefly only after long saturation.

C++ (Arduino IDE)
1#include <Wire.h>
2#include <math.h>
3#include <Adafruit_SHT31.h>
4
5#define SDA_PIN 21   // ESP32 I2C SDA
6#define SCL_PIN 22   // ESP32 I2C SCL
7
8Adafruit_SHT31 sht31;
9unsigned long wetSince = 0;       // nem ne zamandan beri %95 üstü / since when RH > 95 %
10unsigned long heaterOffAt = 0;    // ısıtıcı kapanış zamanı / when the heater was switched off
11bool heating = false;
12
13float dewPoint(float t, float rh) {   // Magnus formülü / Magnus formula
14  const float a = 17.62, b = 243.12;
15  float g = log(rh / 100.0) + a * t / (b + t);
16  return b * g / (a - g);
17}
18
19void setup() {
20  Serial.begin(115200);
21  Wire.begin(SDA_PIN, SCL_PIN);
22  if (!sht31.begin(0x44)) {
23    Serial.println("SHT31 bulunamadi / not found");
24    while (true) delay(10);
25  }
26  sht31.heater(false);
27}
28
29void loop() {
30  unsigned long now = millis();
31
32  if (heating) {                          // ısıtma sırasında ölçüm yanlış / readings are wrong while heating
33    if (now - heaterOffAt >= 10000) {     // 10 sn ısıt / heat for 10 s
34      sht31.heater(false);
35      heating = false;
36      heaterOffAt = now;                  // soğuma başlangıcı / start of cool-down
37      Serial.println("Isitici kapandi / heater off");
38    }
39    delay(500);
40    return;
41  }
42  if (heaterOffAt != 0 && now - heaterOffAt < 30000) {   // 30 sn soğumayı bekle / wait 30 s to cool down
43    delay(500);
44    return;
45  }
46
47  float t, h;
48  if (!sht31.readBoth(&t, &h)) {
49    Serial.println("Okuma hatasi / read error");
50    delay(2000);
51    return;
52  }
53  Serial.printf("T: %.2f C  RH: %.1f %%  Ciy noktasi / dew point: %.1f C\n", t, h, dewPoint(t, h));
54
55  if (h > 95.0) {                         // yoğuşma şüphesi / possible condensation
56    if (wetSince == 0) wetSince = now;
57    if (now - wetSince > 5UL * 60 * 1000) {   // 5 dakikadır doymuşsa ısıt / saturated for 5 min: heat
58      sht31.heater(true);
59      heating = true;
60      heaterOffAt = now;                  // ısıtma başlangıcı / start of heating
61      wetSince = 0;
62      Serial.println("Isitici acildi / heater on");
63    }
64  } else {
65    wetSince = 0;
66  }
67  delay(2000);
68}

The code also calculates the dew point with the Magnus formula: the temperature at which air, as it cools, starts to condense. For example, at 22 °C and 60 % humidity the dew point is about 13.9 °C; a window pane colder than that fogs up. If you want to send the readings online with the ESP32, you can use the structure from ESP32 MQTT and Home Assistant.

SHT31 with a Raspberry Pi Pico and MicroPython

No extra library is needed in MicroPython; you send the command with I2C from the machine module, read the 6 bytes and check the CRC yourself. On the Pico, I2C0 uses GP4 (SDA) and GP5 (SCL). The code first prints the addresses found by an I2C scan; if 0x44 (68) isn't in the list, check the wires and the supply.

MicroPython
1from machine import Pin, I2C
2import time
3
4ADDR = 0x44                                     # ADDR -> VCC ise 0x45 / 0x45 if ADDR -> VCC
5i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=100000)
6print("I2C:", [hex(a) for a in i2c.scan()])     # 0x44 görmelisin / you should see 0x44
7
8def crc8(data):                                 # Sensirion CRC-8: polinom 0x31, başlangıç 0xFF / poly 0x31, init 0xFF
9    crc = 0xFF
10    for byte in data:
11        crc ^= byte
12        for _ in range(8):
13            crc = ((crc << 1) ^ 0x31) & 0xFF if crc & 0x80 else (crc << 1) & 0xFF
14    return crc
15
16def heater(on):                                 # dahili ısıtıcı / built-in heater
17    i2c.writeto(ADDR, bytes([0x30, 0x6D] if on else [0x30, 0x66]))
18
19def read_sht31():
20    i2c.writeto(ADDR, bytes([0x24, 0x00]))      # tek ölçüm, yüksek tekrarlanabilirlik / single shot, high repeatability
21    time.sleep_ms(20)                           # ölçüm en fazla ~15 ms / measurement takes up to ~15 ms
22    d = i2c.readfrom(ADDR, 6)                   # T_msb T_lsb CRC RH_msb RH_lsb CRC
23    if crc8(d[0:2]) != d[2] or crc8(d[3:5]) != d[5]:
24        raise ValueError("CRC hatasi / CRC error")
25    raw_t = d[0] << 8 | d[1]
26    raw_h = d[3] << 8 | d[4]
27    t = -45 + 175 * raw_t / 65535               # veri sayfası formülü / datasheet formula
28    h = 100 * raw_h / 65535
29    return t, h
30
31heater(False)
32while True:
33    try:
34        t, h = read_sht31()
35        print("Sicaklik: {:.2f} C  Nem: {:.1f} %".format(t, h))
36    except (OSError, ValueError) as e:
37        print("Hata / error:", e)
38    time.sleep(2)

Waterproof probe version

For soil, around an aquarium, outdoors or in a cold room, you can buy probe versions in which an SHT30 or SHT31 sits inside a porous metal or plastic cap with a one-metre cable. The porous cap keeps out water and dust but lets humidity through; still, "waterproof" doesn't mean you can submerge it. Wire colours vary by seller, so check the colour chart on the product page before connecting. I2C is designed for short distances; on a long cable, lower the speed with something like Wire.setClock(20000) after calling sht31.begin() and reduce the pull-up resistors to 4.7 kΩ. For distances over a few metres, use an I2C extender module.

Don't put the sensor in direct sunlight or next to a heat source; you'd be measuring the sensor's own temperature, not the air. Outdoors, a ventilated, white protective housing (a radiation shield) improves accuracy a lot.

SHT31 project ideas

  • An egg incubator with precise humidity control: the SHT31 is ideal for holding 55–65 % humidity; details in Arduino Egg Incubator.
  • Greenhouse automation: run the fan when humidity rises and the mister when it drops; Smart Greenhouse Project.
  • Bathroom humidity fan: switch on the extractor with a relay when humidity passes 75 % after a shower and off below 60 %.
  • A room thermometer with an OLED: show temperature, humidity and dew point on an OLED display.
  • A weather station comparing indoors and outdoors: use two SHT31s on the same bus at 0x44 and 0x45.

Common mistakes and how to fix them

SymptomCauseFix
"SHT31 not found"SDA/SCL swapped or address is 0x45Check the wires, run an I2C scanner
Humidity stuck at 100 % for a long timeCondensation on the sensor surfaceRun the heater briefly and ventilate the sensor
Temperature 1–3 °C above room temperatureSensor picks up heat from the board or regulatorMove the sensor away from the board on a cable, read less often
Occasional CRC errorsLong or noisy I2C cableShorten the cable, lower the speed, add pull-ups
Unstable readings on the ESP32Module powered from 5 V, lines pulled to 5 VPower the module from 3V3
Only one of two sensors shows upBoth have address 0x44Connect one sensor's ADR pin to VIN

After the DHT sensors, the SHT31 is the most balanced next step: setup is just as easy and the readings are far more reliable. In the BlueGrays app you can open DHT11 and DHT22 temperature-humidity, temperature logger and smart irrigation projects with step-by-step wiring diagrams and ready code, and take them up a level by swapping in an SHT31.

Frequently asked questions

What is the difference between the SHT31 and SHT30?

Both use the same package, the same commands and the same I2C addresses; the difference is the calibration grade. The SHT31's typical humidity accuracy is ±2 % RH, while the SHT30 gives ±2–3 % RH depending on the datasheet revision. They can replace each other without changing code or wiring.

What is the I2C address of the SHT31?

With the ADDR pin at GND or left open the address is 0x44; with it tied to VDD it's 0x45. Most modules default to 0x44. That's why two SHT31s can share one I2C bus.

Is the SHT31 more accurate than the DHT22?

Yes: the SHT31 typically gives ±2 % RH and ±0.2–0.3 °C, compared with ±2–5 % RH and ±0.5 °C for the DHT22. The SHT31 also measures faster over I2C, catches data errors with a CRC and recovers from condensation with its heater. The DHT22's advantages are its single-wire connection and wide availability.

What is the SHT31 heater for?

The heater warms the chip by a few degrees to evaporate water that has condensed on the sensor surface. You run it for a few seconds when humidity has stayed at 100 % for a long time. Readings taken while the heater is on and right after it are wrong, so discard them.

Does the SHT31 work with 5 V?

Yes, the SHT31 chip runs from 2.15–5.5 V, so it works fine with the Arduino Uno's 5 V. With an ESP32 or Raspberry Pi Pico you should power the module from 3.3 V, because the module's pull-up resistors pull the I2C lines to the supply voltage.

How do I use the SHT31 with a Raspberry Pi Pico?

In MicroPython you open the bus with I2C(0, sda=Pin(4), scl=Pin(5)), send the command 0x24 0x00, wait 20 ms and read 6 bytes. Temperature is −45 + 175 × raw / 65535 and humidity is 100 × raw / 65535. No extra library is needed.

See all projects
Temperature and Humidity with DHT11
AR-55Intermediate

Temperature and Humidity with DHT11

How warm is your room, and is the air humid or dry? The DHT11 sensor measures temperature and humidity, and the Arduino writes the values on a two-line LCD screen, refreshing every couple of seconds.

4.7(47)
96
ESP32 DHT22 Temperature and Humidity (with LCD Display)
ES-15Intermediate

ESP32 DHT22 Temperature and Humidity (with LCD Display)

This project measures ambient temperature and humidity using a DHT22 sensor and displays the data in real time on a 16x2 I2C LCD. Data is read from the sensor using the one-wire protocol via the Adafruit DHT library. This project serves as the foundation for IoT weather stations, smart home automation, or environmental monitoring systems in greenhouses. Communication with the DHT22 is established via the ESP32's GPIO 4 pin.

4.8(38)
57
ESP32 Temperature Data Logger (DHT22 + DS1307 RTC + microSD + OLED)
ES-40Advanced
Premium

ESP32 Temperature Data Logger (DHT22 + DS1307 RTC + microSD + OLED)

Build a timed temperature and humidity logger with the ESP32! A DHT22 sensor reads temperature and humidity once a minute, and each reading is saved to a microSD card in CSV format with a real-time stamp from the DS1307 RTC module. The OLED display shows the latest temperature, humidity, timestamp and total number of records live, in large text.

4.7(7)
12
Temperature Measurement with NTC and 2x16 Lcd Display
AR-44Beginner
Premium

Temperature Measurement with NTC and 2x16 Lcd Display

Measure the temperature around you with a sensor that looks like a tiny bead. This part, called an NTC, lets electricity through more easily as it warms up; the Arduino reads that change, turns it…

4.5(33)
44
Dot Matrix Clock, Calendar and Weather Station (MAX7219, DS1307)
AR-85Intermediate
Premium

Dot Matrix Clock, Calendar and Weather Station (MAX7219, DS1307)

A scrolling-text board for your desk or wall that shows the time, the date and the room's weather in turn.

5.0(50)
66
Smart Irrigation Station
AR-83Advanced
Premium

Smart Irrigation Station

A gardener that waters your plants on its own, so they don't dry out while you're on vacation.

5.0(42)
57
ESP32 HTTPClient + Weather API (with SSD1306 OLED Display)
ES-32Advanced
Premium

ESP32 HTTPClient + Weather API (with SSD1306 OLED Display)

In this project the ESP32 makes a REST API call over the internet to a weather API (wttr.in) using HTTPClient.h. Unlike the I2C LCD version, it uses an SSD1306 OLED (128×64) display, with large text and vector icons for a richer look. Seven weather icons (sunny, rainy, snowy and more) are drawn on the fly, with no bitmap files.

4.3(10)
22