arduino-sht: an example that runs
Yes. You can use the arduino-sht 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, "Autodetect a Sensirion SHT and read it", runs on the Arduino Uno R3.
Built in on Mokxi: #include <SHTSensor.h> and it builds, with nothing to install. Free, and no account needed.
What arduino-sht does
SHTSensor: init() that autodetects the SHT4x, SHT3x, SHT85, SHT2x and SHTC1/SHTC3, readSample(), getTemperature(), getHumidity() and setAccuracy(). That is what arduino-sht, by Sensirion, covers. The sketch below uses it on the Arduino Uno: “Autodetect a Sensirion SHT and read it”.
Sensirion's arduino-sht library, one class for the SHT2x, SHT3x, SHT85, SHTC1, SHTC3, SHTW1/2 and SHT4x humidity sensors, with autodetection.
init() with AUTO_DETECT tries, in upstream's order, an SHT4x at 0x44, an SHT2x at 0x40, an SHT3x at 0x44 and 0x45 and an SHTC1/SHTC3 at 0x70, and keeps the first that hands back a measurement with good CRCs. Each sensor is a single-shot command, the datasheet's wait and six bytes (the SHT2x is two commands of three).
The calls follow release 2.0.0 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 | arduino-sht |
|---|---|
| Author | Sensirion |
| Include | #include <SHTSensor.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, Temperature and humidity, SHT40 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <SHTSensor.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "arduino-sht" by Sensirion from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add arduino-sht to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.0, so a sketch written here builds there too.
What to know in the simulator: Mokxi has the SHT31 and SHT4x parts; an SHT2x or SHTC3 finds nothing on the bus. SHT3xAnalogSensor builds, but there is no analog SHT3x 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.
SHTSensor sht
Makes sht, the SHTSensor object the calls below are made on.
sht.init()
Starts it up and returns true if it started, false if it did not. Call it once in setup().
sht.getSensorType()
Returns the sensor type as a SHTSensorType.
sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM)
Sets the accuracy to the value you pass in. Arguments: new accuracy.
sht.readSample()
Returns the sample as true or false.
sht.getHumidity()
Returns the humidity as a decimal number.
sht.getTemperature()
Returns the temperature as a decimal number.
Example: Autodetect a Sensirion SHT and read it
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Arduino.h>
#include <Wire.h>
#include "SHTSensor.h"
SHTSensor sht;
// To use a specific sensor instead of probing the bus:
// SHTSensor sht(SHTSensor::SHT3X);
void setup() {
Wire.begin();
Serial.begin(9600);
delay(100);
if (sht.init()) {
Serial.print("init(): success\n");
switch (sht.getSensorType()) {
case SHTSensor::SHT3X: Serial.println("Sensor detected: SHT3x/SHT85 (I2C address 0x44)"); break;
case SHTSensor::SHT4X: Serial.println("Sensor detected: SHT4x"); break;
default: Serial.println("Sensor detected: other"); break;
}
} else {
Serial.print("init(): failed\n");
}
sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM); // only supported by SHT3x
}
void loop() {
if (sht.readSample()) {
Serial.print("SHT:\n");
Serial.print(" RH: ");
Serial.print(sht.getHumidity(), 2);
Serial.print("\n");
Serial.print(" T: ");
Serial.print(sht.getTemperature(), 2);
Serial.print("\n");
} else {
Serial.print("Error in readSample()\n");
}
delay(1000);
}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 arduino-sht without installing anything
Open the example, change the sketch, and keep your version in a free account.