SHT4X

SHT4X 用于测量温度和相对湿度。

支持的控制器:

控制器

SHT4X

M5PaperColor

✔

UiFlow2 应用示例

获取温度和湿度

本示例初始化板载 SHT4X 传感器,并读取温度和相对湿度。

在 UiFlow2 中打开 sht4x_papercolor_example.m5f2 项目。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 应用示例

获取温度和湿度

本示例从 M5PaperColor 的板载 SHT4X 传感器读取温度和相对湿度。

MicroPython 代码块:

 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")

示例输出:

None

API参考

SHT4X

class hardware.sht4x.SHT4X

基类:SHT4x

创建板载 SHT4X 传感器对象。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import SHT4X

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

基类:object

创建 SHT4x 温湿度传感器对象。

参数:
  • i2c (I2C) – 连接到传感器的 I2C 总线对象。

  • address (int) – 传感器的 I2C 地址,默认值为 0x44。

UiFlow2 代码块:

init.png

MicroPython 代码块:

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

传感器唯一的 32 位序列号。

返回:

传感器序列号。

返回类型:

int

抛出:

RuntimeError – 接收到的序列号 CRC 无效时引发。

MicroPython 代码块:

sht4x.serial_number
reset()

对传感器执行软复位。

MicroPython 代码块:

sht4x.reset()
返回类型:

None

property mode: int

当前传感器读取模式。

该模式同时选择加热器行为和测量精度。使用 Mode 中的值,例如 Mode.NOHEAT_HIGHPRECISION 或 Mode.HIGHHEAT_100MS。

返回:

当前测量模式命令。

返回类型:

int

MicroPython 代码块:

from driver.sht4x import Mode

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

当前相对湿度,单位为 %RH。

返回:

相对湿度,范围限制为 0 到 100。

返回类型:

float

UiFlow2 代码块:

get_humidity.png

MicroPython 代码块:

sht4x.relative_humidity
get_humidity()

获取当前相对湿度,单位为 %RH。

返回:

相对湿度,范围限制为 0 到 100。

返回类型:

float

UiFlow2 代码块:

get_humidity.png

MicroPython 代码块:

sht4x.get_humidity()
property temperature: float

当前温度,单位为摄氏度。

返回:

温度,单位为摄氏度。

返回类型:

float

UiFlow2 代码块:

get_temperature.png

MicroPython 代码块:

sht4x.temperature
get_temperature()

获取当前温度,单位为摄氏度。

返回:

温度,单位为摄氏度。

返回类型:

float

UiFlow2 代码块:

get_temperature.png

MicroPython 代码块:

sht4x.get_temperature()
measure()

同时测量温度和相对湿度。

返回:

包含 (temperature, relative_humidity) 的元组。

返回类型:

tuple[float, float]

抛出:

RuntimeError – 接收到的测量数据 CRC 无效时引发。

MicroPython 代码块:

temperature, humidity = sht4x.measure()