SCD40

SCD4x 是 Sensirion 的新一代微型 CO2 传感器。通过内置的 SHT4x 湿度和温度传感器,实现芯片级信号补偿。

主机对 SCD40 的具体支持如下:

控制器

SCD40

AirQ

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 *
 9
10
11label0 = None
12rotary = None
13
14
15def btnA_wasClicked_event(state):  # noqa: N802
16    global label0, rotary
17    rotary.reset_rotary_value()
18    label0.setText(str(rotary.get_rotary_value()))
19
20
21def setup():
22    global label0, rotary
23
24    M5.begin()
25    Widgets.fillScreen(0x222222)
26    label0 = Widgets.Label("0", 96, 80, 1.0, 0xFFA000, 0x222222, Widgets.FONTS.DejaVu72)
27
28    BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)
29
30    rotary = Rotary()
31
32
33def loop():
34    global label0, rotary
35    M5.update()
36    if rotary.get_rotary_status():
37        label0.setText(str(rotary.get_rotary_value()))
38
39
40if __name__ == "__main__":
41    try:
42        setup()
43        while True:
44            loop()
45    except (Exception, KeyboardInterrupt) as e:
46        try:
47            from utility import print_error_msg
48
49            print_error_msg(e)
50        except ImportError:
51            print("please update to latest firmware")

UiFlow2 应用示例

example.png

airq_scd40_example.m5f2

class SCD40

构造函数

class SCD40

使用 I2C 接口和地址初始化 SCD40。

UIFLOW2:

init.png

方法

SCD40.available()

检查 I2C 总线上是否存在 SCD40 传感器。

SCD40.set_start_periodic_measurement()

将传感器设置为工作模式,每次测量约需 5 秒。

UIFLOW2:

set_start_periodic_measurement.png

SCD40.set_stop_periodic_measurement()

停止传感器的测量模式。

UIFLOW2:

set_stop_periodic_measurement.png

SCD40.get_sensor_measurement()

从传感器获取温度、湿度和 CO2 浓度。

SCD40.is_data_ready()

检查传感器的数据(温度、湿度、CO2)是否已准备好。

UIFLOW2:

is_data_ready.png

SCD40.get_temperature_offset()

获取要添加到上报测量值中的温度偏移量。

UIFLOW2:

get_temperature_offset.png

SCD40.set_temperature_offset(offset)

设置温度偏移的最大值为 374°C。

参数:

offset (int) – 要设置的温度偏移量,默认为 0。

UIFLOW2:

set_temperature_offset.png

SCD40.get_sensor_altitude()

获取测量位置相对于海平面的海拔高度值,单位为 m。

UIFLOW2:

get_sensor_altitude.png

SCD40.set_sensor_altitude(height)

以海平面为基准,设置测量位置的海拔值,单位为米。

参数:

height (int) – 要设置的海拔高度,单位为米。必须在 0 到 65535 米之间。

UIFLOW2:

set_sensor_altitude.png

SCD40.set_ambient_pressure(ambient_pressure)

可随时设置环境压力(单位:hPa),以调整 CO2 计算结果。

参数:

ambient_pressure (int) – 环境气压,单位为 hPa,限制在 [0, 65535] 范围内。

UIFLOW2:

set_ambient_pressure.png

SCD40.set_force_calibration(target_co2)

强制传感器使用给定的当前 CO2 水平重新校准。

参数:

target_co2 (int) – 用于重新校准的当前 CO2 浓度。

UIFLOW2:

set_force_calibration.png

SCD40.get_calibration_enabled()

获取自动自校准(ASC)是启用还是禁用。

UIFLOW2:

get_calibration_enabled.png

SCD40.set_calibration_enabled(enabled)

启用或禁用自动自校准(ASC)。

参数:

enabled (bool) – 设置为 True 以启用 ASC,或设置为 False 以禁用它。

UIFLOW2:

set_calibration_enabled.png

SCD40.set_start_low_periodic_measurement()

将传感器设置为低功耗工作模式,每次测量间隔约 30 秒。

UIFLOW2:

set_start_low_periodic_measurement.png

SCD40.data_isready()

检查传感器是否有新数据可用。

SCD40.save_to_eeprom()

将温度偏移量、海拔偏移量和自校准启用设置保存到 EEPROM。

UIFLOW2:

save_to_eeprom.png

SCD40.get_serial_number()

获取该传感器的唯一序列号。

UIFLOW2:

get_serial_number.png

SCD40.set_self_test()

执行自检,最多可能需要 10 秒。

UIFLOW2:

set_self_test.png

SCD40.set_factory_reset()

重置存储在 EEPROM 中的所有配置设置,并清除 FRC 和 ASC 算法历史记录。

UIFLOW2:

set_factory_reset.png

SCD40.reinit()

通过从 EEPROM 重新加载用户设置来重新初始化传感器。

UIFLOW2:

reinit.png

SCD40.set_single_shot_measurement_all()

将传感器设置为对 CO2、湿度和温度执行单次测量。

SCD40.set_single_shot_measurement_ht()

将传感器设置为对湿度和温度执行单次测量。

SCD40.set_sleep_mode()

将传感器置于睡眠模式以降低电流消耗。

SCD40.set_wake_up()

将传感器从睡眠模式唤醒到空闲模式。

SCD40.write_cmd(cmd_wr, value)

向传感器写入命令。

参数:
  • cmd_wr (int) – 写入传感器的命令。

  • value (int) – 命令要发送的值(如果有)。

SCD40.read_response(num)

读取传感器的响应。

参数:

num (int) – 从传感器读取的字节数。

SCD40.check_crc(buf)

检查接收数据的 CRC,以确保其正确。

参数:

buf (bytearray) – 要检查 CRC 的字节缓冲区。

SCD40.crc8(buffer)

计算给定缓冲区的 CRC-8 校验和。

参数:

buffer (bytearray) – 用于计算 CRC 的字节缓冲区。