Erriez MH-Z19B CO2 sensor library for Arduino
1.0.0
This is a MH-Z19B CO2 sensor library for Arduino with a small footprint.
|
Class ErriezMHZ19B. More...
#include <ErriezMHZ19B.h>
Public Member Functions | |
ErriezMHZ19B (Stream *serial) | |
Constructor with serial Stream. More... | |
~ErriezMHZ19B () | |
Destructor. More... | |
bool | detect () |
Detect MHZ19B sensor. More... | |
bool | isWarmingUp () |
Check if sensor is warming-up after power-on. More... | |
bool | isReady () |
Check minimum interval between CO2 reads. More... | |
int16_t | readCO2 () |
Read CO2 from sensor. More... | |
int8_t | getVersion (char *version, uint8_t versionLen) |
Get firmware version (NOT DOCUMENTED) More... | |
int8_t | setRange2000ppm () |
Set CO2 range 2000 ppm. More... | |
int8_t | setRange5000ppm () |
Set CO2 range 5000 ppm. More... | |
int16_t | getRange () |
Get CO2 range in PPM (NOT DOCUMENTED) More... | |
int8_t | setAutoCalibration (bool calibrationOn) |
Enable or disable automatic calibration. More... | |
int8_t | getAutoCalibration () |
Get status automatic calibration (NOT DOCUMENTED) More... | |
int8_t | startZeroCalibration () |
Start Zero Point Calibration manually at 400ppm. More... | |
int8_t | sendCommand (uint8_t cmd, byte b3=0, byte b4=0, byte b5=0, byte b6=0, byte b7=0) |
Send serial command to sensor and read response. More... | |
Public Attributes | |
uint8_t | rxBuffer [MHZ19B_SERIAL_RX_BYTES] |
Class ErriezMHZ19B.
Definition at line 91 of file ErriezMHZ19B.h.
ErriezMHZ19B::ErriezMHZ19B | ( | Stream * | serial | ) |
Constructor with serial Stream.
serial | Serial Stream pointer. |
Definition at line 49 of file ErriezMHZ19B.cpp.
ErriezMHZ19B::~ErriezMHZ19B | ( | ) |
Destructor.
The serial Stream pointer is cleared and requires a new constructor to reuse it again.
Definition at line 58 of file ErriezMHZ19B.cpp.
bool ErriezMHZ19B::detect | ( | ) |
Detect MHZ19B sensor.
true | Sensor detected. |
false | Sensor not detected, check wiring/power. |
Definition at line 70 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::getAutoCalibration | ( | ) |
Get status automatic calibration (NOT DOCUMENTED)
<0 | MH-Z19B response error codes. |
1 | Automatic calibration on. |
0 | Automatic calibration off. |
Definition at line 296 of file ErriezMHZ19B.cpp.
int16_t ErriezMHZ19B::getRange | ( | ) |
Get CO2 range in PPM (NOT DOCUMENTED)
This function verifies valid read ranges of 2000 or 5000 ppm.
Note: Other ranges may be returned, but are undocumented and marked as invalid.
<0 | MH-Z19B response error codes. |
MHZ19B_RANGE_2000 | Range 2000 ppm. |
MHZ19B_RANGE_5000 | Range 5000 ppm (default). |
Definition at line 252 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::getVersion | ( | char * | version, |
uint8_t | versionLen | ||
) |
Get firmware version (NOT DOCUMENTED)
This is an undocumented command, but most sensors returns ASCII "0430 or "0443".
version | Returned character pointer to version (must be at least 5 Bytes) Only valid when return is set to MHZ19B_RESULT_OK. |
versionLen | Number of characters including NULL of version buffer. |
Definition at line 189 of file ErriezMHZ19B.cpp.
bool ErriezMHZ19B::isReady | ( | ) |
Check minimum interval between CO2 reads.
Not described in the datasheet, but it is the same frequency as the built-in LED blink.
true | Ready to call readCO2(). |
false | Conversion not completed. |
Definition at line 133 of file ErriezMHZ19B.cpp.
bool ErriezMHZ19B::isWarmingUp | ( | ) |
Check if sensor is warming-up after power-on.
The datasheet mentions a startup delay of 3 minutes before reading CO2.
Experimentally discovered, the sensor may return CO2 data earlier. To speed-up the boot process, it is possible to check if the CO2 value changes to abort the warming-up, for example when the MCU is reset and keep the sensor powered.
Recommended to disable this option for deployment by disabling macro MHZ19B_SMART_WARMING_UP in header file.
true | Sensor is warming-up. |
false | Sensor is ready to use. |
Definition at line 96 of file ErriezMHZ19B.cpp.
int16_t ErriezMHZ19B::readCO2 | ( | ) |
Read CO2 from sensor.
<0 | MH-Z19B response error codes. |
0..399 | ppm Incorrect values. Minimum value starts at 400ppm outdoor fresh air. |
400..1000 | ppm Concentrations typical of occupied indoor spaces with good air exchange. |
1000..2000 | ppm Complaints of drowsiness and poor air quality. Ventilation is required. |
2000..5000 | ppm Headaches, sleepiness and stagnant, stale, stuffy air. Poor concentration, loss of attention, increased heart rate and slight nausea may also be present. Higher values are extremely dangerous and cannot be measured. |
Definition at line 158 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::sendCommand | ( | uint8_t | cmd, |
byte | b3 = 0 , |
||
byte | b4 = 0 , |
||
byte | b5 = 0 , |
||
byte | b6 = 0 , |
||
byte | b7 = 0 |
||
) |
Send serial command to sensor and read response.
Send command to sensor and read response, protected with a receive timeout.
Result is available in public rxBuffer[9].
cmd | Command Byte |
b3 | Byte 3 (default 0) |
b4 | Byte 4 (default 0) |
b5 | Byte 5 (default 0) |
b6 | Byte 6 (default 0) |
b7 | Byte 7 (default 0) |
Definition at line 345 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::setAutoCalibration | ( | bool | calibrationOn | ) |
Enable or disable automatic calibration.
calibrationOn | true: Automatic calibration on. false: Automatic calibration off. |
Definition at line 281 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::setRange2000ppm | ( | ) |
Set CO2 range 2000 ppm.
Definition at line 221 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::setRange5000ppm | ( | ) |
Set CO2 range 5000 ppm.
Definition at line 233 of file ErriezMHZ19B.cpp.
int8_t ErriezMHZ19B::startZeroCalibration | ( | ) |
Start Zero Point Calibration manually at 400ppm.
The sensor must be powered-up for at least 20 minutes in fresh air at 400ppm room temperature. Then call this function once to execute self calibration.
Recommended to use this function when auto calibrate turned off.
Definition at line 321 of file ErriezMHZ19B.cpp.