Arduino library · Sensors

Sensirion I2C SHT3x: an example that runs

Yes. You can use the Sensirion I2C SHT3x library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Temperature and humidity, SHT31). The example below, "An SHT31 measuring once a second through Sensirion’s calls", runs on the Arduino Uno R3.

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

What Sensirion I2C SHT3x does

The SHT3x through Sensirion’s calls: measureSingleShot(), startPeriodicMeasurement() and blockingReadMeasurement(), the status register, the heater, softReset() and errorToString(). That is what Sensirion I2C SHT3x covers. The sketch below uses it on the Arduino Uno: “An SHT31 measuring once a second through Sensirion’s calls”.

Sensirion's SHT3x library, for the SHT30, SHT31, SHT33 and SHT35 temperature and humidity sensors.

Every call returns NO_ERROR (0) or an error code errorToString() names. Commands are sixteen bits; the single shots wait the datasheet's maximum (16, 7 and 5 ms) and read two words: T = -45 + 175 x S / 65535 and RH = 100 x S / 65535.

The calls follow release 1.0.1 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameSensirion I2C SHT3x
AuthorSensirion
Include#include <SensirionI2cSht3x.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesTemperature and humidity, SHT31

Install it

It builds on every board Mokxi compiles for but ATtiny85.

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

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

What to know in the simulator: blockingReadMeasurement() ends its wait when the fetch is answered, since the part leaves the status bits Sensirion’s library polls clear. The heater sets its status bit without warming the part.

The calls you will use

In the order a sketch meets them.

SensirionI2cSht3x sensor

Makes sensor, the SensirionI2cSht3x object the calls below are made on.

sensor.begin(Wire, SHT30_I2C_ADDR_44)

Starts it up. Call it once in setup().

sensor.stopMeasurement()

Stops the measurement and returns a whole number.

sensor.readStatusRegister(aStatusRegister)

Returns the status register as a whole number. Arguments: a status register.

Example: An SHT31 measuring once a second through Sensirion’s calls

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

sketch.ino · Arduino Uno R3
#include <Arduino.h>
#include <SensirionI2cSht3x.h>
#include <Wire.h>

SensirionI2cSht3x sensor;

static char errorMessage[64];
static int16_t error;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  sensor.begin(Wire, SHT30_I2C_ADDR_44);

  sensor.stopMeasurement();
  delay(1);
  sensor.softReset();
  delay(100);
  uint16_t aStatusRegister = 0u;
  error = sensor.readStatusRegister(aStatusRegister);
  if (error != NO_ERROR) {
    Serial.print("Error trying to execute readStatusRegister(): ");
    errorToString(error, errorMessage, sizeof errorMessage);
    Serial.println(errorMessage);
    return;
  }
  Serial.print("aStatusRegister: ");
  Serial.print(aStatusRegister);
  Serial.println();
  error = sensor.startPeriodicMeasurement(REPEATABILITY_MEDIUM, MPS_ONE_PER_SECOND);
  if (error != NO_ERROR) {
    Serial.print("Error trying to execute startPeriodicMeasurement(): ");
    errorToString(error, errorMessage, sizeof errorMessage);
    Serial.println(errorMessage);
    return;
  }
}

void loop() {
  float aTemperature = 0.0;
  float aHumidity = 0.0;
  error = sensor.blockingReadMeasurement(aTemperature, aHumidity);
  if (error != NO_ERROR) {
    Serial.print("Error trying to execute blockingReadMeasurement(): ");
    errorToString(error, errorMessage, sizeof errorMessage);
    Serial.println(errorMessage);
    return;
  }
  Serial.print("aTemperature: ");
  Serial.print(aTemperature);
  Serial.print("	");
  Serial.print("aHumidity: ");
  Serial.print(aHumidity);
  Serial.println();
}

Wiring

  • Arduino Uno R3 A4 to Temperature and humidity, SHT31 SDA
  • Arduino Uno R3 A5 to Temperature and humidity, SHT31 SCL
  • Arduino Uno R3 5V to Temperature and humidity, SHT31 VIN
  • Arduino Uno R3 GND to Temperature and humidity, SHT31 GND

Open it in the editor, wired and ready to run

Try Sensirion I2C SHT3x without installing anything

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