MojoScale Studio Docs
API Reference

sht4x

SHT4x temperature and humidity module.

Studio Docs Sensors & I/O

import sht4x

The sht4x module reads Sensirion SHT40, SHT41, and SHT45 temperature/humidity sensors over I2C. It verifies readings with the Sensirion CRC and returns simple temperature and relative-humidity values for scheduled Studio scripts.

Quick example

import sht4x

if sht4x.begin(21, 22):
    reading = sht4x.read()
    print(reading["c"])
    print(reading["humidity"])

sht4x.configure("medium")

importsht4x

SHT4x temperature and humidity module.

The sht4x module reads Sensirion SHT40, SHT41, and SHT45 temperature/humidity sensors over I2C. It verifies readings with the Sensirion CRC and returns simple temperature and relative-humidity values for scheduled Studio scripts.

API 9 available

sht4x.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> bool

Initialize an SHT4x sensor on I2C.

Parameters
Name Type Pass as Required Description
sda int positional or keyword Yes GPIO pin used for I2C SDA.
scl int positional or keyword Yes GPIO pin used for I2C SCL.
address int positional or keyword No Optional I2C address. Defaults to 0x44.
frequency int positional or keyword No Optional I2C bus speed in hertz. Defaults to 100000.
port int positional or keyword No Optional ESP-IDF I2C port number. Defaults to 0.
Returns

bool True when the sensor responds to the serial-number command with valid CRC bytes.

sht4x.configure(precision: str = None) -> bool

Set the default measurement precision.

Parameters
Name Type Pass as Required Description
precision str positional or keyword No Optional precision string. Use "high", "medium", or "low". Defaults to "high".
Returns

bool True when the precision is accepted.

sht4x.read(precision: str = None) -> dict

Read temperature and relative humidity.

The command is blocking for the selected precision: about 10 ms for high, 5 ms for medium, and 2 ms for low.

Parameters
Name Type Pass as Required Description
precision str positional or keyword No Optional precision string for this read only. Use "high", "medium", or "low".
Returns

dict Dict with ``ok``, ``c``, ``f``, ``temperature``, ``humidity``, ``raw_temperature``, ``raw_humidity``, ``precision``, ``address``, and ``serial`` fields.

sht4x.temperature(precision: str = None) -> float

Read temperature in Celsius.

Parameters
Name Type Pass as Required Description
precision str positional or keyword No Optional precision string for this read only. Use "high", "medium", or "low".
Returns

float Float temperature in degrees Celsius.

sht4x.humidity(precision: str = None) -> float

Read relative humidity.

Parameters
Name Type Pass as Required Description
precision str positional or keyword No Optional precision string for this read only. Use "high", "medium", or "low".
Returns

float Float relative humidity percentage, clamped to 0 through 100.

sht4x.heater(power: str = None, duration_ms: int = None) -> dict

Activate the SHT4x heater and return the following measurement.

Use this sparingly for condensation recovery or diagnostics. The firmware maps duration values at or below 500 ms to the short heater command and larger values to the long heater command.

Parameters
Name Type Pass as Required Description
power str positional or keyword No Optional heater power string. Use "low", "medium", or "high". Defaults to "low".
duration_ms int positional or keyword No Optional heater duration. Use about 100 or 1000 ms. Defaults to 100.
Returns

dict Dict with the same fields as ``read()``, plus ``heater_power`` and ``heater_duration_ms``.

sht4x.serial() -> str

Read the SHT4x serial number.

Returns

str Eight-character hex string.

sht4x.reset() -> bool

Soft-reset the SHT4x sensor.

Returns

bool True when reset succeeds and the serial number can be read again.

sht4x.info() -> dict

Return SHT4x module state.

Returns

dict Dict with initialization state, I2C pins, address, frequency, port, precision, and serial number.