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 name | Sensirion I2C SHT3x |
|---|---|
| Author | Sensirion |
| Include | #include <SensirionI2cSht3x.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 <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.
- 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.
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.
#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
Keep going
Try Sensirion I2C SHT3x without installing anything
Open the example, change the sketch, and keep your version in a free account.