CO2 Sensor
#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.
#Links
#Units
- Percent%
- Parts per Millionppm
#Functions
#JavaScript / Node.js
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
| Option | Type | Required | Description |
|---|---|---|---|
rxPin | string | required | — |
txPin | string | required | — |
#Events
| Event | Parameters | Description |
|---|---|---|
change | ppm: number | — |
#Methods
get value→ number | undefinedfrom ComponentBaseGet the current value of the component.
#Example
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)
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
#include "blokdots.h"
CO2Sensor myComponent(eventBus, 5, 5, MY_EVENT);#Constructor
CO2Sensor(bus, rxPin, txPin, evtChanged, sampleIntervalMs, minEmitIntervalMs, changeThresholdPpm)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
rxPin | uint8_t | required |
txPin | uint8_t | required |
evtChanged | uint8_t | required |
sampleIntervalMs | uint16_t | 1000 |
minEmitIntervalMs | uint16_t | 0 |
changeThresholdPpm | int | 1 |
#Methods
ppm()→ intemittedValue()→ intrawPpm()→ intOptional: most recent parsed sample (may be newer than last emit)