Try Embedded

SHT31 without a library (CRC check)

Talk to an SHT31 with Wire alone: send its 16-bit command, wait out the measurement, and check each word's CRC-8 before trusting it.

Sensors · Intermediate

In a real product: Data centre climate monitor (Telecom and networking): Humidity readings are trusted only after their CRC checks.

Libraries hide the protocol. This sketch talks to the SHT31 humidity sensor straight from its datasheet: a two-byte command, a wait, six bytes back, and a checksum on each value so a corrupted reading is thrown away instead of used.

What you'll learn

  • Sending a 16-bit command over I²C, most significant byte first
  • Why a sensor can refuse its own address: it does not acknowledge while it measures
  • Computing a CRC-8 (polynomial 0x31, start 0xFF) and checking it against the sensor's

How it works

Command 0x2400 asks for one measurement at high repeatability, without clock stretching. It takes up to 15 ms, and until then the sensor does not acknowledge its address, so the sketch waits 16 ms before reading. The six bytes are the temperature (two bytes and a CRC) and the humidity (two bytes and a CRC). The CRC covers the two bytes before it; a mismatch means the bus garbled something, and the reading is discarded.

The raw values convert as in the datasheet: T = -45 + 175 × raw / 65535 °C and RH = 100 × raw / 65535 %.

Try this

  • Shorten the wait to 5 ms: the read fails, because the sensor is still measuring and NACKs its address.
  • Wire ADDR to 5V: the sensor moves to 0x45. Change the sketch to find it there.
  • Send 0x306D (heater on) once in setup() and read the status register (0xF32D) to see bit 13 set.

sketch.ino

// An SHT31 read with Wire, no library: the single-shot command, a wait for the measurement, six bytes back, and a
// CRC-8 check (polynomial 0x31, starting at 0xFF) on each word before it is used.
#include <Wire.h>

const uint8_t SHT31 = 0x44;

uint8_t crc8(const uint8_t *data, int len) {
  uint8_t crc = 0xFF;
  for (int i = 0; i < len; i++) {
    crc ^= data[i];
    for (int b = 0; b < 8; b++) crc = (crc & 0x80) ? (crc << 1) ^ 0x31 : crc << 1;
  }
  return crc;
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  Wire.beginTransmission(SHT31);
  Wire.write(0x24);  // single shot, high repeatability, no clock stretching
  Wire.write(0x00);
  if (Wire.endTransmission() != 0) {
    Serial.println("No SHT31 at 0x44");
    delay(1000);
    return;
  }
  delay(16);  // up to 15 ms, during which it does not answer its address
  uint8_t b[6];
  if (Wire.requestFrom(SHT31, (uint8_t)6) != 6) {
    Serial.println("No answer: still measuring?");
    delay(1000);
    return;
  }
  for (int i = 0; i < 6; i++) b[i] = Wire.read();
  if (crc8(b, 2) != b[2] || crc8(b + 3, 2) != b[5]) {
    Serial.println("CRC mismatch: reading thrown away");
    return;
  }
  uint16_t rawT = ((uint16_t)b[0] << 8) | b[1];
  uint16_t rawRH = ((uint16_t)b[3] << 8) | b[4];
  Serial.print(-45 + 175.0 * rawT / 65535.0, 2);
  Serial.print(" C, ");
  Serial.print(100.0 * rawRH / 65535.0, 2);
  Serial.println(" %RH");
  delay(1000);
}

Lessons behind it