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.
Public Member Functions | Public Attributes | List of all members
ErriezMHZ19B Class Reference

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]
 

Detailed Description

Class ErriezMHZ19B.

Definition at line 91 of file ErriezMHZ19B.h.

Constructor & Destructor Documentation

◆ ErriezMHZ19B()

ErriezMHZ19B::ErriezMHZ19B ( Stream *  serial)

Constructor with serial Stream.

Parameters
serialSerial Stream pointer.

Definition at line 49 of file ErriezMHZ19B.cpp.

◆ ~ErriezMHZ19B()

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.

Member Function Documentation

◆ detect()

bool ErriezMHZ19B::detect ( )

Detect MHZ19B sensor.

Return values
trueSensor detected.
falseSensor not detected, check wiring/power.

Definition at line 70 of file ErriezMHZ19B.cpp.

◆ getAutoCalibration()

int8_t ErriezMHZ19B::getAutoCalibration ( )

Get status automatic calibration (NOT DOCUMENTED)

Return values
<0MH-Z19B response error codes.
1Automatic calibration on.
0Automatic calibration off.

Definition at line 296 of file ErriezMHZ19B.cpp.

◆ getRange()

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.

Return values
<0MH-Z19B response error codes.
MHZ19B_RANGE_2000Range 2000 ppm.
MHZ19B_RANGE_5000Range 5000 ppm (default).

Definition at line 252 of file ErriezMHZ19B.cpp.

◆ getVersion()

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".

Parameters
versionReturned character pointer to version (must be at least 5 Bytes)
Only valid when return is set to MHZ19B_RESULT_OK.
versionLenNumber of characters including NULL of version buffer.
Returns
MH-Z19B response error codes.

Definition at line 189 of file ErriezMHZ19B.cpp.

◆ isReady()

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.

Return values
trueReady to call readCO2().
falseConversion not completed.

Definition at line 133 of file ErriezMHZ19B.cpp.

◆ isWarmingUp()

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.

Return values
trueSensor is warming-up.
falseSensor is ready to use.

Definition at line 96 of file ErriezMHZ19B.cpp.

◆ readCO2()

int16_t ErriezMHZ19B::readCO2 ( )

Read CO2 from sensor.

Return values
<0MH-Z19B response error codes.
0..399ppm Incorrect values. Minimum value starts at 400ppm outdoor fresh air.
400..1000ppm Concentrations typical of occupied indoor spaces with good air exchange.
1000..2000ppm Complaints of drowsiness and poor air quality. Ventilation is required.
2000..5000ppm 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.

◆ sendCommand()

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].

Parameters
cmdCommand Byte
b3Byte 3 (default 0)
b4Byte 4 (default 0)
b5Byte 5 (default 0)
b6Byte 6 (default 0)
b7Byte 7 (default 0)

Definition at line 345 of file ErriezMHZ19B.cpp.

◆ setAutoCalibration()

int8_t ErriezMHZ19B::setAutoCalibration ( bool  calibrationOn)

Enable or disable automatic calibration.

Parameters
calibrationOntrue: Automatic calibration on.
false: Automatic calibration off.
Returns
MH-Z19B response error codes.

Definition at line 281 of file ErriezMHZ19B.cpp.

◆ setRange2000ppm()

int8_t ErriezMHZ19B::setRange2000ppm ( )

Set CO2 range 2000 ppm.

Returns
MH-Z19B response error codes.

Definition at line 221 of file ErriezMHZ19B.cpp.

◆ setRange5000ppm()

int8_t ErriezMHZ19B::setRange5000ppm ( )

Set CO2 range 5000 ppm.

Returns
MH-Z19B response error codes.

Definition at line 233 of file ErriezMHZ19B.cpp.

◆ startZeroCalibration()

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.

Returns
MH-Z19B response error codes.

Definition at line 321 of file ErriezMHZ19B.cpp.


The documentation for this class was generated from the following files: