Adafruit SHT4x: the SHT40 family on Arduino
Yes. You can use the Adafruit SHT4x library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Temperature and humidity, SHT40). The example below, "An SHT40 read at high precision with Adafruit SHT4x", runs on the Arduino Uno R3.
Built in on Mokxi: #include <Adafruit_SHT4x.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit SHT4x does
The SHT40, SHT41 and SHT45 are Sensirion’s fourth generation of temperature and humidity sensors. All three use the same commands at 0x44, so one library covers them; they differ in accuracy grade, not in code. Adafruit’s library returns each measurement as two sensors_event_t structs, one for humidity and one for temperature.
There are no registers to set up. Every measurement is a single command picked from two choices, the precision (high, medium or low, trading time and power against noise) and the heater, followed by a short wait and six bytes with two CRCs. setPrecision() and setHeater() simply choose which command the next getEvent() sends. begin() resets the sensor and reads its serial number; no answer means no sensor.
The heater exists to drive off condensation after long spells in very damp air. A heater setting runs it for 100 ms or a second before the measurement, so that reading is warm and dry and should be thrown away. In Mokxi the sht4x part has sliders for both values; the heater runs and holds the reading back for its time, but the part does not warm up. Sensirion publishes its own SHT4x library with error codes in place of events, listed separately.
| Library Manager name | Adafruit SHT4x |
|---|---|
| Author | Adafruit |
| Include | #include <Adafruit_SHT4x.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, SHT40 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Adafruit_SHT4x.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit SHT4x" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit SHT4x to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.5, so a sketch written here builds there too.
What to know in the simulator: The heater runs and holds the reading back for its time; the part does not warm up.
- 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
- ESP32-S3-CAM
- 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.
Adafruit_SHT4x sht4 = Adafruit_SHT4x()
Creates the driver.
sht4.begin()
Soft-resets the sensor at 0x44 and reads its serial number. Returns false when nothing answers. begin(&Wire1) uses another bus.
sht4.readSerial()
The sensor’s 32-bit serial number, printed in hex by the example.
sht4.setPrecision(SHT4X_HIGH_PRECISION)
Picks the measurement command: SHT4X_MED_PRECISION and SHT4X_LOW_PRECISION are quicker and noisier.
sht4.setHeater(SHT4X_NO_HEATER)
No heater for normal readings. SHT4X_HIGH_HEATER_1S down to SHT4X_LOW_HEATER_100MS run it before the next measurement.
sht4.getEvent(&humidity, &temp)
Measures and fills humidity.relative_humidity in percent, clipped to 0 to 100, and temp.temperature in °C. Returns false on a failed read or a bad CRC.
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
Serial.println(humidity.relative_humidity);sht4.getTemperatureSensor()
An Adafruit_Sensor for temperature alone; getHumiditySensor() for humidity.
sht4.reset()
Sends the soft reset again without the rest of begin().
Example: An SHT40 read at high precision with Adafruit SHT4x
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include "Adafruit_SHT4x.h"
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
void setup() {
Serial.begin(115200);
if (!sht4.begin()) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
Serial.print("Found SHT4x sensor, serial 0x");
Serial.println(sht4.readSerial(), HEX);
sht4.setPrecision(SHT4X_HIGH_PRECISION);
sht4.setHeater(SHT4X_NO_HEATER);
}
void loop() {
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp);
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity.relative_humidity);
Serial.println("% rH");
delay(500);
}Wiring
- Arduino Uno R3 A4 to Temperature and humidity, SHT40 SDA
- Arduino Uno R3 A5 to Temperature and humidity, SHT40 SCL
- Arduino Uno R3 5V to Temperature and humidity, SHT40 VIN
- Arduino Uno R3 GND to Temperature and humidity, SHT40 GND
Common errors and how to fix them
Couldn't find SHT4x
Nothing answered the reset and serial number request at 0x44. Check the bus wires and VIN. This library always uses 0x44, so a sensor variant at another address will not answer it.
One reading jumps several degrees warmer and drier than the rest
A heater setting was still selected, so that measurement followed a heater pulse. Call setHeater(SHT4X_NO_HEATER) after the drying reading and discard the heated one.
Humidity sits at exactly 100
The formula went past the top and the library clipped it, which on hardware usually means condensation on the sensor. Run a few heater cycles to dry it, then read again with the heater off.
Questions and answers
Which precision setting should I use on an SHT4x?
High, as the example does, unless power or speed matters. Low precision returns sooner and draws less, with more noise in the last digits.
How is this different from the Adafruit SHT31 library?
The SHT31 driver returns plain floats from readTemperature() and readHumidity(). This one returns events and lets each measurement choose its precision and heater.
Projects that use Adafruit SHT4x
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Try Adafruit SHT4x without installing anything
Open the example, change the sketch, and keep your version in a free account.