Skip to main content

CO2 Sensor

Digital
input
Small RAM
0.4.5

#Description

This sensor is able to detect the CO2 particles in the air.

Unfortunately the CO2 sensor is currently not supported in combination with the Arduino MKR WiFi 1010 board.

This component requires 2 pins.

#Units

  • Percent%
  • Parts per Millionppm

#Functions



#JavaScript / Node.js

ClassCO2Sensorextends ComponentBase

CO2 Sensor component for reading CO2 concentration from Grove MH-Z16 sensor. This is a UART component that communicates via SoftwareSerial (RX/TX pins).

#Constructor options

OptionTypeRequiredDescription
rxPinstringrequired
txPinstringrequired

#Events

EventParametersDescription
changeppm: number

#Methods

get valuenumber | undefinedfrom ComponentBase

Get the current value of the component.

#Example

JavaScript
import { Board, CO2Sensor } from "@blokdots/components";

const board = new Board("/dev/ttyACM0");
await board.open();

const cO2Sensor = new CO2Sensor({ board, rxPin: "D5", txPin: "D5" });

cO2Sensor.on("change", (value) => {
  console.log("CO2Sensor changed:", value);
});


#C++ (Arduino)

ClassCO2Sensorextends ComponentBase

Grove CO2 Sensor (MH-Z16) — UART mode (Seeed) - Baud: 9600 - Read command: FF 01 86 00 00 00 00 00 CS - Response: 9 bytes * CO2 ppm = data[2] * 256 + data[3] Emits ONE event: - evtChanged : when ppm changes enough (threshold + optional rate limit) Output: - ppm() / emittedValue() -> last emitted CO2 concentration

#Required libraries

  • #include <SoftwareSerial.h>SoftwareSerial

#Setup

C++
#include "blokdots.h"

CO2Sensor myComponent(eventBus, 5, 5, MY_EVENT);

#Constructor

CO2Sensor(bus, rxPin, txPin, evtChanged, sampleIntervalMs, minEmitIntervalMs, changeThresholdPpm)
ParameterTypeDefault
busEventBusrequired
rxPinuint8_trequired
txPinuint8_trequired
evtChangeduint8_trequired
sampleIntervalMsuint16_t1000
minEmitIntervalMsuint16_t0
changeThresholdPpmint1

#Methods

ppm()int
emittedValue()int
rawPpm()int

Optional: most recent parsed sample (may be newer than last emit)