pdf-icon

Arduino Quick Start

Unit CO2L Arduino Tutorial

1. Preparations

Driver Libraries
The M5Unit-ENV library includes drivers and examples for the Unit CO2 / Unit CO2L sensors, making sensor data reading very convenient.

Dependent Libraries
The above driver libraries such as M5UnitUnified and M5Unit-ENV require additional dependency libraries (e.g., M5HAL, M5Utility, etc.). If you are installing via the Arduino Library Manager, please install all dependencies as prompted.

2. Example

Example Explanation
This example uses the Unit CO2L to read air CO2 concentration data. This program is applicable to both Unit CO2L and Unit CO2; the difference is that the Unit CO2L supports low-power single measurements, so you can choose whether to enable this option in your program based on the hardware you are using.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <M5Unified.h>
#include <M5UnitUnified.h>
#include <M5UnitUnifiedENV.h>
auto& lcd = M5.Display;
m5::unit::UnitUnified Units;
m5::unit::UnitCO2 unit;
void setup()
{
M5.begin();
M5.Display.setFont(&fonts::lgfxJapanMinchoP_20);
M5.Display.setTextSize(1);
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
Wire.begin(pin_num_sda, pin_num_scl, 400000U);
if (!Units.add(unit, Wire) || !Units.begin()) {
M5_LOGE("Failed to begin");
M5.Display.clear(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
M5_LOGI("M5UnitUnified has been begun");
M5_LOGI("%s", Units.debugInfo().c_str());
M5.Display.setCursor(0, 0);
M5.Display.clear();
M5.Display.println("Init...");
}
void loop()
{
M5.update();
Units.update();
if (unit.updated()) {
M5.Display.setCursor(0, 0);
M5.Display.clear();
M5.Display.printf("\n>CO2:%u\n>Temperature:%2.2f\n>Humidity:%2.2f", unit.co2(), unit.temperature(),
unit.humidity());
}
}

3. Compile and Upload

    1. Download Mode:
      Before programming, different devices need to enter download mode, and the procedure may vary depending on the main controller. For details, please refer to the device programming download tutorials listed at the bottom of the Arduino IDE Quick Start Guide.
    • For CoreS3, press and hold the reset button (for about 2 seconds) until the internal green LED lights up, then release it. The device will then enter download mode and await programming.
    1. Select the Device Port:
      Click the compile and upload button at the top left of the Arduino IDE, and wait for the program to compile and be uploaded to the device.

4. CO2 Concentration Reading

On This Page