SHT31 temperature and humidity sensor

The Sensirion SHT31 on an I2C breakout: temperature and humidity at 0x44 or 0x45. Aliases: GY-SHT31-D, Adafruit SHT31-D, SHT30, SHT35.

It is the sensor air quality monitors and weather stations use when the DHT22 is not accurate enough: every reading comes with a checksum, and it shares the I2C bus with a display or a gas sensor instead of needing a pin of its own.

Pins

Pin What it does
VIN Supply, 2.15 V to 5.5 V.
GND Ground.
SCL I2C clock. The module carries the pull-ups.
SDA I2C data.

Properties

address is 0x44 (the ADDR strap low, the boards' default) or 0x45. temperature (°C, -40 to 125) and humidity (%RH, 0 to 100) are the weather, and the two sliders on the part change them while the circuit runs.

How it talks

There are no registers. Every command is sixteen bits, high byte first, and a read returns words of two bytes, each followed by a CRC, as the SHT3x-DIS datasheet lays out:

  • Power on and soft reset (0x30A2) take up to 1.5 ms; until then the address is not acknowledged.
  • Single shot without clock stretching: 0x2400 (high repeatability, up to 15 ms), 0x240B (medium, 6 ms) or 0x2416 (low, 4 ms). While it measures, the sensor answers a read with a NACK.
  • Read six bytes: the temperature word, its CRC, the humidity word, its CRC. The CRC is CRC-8, polynomial 0x31, starting at 0xFF.
  • Convert: T = -45 + 175 x S / 65535 °C and RH = 100 x S / 65535 %.
  • Periodic mode (0x2032 to 0x2737, and ART 0x2B32) measures on its own; Fetch Data (0xE000) reads the latest result and Break (0x3093) stops it.
  • Status (0xF32D) has the heater bit (switched by 0x306D and 0x3066), the reset bit and the command error bit; 0x3041 clears it.

A slider moved between measurements shows up in the next one, never the one already taken.

The library

Adafruit_SHT31.h works as its tutorial uses it:

#include <Wire.h>
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup() {
  Serial.begin(9600);
  if (!sht31.begin(0x44)) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
}

void loop() {
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();
  if (!isnan(t)) {
    Serial.print("Temp *C = ");
    Serial.println(t);
  }
  if (!isnan(h)) {
    Serial.print("Hum. % = ");
    Serial.println(h);
  }
  delay(1000);
}

readBoth(&t, &h), heater(true), isHeaterEnabled(), readStatus() and reset() are there too. The readings are floats built from whole hundredths, so printing them works on every board. Sums on them link only on the Uno R4; for arithmetic, sht31.getTemperatureCentis() and sht31.getHumidityCentis() return the last reading in hundredths.

What the model gets right

The command set, the power-on and reset times, the three measurement durations and the NACK while measuring, periodic mode with Fetch and Break, the CRC on every word, the status register and its heater, reset and command bits, all from the SHT3x-DIS datasheet (version 7).

What it does not model

Clock stretching (the commands that ask for it are taken, and a read during the measurement is refused as in the no-stretch mode), the ALERT pin and its limits, the heater's actual warming, the accuracy figures (±2 %RH and ±0.3 °C typical) and the response time.

Common mistakes

Reading straight after the command. The sensor needs up to 15 ms at high repeatability, and a read before then gets a NACK. The library waits 20 ms for you.

Using 0x45 in the sketch when the strap is at 0x44, or the other way around.

See it in action

Air quality monitor reads an SHT31 and an SGP30 on the same two wires. Open it at /templates.