SHT30

SHT30 是一款用于测量温度和湿度的传感器。

UiFlow2 示例

获取温度和湿度

在 UiFlow2 中打开 paper_sht30_example.m5f2 项目。

本示例从 SHT30 传感器读取温度和湿度。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 示例

获取温度和湿度

本示例从 SHT30 传感器读取温度和湿度。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import SHT30
 9
10
11sht30 = None
12
13
14def setup():
15    global sht30
16
17    M5.begin()
18    Widgets.fillScreen(0xEEEEEE)
19
20    sht30 = SHT30()
21
22
23def loop():
24    global sht30
25    M5.update()
26    print((str("Humidity:") + str((sht30.get_humidity()))))
27    print((str("Temperature:") + str((sht30.get_temperature()))))
28
29
30if __name__ == "__main__":
31    try:
32        setup()
33        while True:
34            loop()
35    except (Exception, KeyboardInterrupt) as e:
36        try:
37            from utility import print_error_msg
38
39            print_error_msg(e)
40        except ImportError:
41            print("please update to latest firmware")

示例输出:

None

API参考

SHT30

class hardware.sht30.SHT30

基类:SHT30

class driver.sht30.SHT30(i2c=None, delta_temp=0, delta_hum=0, i2c_address=68)

基类:object

创建一个 SHT30 对象。

参数:
  • i2c (I2C) – I2C 总线对象。

  • delta_temp (int) – 用于测量的温度修正值。

  • delta_hum (int) – 用于测量的湿度修正值。

  • i2c_address (int) – 传感器的 I2C 地址。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import Pin
from hardware import I2C
from hardware import SHT30

# Paper
i2c0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
sht30 = SHT30(i2c0)
get_temperature()

获取摄氏温度。

UiFlow2 代码块:

get_temperature.png

MicroPython 代码块:

sht30.get_temperature()
get_humidity()

获取相对湿度(百分比)。

UiFlow2 代码块:

get_humidity.png

MicroPython 代码块:

sht30.get_humidity()