SHT4X

SHT4X measures temperature and relative humidity.

Supported controllers:

Controller

SHT4X

M5PaperColor

UiFlow2 Example

get temperature and humidity

This example initializes the onboard SHT4X sensor and reads the temperature and relative humidity.

Open the sht4x_papercolor_example.m5f2 project in UiFlow2.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

get temperature and humidity

This example reads temperature and relative humidity from the onboard SHT4X sensor on M5PaperColor.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import SHT4X
 9import time
10
11
12sht4x = None
13
14
15def setup():
16    global sht4x
17
18    M5.begin({"clear_display": False})
19    Widgets.setRotation(1)
20
21    M5.Lcd.setEpdMode(M5.Lcd.EPDMode.EPD_FASTEST)
22    sht4x = SHT4X()
23
24
25def loop():
26    global sht4x
27    M5.update()
28    print((str("hum:") + str((sht4x.get_humidity()))))
29    print((str("temp:") + str((sht4x.get_temperature()))))
30    time.sleep(1)
31
32
33if __name__ == "__main__":
34    try:
35        setup()
36        while True:
37            loop()
38    except (Exception, KeyboardInterrupt) as e:
39        try:
40            print(e)
41        except ImportError:
42            print("please update to latest firmware")

Example output:

None

API

SHT4X

class hardware.sht4x.SHT4X

Bases: SHT4x

Create an onboard SHT4X sensor object.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hardware import SHT4X

sht4x = SHT4X()
class driver.sht4x.SHT4x(i2c, address=micropython.const)

Bases: object

Create an SHT4x temperature and humidity sensor object.

Parameters:
  • i2c (I2C) – The I2C bus object connected to the sensor.

  • address (int) – The I2C address of the sensor. Default is 0x44.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hardware import I2C, Pin
from driver.sht4x import SHT4x

i2c0 = I2C(0, scl=Pin(2), sda=Pin(3), freq=100000)
sht4x = SHT4x(i2c0)
property serial_number: int

The unique 32-bit serial number of the sensor.

Returns:

The sensor serial number.

Return type:

int

Raises:

RuntimeError – If the received serial number CRC is invalid.

MicroPython Code Block:

sht4x.serial_number
reset()

Perform a soft reset of the sensor.

MicroPython Code Block:

sht4x.reset()
Return type:

None

property mode: int

The current sensor reading mode.

The mode selects both heater behavior and measurement precision. Use one of the values from Mode, such as Mode.NOHEAT_HIGHPRECISION or Mode.HIGHHEAT_100MS.

Returns:

The current measurement mode command.

Return type:

int

MicroPython Code Block:

from driver.sht4x import Mode

sht4x.mode = Mode.NOHEAT_HIGHPRECISION
sht4x.mode
property relative_humidity: float

The current relative humidity in percent RH.

Returns:

The relative humidity, constrained to 0 through 100.

Return type:

float

UiFlow2 Code Block:

get_humidity.png

MicroPython Code Block:

sht4x.relative_humidity
get_humidity()

Get the current relative humidity in percent RH.

Returns:

The relative humidity, constrained to 0 through 100.

Return type:

float

UiFlow2 Code Block:

get_humidity.png

MicroPython Code Block:

sht4x.get_humidity()
property temperature: float

The current temperature in degrees Celsius.

Returns:

The temperature in degrees Celsius.

Return type:

float

UiFlow2 Code Block:

get_temperature.png

MicroPython Code Block:

sht4x.temperature
get_temperature()

Get the current temperature in degrees Celsius.

Returns:

The temperature in degrees Celsius.

Return type:

float

UiFlow2 Code Block:

get_temperature.png

MicroPython Code Block:

sht4x.get_temperature()
measure()

Measure temperature and relative humidity simultaneously.

Returns:

A tuple containing (temperature, relative_humidity).

Return type:

tuple[float, float]

Raises:

RuntimeError – If the received measurement CRC is invalid.

MicroPython Code Block:

temperature, humidity = sht4x.measure()