CO2 Unit
CO2Unit is a hardware module designed for measuring CO2 concentration, temperature, and humidity. It communicates via I2C and provides functions for calibration, measurement, and configuration.
Support the following products:
Micropython Example:
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:
class CO2Unit
Constructors
Methods
- CO2Unit.available()
Check if the CO2 unit is available on the I2C bus.
- CO2Unit.set_start_periodic_measurement()
Set the sensor into working mode, which takes about 5 seconds per measurement.
UIFLOW2:
- CO2Unit.set_stop_periodic_measurement()
Stop the measurement mode for the sensor.
UIFLOW2:
- CO2Unit.get_sensor_measurement()
Get temperature, humidity, and CO2 concentration from the sensor.
- CO2Unit.is_data_ready()
Check if the data (temperature, humidity, CO2) is ready from the sensor.
UIFLOW2:
- CO2Unit.get_temperature_offset()
Get the temperature offset to be added to the reported measurements.
UIFLOW2:
- CO2Unit.set_temperature_offset(offset)
Set the maximum value of 374°C temperature offset.
- Parameters:
offset (int) – The temperature offset to set, default is 0.
UIFLOW2:
- CO2Unit.get_sensor_altitude()
Get the altitude value of the measurement location in meters above sea level.
UIFLOW2:
- CO2Unit.set_sensor_altitude(height)
Set the altitude value of the measurement location in meters above sea level.
- Parameters:
height (int) – The altitude in meters to set. Must be between 0 and 65535 meters.
UIFLOW2:
- CO2Unit.set_ambient_pressure(ambient_pressure)
Set the ambient pressure in hPa at any time to adjust CO2 calculations.
- Parameters:
ambient_pressure (int) – The ambient pressure in hPa, constrained to the range [0, 65535].
UIFLOW2:
- CO2Unit.set_force_calibration(target_co2)
Force the sensor to recalibrate with a given current CO2 level.
- Parameters:
target_co2 (int) – The current CO2 concentration to be used for recalibration.
UIFLOW2:
- CO2Unit.get_calibration_enabled()
Get whether automatic self-calibration (ASC) is enabled or disabled.
UIFLOW2:
- CO2Unit.set_calibration_enabled(enabled)
Enable or disable automatic self-calibration (ASC).
- Parameters:
enabled (bool) – Set to True to enable ASC, or False to disable it.
UIFLOW2:
- CO2Unit.set_start_low_periodic_measurement()
Set the sensor into low power working mode, with about 30 seconds per measurement.
UIFLOW2:
- CO2Unit.data_isready()
Check if new data is available from the sensor.
- CO2Unit.save_to_eeprom()
Save temperature offset, altitude offset, and self-calibration enable settings to EEPROM.
UIFLOW2:
- CO2Unit.get_serial_number()
Get a unique serial number for this sensor.
UIFLOW2:
- CO2Unit.set_self_test()
Perform a self-test, which can take up to 10 seconds.
UIFLOW2:
- CO2Unit.set_factory_reset()
Reset all configuration settings stored in the EEPROM and erase the FRC and ASC algorithm history.
UIFLOW2:
- CO2Unit.reinit()
Reinitialize the sensor by reloading user settings from EEPROM.
UIFLOW2:
- CO2Unit.set_single_shot_measurement_all()
Set the sensor to perform a single-shot measurement for CO2, humidity, and temperature.
- CO2Unit.set_single_shot_measurement_ht()
Set the sensor to perform a single-shot measurement for humidity and temperature.
- CO2Unit.set_sleep_mode()
Put the sensor into sleep mode to reduce current consumption.
- CO2Unit.set_wake_up()
Wake up the sensor from sleep mode into idle mode.
- CO2Unit.write_cmd(cmd_wr, value)
Write a command to the sensor.
- CO2Unit.read_response(num)
Read the sensor’s response.
- Parameters:
num (int) – The number of bytes to read from the sensor.