Last active
January 22, 2019 09:03
-
-
Save didasy/c33a112d31445f08ea3b4a06a0d738cd to your computer and use it in GitHub Desktop.
Read MH-Z19 CO2 Sensor Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to wait the sensor to heat up first before attempting to get CO2 concentration data for the first time (approx 3 minutes preheat time). | |
int readCO2() | |
{ | |
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; | |
// command to ask for data | |
byte response[9]; // for answer | |
// co2Serial is a software serial instance | |
co2Serial.write(cmd, 9); //request PPM CO2 | |
// The serial stream can get out of sync. The response starts with 0xff, try to resync. | |
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) { | |
co2Serial.read(); | |
} | |
memset(response, 0, 9); | |
co2Serial.readBytes(response, 9); | |
if (response[1] != 0x86) | |
{ | |
Serial.println("Invalid response from co2 sensor!"); | |
return -1; | |
} | |
byte crc = 0; | |
for (int i = 1; i < 8; i++) { | |
crc += response[i]; | |
} | |
crc = 255 - crc + 1; | |
if (response[8] == crc) { | |
int responseHigh = (int) response[2]; | |
int responseLow = (int) response[3]; | |
int ppm = (256 * responseHigh) + responseLow; | |
return ppm; | |
} else { | |
Serial.println("CRC error!"); | |
return -1; | |
} | |
} |
I forgot that calibration is also possible by hardware, instead of software only.
When you connect the pins 'Hd' and 'Gnd' together for 7s, it should assume current level is 400 ppm.From the manual
Sensor HD pin with low level(0V) and lasting for over 7s (under 400ppm for at least 20 minutes)
So you basically short Hd and Gnd pins for 7s outdoor.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello,
How have you calibrated this sensor? can you please share the whole code with a sketch of sensor wiring?