CO2 单位

CO2Unit 是一款用于测量 CO2 浓度、温度和湿度的硬件模块。它通过 I2C 通信,并提供用于校准、测量和配置的功能。

支持以下产品:

CO2Unit

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 *
 9from unit import CO2Unit
10
11
12title0 = None
13label0 = None
14label1 = None
15label2 = None
16label3 = None
17i2c0 = None
18co2_0 = None
19
20
21def setup():
22    global title0, label0, label1, label2, label3, i2c0, co2_0
23
24    M5.begin()
25    Widgets.fillScreen(0x222222)
26    title0 = Widgets.Title("CO2Unit CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
27    label0 = Widgets.Label("label0", 1, 44, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label1 = Widgets.Label("label1", 1, 95, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29    label2 = Widgets.Label("label2", 1, 146, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30    label3 = Widgets.Label("label3", 1, 198, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31
32    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
33    co2_0 = CO2Unit(i2c0)
34    co2_0.set_start_periodic_measurement()
35
36
37def loop():
38    global title0, label0, label1, label2, label3, i2c0, co2_0
39    if co2_0.is_data_ready():
40        label0.setText(str("Data is ready."))
41        label1.setText(str((str("CO2 ppm:") + str((co2_0.co2)))))
42        label2.setText(str((str("Humidity:") + str((co2_0.humidity)))))
43        label3.setText(str((str("Temperature:") + str((co2_0.temperature)))))
44    else:
45        label0.setText(str("Data not ready."))
46
47
48if __name__ == "__main__":
49    try:
50        setup()
51        while True:
52            loop()
53    except (Exception, KeyboardInterrupt) as e:
54        try:
55            from utility import print_error_msg
56
57            print_error_msg(e)
58        except ImportError:
59            print("please update to latest firmware")

UiFlow2 应用示例

example.png

co2_cores3_example.m5f2

class CO2Unit

Constructors

class CO2Unit(i2c, address)

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

参数:
  • i2c – 用于通信的 I2C 接口或 PAHUBUnit 实例。

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

UiFlow2

init.png

Methods

CO2Unit.available()

检查 I2C 总线上是否存在 CO2 单元。

CO2Unit.set_start_periodic_measurement()

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

UiFlow2

set_start_periodic_measurement.png

CO2Unit.set_stop_periodic_measurement()

停止传感器的测量模式。

UiFlow2

set_stop_periodic_measurement.png

CO2Unit.get_sensor_measurement()

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

CO2Unit.is_data_ready()

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

UiFlow2

is_data_ready.png

CO2Unit.get_temperature_offset()

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

UiFlow2

get_temperature_offset.png

CO2Unit.set_temperature_offset(offset)

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

参数:

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

UiFlow2

set_temperature_offset.png

CO2Unit.get_sensor_altitude()

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

UiFlow2

get_sensor_altitude.png

CO2Unit.set_sensor_altitude(height)

以海拔高度(单位:米)设置测量位置的海拔值。

参数:

height (int) – 要设置的高度(单位:m)。必须在 0 到 65535 m 之间。

UiFlow2

set_sensor_altitude.png

CO2Unit.set_ambient_pressure(ambient_pressure)

可随时设置以 hPa 为单位的环境压力,用于调整 CO2 计算结果。

参数:

ambient_pressure (int) – 环境压力(单位:hPa),取值范围限制为 [0, 65535]。

UiFlow2

set_ambient_pressure.png

CO2Unit.set_force_calibration(target_co2)

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

参数:

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

UiFlow2

set_force_calibration.png

CO2Unit.get_calibration_enabled()

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

UiFlow2

get_calibration_enabled.png

CO2Unit.set_calibration_enabled(enabled)

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

参数:

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

UiFlow2

set_calibration_enabled.png

CO2Unit.set_start_low_periodic_measurement()

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

UiFlow2

set_start_low_periodic_measurement.png

CO2Unit.data_isready()

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

CO2Unit.save_to_eeprom()

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

UiFlow2

save_to_eeprom.png

CO2Unit.get_serial_number()

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

UiFlow2

get_serial_number.png

CO2Unit.set_self_test()

执行自检,可能需要长达 10 秒。

UiFlow2

set_self_test.png

CO2Unit.set_factory_reset()

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

UiFlow2

set_factory_reset.png

CO2Unit.reinit()

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

UiFlow2

reinit.png

CO2Unit.set_single_shot_measurement_all()

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

CO2Unit.set_single_shot_measurement_ht()

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

CO2Unit.set_sleep_mode()

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

CO2Unit.set_wake_up()

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

CO2Unit.write_cmd(cmd_wr, value)

向传感器写入命令。

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

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

CO2Unit.read_response(num)

读取传感器的响应。

参数:

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

CO2Unit.check_crc(buf)

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

参数:

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

CO2Unit.crc8(buffer)

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

参数:

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