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.
ErriezMHZ19B.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020-2023 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef ERRIEZ_MHZ19B_H_
34 #define ERRIEZ_MHZ19B_H_
35 
36 #include <Arduino.h>
37 
42 // #define MHZ19B_SMART_WARMING_UP
43 
45 #define MHZ19B_WARMING_UP_TIME_MS (3UL * 60000UL)
46 
48 #define MHZ19B_READ_INTERVAL_MS (5UL * 1000UL)
49 
51 #define MHZ19B_SERIAL_RX_BYTES 9
52 
54 #define MHZ19B_SERIAL_RX_TIMEOUT_MS 120
55 
56 // Documented commands
57 #define MHZ19B_CMD_SET_AUTO_CAL 0x79
58 #define MHZ19B_CMD_READ_CO2 0x86
59 #define MHZ19B_CMD_CAL_ZERO_POINT 0x87
60 #define MHZ19B_CMD_CAL_SPAN_POINT 0x88
61 #define MHZ19B_CMD_SET_RANGE 0x99
62 
63 // Not documented commands
64 #define MHZ19B_CMD_GET_AUTO_CAL 0x7D
65 #define MHZ19B_CMD_GET_RANGE 0x9B
66 #define MHZ19B_CMD_GET_VERSION 0xA0
67 
71 typedef enum {
78 
82 typedef enum {
86 
87 
92 {
93 public:
94  ErriezMHZ19B(Stream *serial);
95  ~ErriezMHZ19B();
96 
97  // Detect sensor by checking range response
98  bool detect();
99 
100  // Minimum wait time after power-on
101  bool isWarmingUp();
102 
103  // Minimum wait time between CO2 reads
104  bool isReady();
105 
106  // Read CO2 value
107  int16_t readCO2();
108 
109  // Get firmware version (NOT DOCUMENTED)
110  int8_t getVersion(char *version, uint8_t versionLen);
111 
112  // Set/get CO2 range, default 5000ppm)
113  int8_t setRange2000ppm();
114  int8_t setRange5000ppm();
115  int16_t getRange(); // (NOT DOCUMENTED)
116 
117  // Set and get ABC, default on (Automatic Baseline Correction)
118  int8_t setAutoCalibration(bool calibrationOn);
119  int8_t getAutoCalibration(); // (NOT DOCUMENTED)
120 
121  // Start Zero Point Calibration manually at 400ppm
122  int8_t startZeroCalibration();
123 
124  // Serial communication
125  int8_t sendCommand(uint8_t cmd, byte b3=0, byte b4=0, byte b5=0, byte b6=0, byte b7=0);
126 
127  // Global receive buffer
128  uint8_t rxBuffer[MHZ19B_SERIAL_RX_BYTES];
129 
130 private:
131  Stream *_serial;
132  unsigned long _tLastReadCO2;
133 
134  // CRC check on serial transmit/receive buffer
135  uint8_t calcCRC(uint8_t *data);
136 };
137 
138 #endif // ERRIEZ_MHZ19B_H_
#define MHZ19B_SERIAL_RX_BYTES
Fixed 9 Bytes response.
Definition: ErriezMHZ19B.h:51
MHZ19B_Range_e
PPM range.
Definition: ErriezMHZ19B.h:82
@ MHZ19B_RANGE_5000
Range 5000 ppm (Default)
Definition: ErriezMHZ19B.h:84
@ MHZ19B_RANGE_2000
Range 2000 ppm.
Definition: ErriezMHZ19B.h:83
MHZ19B_Result_e
Response on a command.
Definition: ErriezMHZ19B.h:71
@ MHZ19B_RESULT_ERR_TIMEOUT
Response timeout.
Definition: ErriezMHZ19B.h:75
@ MHZ19B_RESULT_OK
Response OK.
Definition: ErriezMHZ19B.h:72
@ MHZ19B_RESULT_ERR_CRC
Response CRC error.
Definition: ErriezMHZ19B.h:74
@ MHZ19B_RESULT_ARGUMENT_ERROR
Response argument error.
Definition: ErriezMHZ19B.h:76
@ MHZ19B_RESULT_ERROR
Response error.
Definition: ErriezMHZ19B.h:73
Class ErriezMHZ19B.
Definition: ErriezMHZ19B.h:92
int8_t setAutoCalibration(bool calibrationOn)
Enable or disable automatic calibration.
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.
int8_t getAutoCalibration()
Get status automatic calibration (NOT DOCUMENTED)
int8_t startZeroCalibration()
Start Zero Point Calibration manually at 400ppm.
bool isWarmingUp()
Check if sensor is warming-up after power-on.
ErriezMHZ19B(Stream *serial)
Constructor with serial Stream.
bool detect()
Detect MHZ19B sensor.
int8_t setRange2000ppm()
Set CO2 range 2000 ppm.
bool isReady()
Check minimum interval between CO2 reads.
int16_t getRange()
Get CO2 range in PPM (NOT DOCUMENTED)
int8_t getVersion(char *version, uint8_t versionLen)
Get firmware version (NOT DOCUMENTED)
int8_t setRange5000ppm()
Set CO2 range 5000 ppm.
~ErriezMHZ19B()
Destructor.
int16_t readCO2()
Read CO2 from sensor.