The Winsen MH-Z19B talks 9600 baud, 8N1, 3.3 V logic (5 V tolerant), and answers a nine byte read command. Aliases: MH-Z19, MH-Z19B, MH-Z19C, MHZ19, Winsen, CO2 sensor, carbon dioxide sensor, NDIR.
Pins
| Pin | Name | What it does |
|---|---|---|
| 1 | VIN |
Supply, 4.5 V to 5.5 V. Under 4.5 V the sensor does nothing. |
| 2 | GND |
Ground. |
| 3 | VO |
Analog output, 0.4 V at 0 ppm to 2.0 V at the top of the range. |
| 4 | PWM |
A 1004 ms cycle whose high time is the CO2. |
| 5 | HD |
Held low for 7 seconds or more: zero calibration. Leave it open. |
| 6 | RX |
Commands in, 3.3 V logic. |
| 7 | TX |
Answers out, 3.3 V logic. |
The serial commands
Every command is nine bytes: FF 01, the command, five data bytes and a check, which is
0xFF minus the sum of bytes 1 to 7, plus 1. Only the read is answered.
| Command | Bytes | What happens |
|---|---|---|
| Read CO2 | FF 01 86 00 00 00 00 00 79 |
Answered FF 86 HIGH LOW and five more bytes: the ppm is HIGH * 256 + LOW. |
| Zero | FF 01 87 00 00 00 00 00 78 |
The air right now becomes 400 ppm. |
| Span | FF 01 88 HIGH LOW 00 00 00 check |
The air right now becomes that many ppm. Zero first. |
| ABC on / off | FF 01 79 A0 ... / FF 01 79 00 ... |
Automatic baseline correction, on from the factory. |
| Range | FF 01 99 00 00 00 07 D0 8F |
2000 ppm. 27 10 2F for 10000; 5000 also works. |
Byte 4 of the read answer is the sensor's temperature plus 40 in degrees C. The datasheet leaves it blank, but the sensor fills it and the MHZ19 library reads it.
PWM and Vo
The PWM output is high for 2 ms, then for 1000 ms * C / range, then low to the end of
the 1004 ms cycle. Measure the high time TH and the low time TL and work it out:
Cppm = range * (TH - 2 ms) / (TH + TL - 4 ms)
pulseIn(pin, HIGH, 1100000) gives TH in microseconds. The analog output is
Vo = 0.4 V + 1.6 V * C / range, so on an Uno analogRead() reads about 82 counts at
0 ppm and 409 at the top of the range.
Properties
| Property | What it is |
|---|---|
co2 |
The air, 0 to 10000 ppm. The slider sets it while it runs. |
temperature |
What byte 4 of the read answer carries, in degrees C. |
range |
2000, 5000 or 10000 ppm. Every output is held to it. |
Libraries
MHZ19.h (WifWaf's MH-Z19 library): myMHZ19.begin(mySerial) with a SoftwareSerial or a
spare hardware port at 9600 baud, then myMHZ19.getCO2() and myMHZ19.getTemperature().
autoCalibration(false), calibrate() and setRange(2000) send the commands above, and
errorCode is RESULT_OK after a good read. Or send the nine read bytes yourself and
read the nine that come back.
What the model gets right
The command frames, the check byte, the read answer, the PWM cycle and formula, the Vo scale, the 7 second HD calibration and the 4.5 V minimum supply are the Winsen manual's (V1.3). The zero and span calibrations move the readings the way they do on the real sensor, and the range holds the serial reading, the PWM and Vo alike.
What it does not model
The real sensor needs 3 minutes to warm up and takes up to 2 minutes to follow a change in
the air; here the reading follows the slider at once, exactly, with none of its 50 ppm plus
5 percent error. The automatic baseline correction runs once a day on the real sensor; the
command is taken here, but no run lasts a day. Commands the datasheet does not list (the
library's getRange(), getABC(), getVersion() and the unlimited read 0x85) get no
answer, because the datasheet does not say what the sensor sends back.
Common mistakes
Powering it from 3.3 V: it needs 5 V. Crossing the serial pins the wrong way: the sensor's
TX goes to your RX pin. Reading it on the Uno's pins 0 and 1, which the serial monitor
uses: use SoftwareSerial on two other pins. Calling calibrate() in a stuffy room: zero
means 400 ppm, so only zero it in fresh outdoor air.
See it in action
MH-Z19B CO2 monitor reads the sensor with the MHZ19 library over SoftwareSerial and prints the CO2 and how fresh the air is. Open it at /templates.