Source: https://mokxi.com/libraries/adafruit-sht31
Updated: 2026-11-11

Libraries / Sensors

Arduino library · Sensors

# Adafruit SHT31: temperature and humidity from the SHT31-D

The SHT31-D is Sensirion’s I2C temperature and humidity sensor. Each answer it sends carries a CRC byte after every data word, so a garbled transfer is caught and reported as NaN instead of being printed as a believable number. Adafruit’s library for it needs only Wire: there are no events to declare, just a call for each value that returns a float.

Open the example in the editor See the calls

Built in on Mokxi: #include <Adafruit_SHT31.h> and it builds, with nothing to install. Free, and no account needed.

## What Adafruit SHT31 Library does

The module answers at 0x44, or 0x45 when its ADR pin is tied high. begin() sends a soft reset and reads the status register; when nothing replies it returns false. Each readTemperature() or readHumidity() runs its own single-shot measurement at high repeatability and waits 20 ms for it, so a sketch that wants both values from the same moment should use readBoth(), which takes one measurement for the pair.

In Mokxi the sht31 part has a slider for each value. The Mokxi build adds measure(), getTemperatureCentis() and getHumidityCentis(), which return whole hundredths and build no floats, to save flash on an Uno. The air quality monitor below relies on them to give the SGP30 its humidity correction and to show the climate on a four-digit display. The upstream library lacks those three calls, so code meant for hardware should stay with the float ones.

Library Manager name | Adafruit SHT31 Library

Author | Adafruit

Include | #include <Adafruit_SHT31.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Temperature and humidity, SHT31

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_SHT31.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit SHT31 Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit SHT31 Library to lib_deps in PlatformIO. Mokxi follows the calls of release 2.2.2, so a sketch written here builds there too.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Adafruit_SHT31 sht31 = Adafruit_SHT31()

Creates the driver on the default Wire bus. Pass &Wire1 to the constructor on a board with a second bus.

### sht31.begin(0x44)

Resets the sensor and checks that its status register answers. Returns false when nothing is at that address; use 0x45 for a module with ADR high.

### float t = sht31.readTemperature()

Degrees Celsius from a fresh measurement, or NaN when the read failed or a CRC did not match.

### float h = sht31.readHumidity()

Relative humidity in percent, from a measurement of its own, with the same NaN rule.

### sht31.readBoth(&t, &h)

One measurement for both values, half the bus time of two separate calls. Returns false, and writes NaN to both, when it failed.

### sht31.heater(true)

Switches on the heater built into the sensor, which a real SHT31 uses to drive off condensation. isHeaterEnabled() reads the status bit back.

### sht31.getTemperatureCentis()

Mokxi’s own: after measure(), the temperature in hundredths of a degree as an int32_t, so 2345 means 23.45 °C. getHumidityCentis() is the humidity twin.

## Example: Air quality monitor

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · Air quality monitor: the project page

## Common errors and how to fix them

### Couldn't find SHT31

begin() heard no reply at the address given. On an Uno, SDA goes to A4 and SCL to A5, with VIN powered. If the ADR pin is high, call begin(0x45); an I2C scanner shows which of the two answers.

### Temperature or humidity prints nan

The measurement failed or its checksum was wrong, usually from a loose jumper, a long cable without pull-ups, or two devices sharing one address. Test with isnan() before using the value and try again on the next pass.

### Readings run warm and dry compared with another thermometer nearby

The heater may have been left on, or the module sits beside a regulator or a busy chip. Call heater(false), then mount the sensor where nothing on the board warms it.

## Questions and answers

### Can I read the SHT31 without floating point on an Uno?

In Mokxi, yes: call measure(), then getTemperatureCentis() and getHumidityCentis() for whole hundredths, which is how the air quality monitor stays inside the Uno’s flash.

### How is this different from the Adafruit SHT4x library?

The SHT4x driver returns sensors_event_t structs from getEvent() and has precision and heater settings per measurement. This one returns plain floats from separate calls.

## Keep going

### Learn

- Arduino SHT31: Library, Example and Wiring
- I2C Scanner for Arduino and ESP32, and Fixes

### Parts

- Temperature and humidity, SHT31

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280

Every built-in library · Open the editor

## Try Adafruit SHT31 Library without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
