MHZ19: Difference between revisions
mNo edit summary |
(Add link to MH-Z19B) |
||
(14 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
|Picture=mhz19.jpg | |Picture=mhz19.jpg | ||
|Omschrijving=Some research into the MH-Z19 CO<sub>2</sub> sensor | |Omschrijving=Some research into the MH-Z19 CO<sub>2</sub> sensor | ||
|Status= | |Status=Completed | ||
|Contact=bertrik | |Contact=bertrik | ||
}} | }} | ||
Line 20: | Line 20: | ||
The MH-Z19 is the cheapest optical CO<sub>2</sub> sensor I could find on AliExpress, about E22,-. | The MH-Z19 is the cheapest optical CO<sub>2</sub> sensor I could find on AliExpress, about E22,-. | ||
<b> | |||
UPDATE: | |||
See also [https://geektimes.ru/post/285572/ this page on geektimes.ru] that explains some of the mysteries around this sensor that I didn't figure out. | |||
</b> | |||
See also: [[MH-Z19B]] | |||
== Status == | == Status == | ||
The sensor works as intended, some software has been written to read the CO2 level and publish measurements it on MQTT. | The sensor works as intended, some software has been written to read the CO2 level and publish measurements it on MQTT. | ||
It is also clear how to change the measurement range from 0-2000ppm to 0-5000ppm in software. | It is also clear how to change the measurement range from 0-2000ppm to 0-5000ppm in software. | ||
Currently, it's running in Space3, publishing its value on a topic on MQTT, you can read this as follows: | |||
mosquitto_sub -h revspace.nl -t revspace/sensors/co2/# -v | |||
See also https://revgraph.bewaar.me/dashboard/db/all-co2 | |||
Next steps: | Next steps: | ||
Line 32: | Line 44: | ||
== Hardware and reference data == | == Hardware and reference data == | ||
See [http://www.winsen-sensor.com/products/ndir-co2-sensor/mh-z19.html the manufacturer MH-Z19 page]. | See [http://www.winsen-sensor.com/products/ndir-co2-sensor/mh-z19.html the manufacturer MH-Z19 page]. | ||
[https://forum.mysensors.org/topic/7761/mh-z19-teardown/2 Here's a teardown] of the MH-Z19 showing what it actually looks like inside. | |||
Basically a light source and curved mirror focusing the light onto a photo diode with an optical filter tuned to a CO2 peak. | |||
It has an STM32F103 processor inside, the same kind as the one on the famous 'blue pill' board. | |||
== Software == | == Software == | ||
See [https://github.com/bertrik/mhz19 this github repo] for code using this sensor with an ESP8266 board (WeMos D1 mini). | See [https://github.com/bertrik/mhz19 this github repo] for code using this sensor with an ESP8266 board (WeMos D1 mini). | ||
It publishes the CO<sub>2</sub> concentration to topic | It publishes the CO<sub>2</sub> concentration to a MQTT topic every 5 seconds. | ||
To connect the Wemos to the MHZ19: | |||
* GND to both the Wemos and MHZ19 GND pin | |||
* 5V to the Wemos +5V pin and MHZ19 Vin pin | |||
* Wemos D1 pin to the MHZ19 TX pin | |||
* Wemos D2 pin to the MHZ19 RX pin | |||
It seems that support for this sensor was recently added to [https://www.esp8266.nu/index.php/ESPEasy ESPEasy]. | It seems that support for this sensor was recently added to [https://www.esp8266.nu/index.php/ESPEasy ESPEasy]. | ||
=== | == Setting the measurement range == | ||
The following command sequences can be used to configure the measurement range of the sensor: | |||
* 1000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x03, 0xE8, 0x7B | |||
* 2000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x8F | |||
* 3000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x0B, 0xB8, 0xA3 | |||
* 5000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB | |||
== Experimentation == | |||
Normally, this sensor is read out using a command/response sequence over serial (9600,8N1). | Normally, this sensor is read out using a command/response sequence over serial (9600,8N1). | ||
The following commands are known from the datasheet: | The following commands are known from the datasheet: | ||
* 0x86: gas concentration reading | * 0x86: gas concentration reading | ||
* 0x87: calibrate zero point | * 0x87: calibrate zero point, I advise to AVOID SENDING THIS COMMAND, unless you know exactly what you are doing | ||
* 0x88: calibrate span point | * 0x88: calibrate span point, I advise to AVOID SENDING THIS COMMAND, unless you know exactly what you are doing | ||
The MH-Z19B datasheet additionally mentions to following commands: | The MH-Z19B datasheet additionally mentions to following commands: | ||
* 0x79: ABC logic on/off | * 0x79: ABC logic on/off (ABC = automatic baseline correction) | ||
* 0x99: Sensor detection range setting, this command can be used to set the measurement range (e.g. 0-2000ppm or 0-5000ppm) | * 0x99: Sensor detection range setting, this command can be used to set the measurement range (e.g. 0-2000ppm or 0-5000ppm) | ||
It appears that these commands work on the MH-Z19 too, although with a little different command layout. | It appears that these commands work on the MH-Z19 too, although with a little different command layout. | ||
Specifically, the range is not put into bytes 3/4 but in bytes 6/7 of the command. | Specifically, the range for command 0x99 is not put into bytes 3/4 but in bytes 6/7 of the command. | ||
This paragraph describes some experiments done to discover other commands than the ones mentioned in the datasheet. | |||
=== command 0x00-0x30 === | === command 0x00-0x30 === | ||
No response. | No response. | ||
Line 57: | Line 88: | ||
=== command 0x31-0x6F === | === command 0x31-0x6F === | ||
Untested. | Untested. | ||
=== command 0x62 (device name / id) === | |||
According to the information at https://geektimes.ru/post/285572/ | |||
this command should return a kind of string, I couldn't get this to work unfortunately | |||
<s> | |||
Command 0x62 seems to query the sensor for a device name, either "KB200" or "MODBUS" | |||
</s> | |||
=== command 0x70 === | === command 0x70 === | ||
Line 63: | Line 101: | ||
9F 83 07 7D 00 00 | 9F 83 07 7D 00 00 | ||
</pre> | </pre> | ||
The value 0x9F83 looks similar to the output of command 0x84. | The value 0x9F83 (40835 in decimal) looks similar to the output of command 0x84. | ||
The value 0x077d looks similar to the output of command 0x85. | The value 0x077d (1917 in decimal) looks similar to the output of command 0x85. | ||
=== command 0x71, 0x72 === | === command 0x71, 0x72 === | ||
Line 73: | Line 111: | ||
=== command 0x73 === | === command 0x73 === | ||
<pre>9F 86 00 00 3D 40</pre> | <pre>9F 86 00 00 3D 40</pre> | ||
0x9F86 looks similar to output of command 0x84. | 0x9F86 = 40838 decimal, looks similar to output of command 0x84. | ||
0x3d40 = 15680 decimal. | 0x3d40 = 15680 decimal. | ||
<pre>07 8C 00 00 3D 04</pre> | <pre>07 8C 00 00 3D 04</pre> | ||
* 0x078c = 1932 decimal | |||
* the 0x3D is temperature I think | * the 0x3D is temperature I think | ||
* the 0x04 is the 'status' byte | * the 0x04 is the 'status' byte | ||
Line 89: | Line 128: | ||
Some other time | Some other time | ||
<pre>9F A1 00 80 00 00</pre> | <pre>9F A1 00 80 00 00</pre> | ||
=== command 0x85 === | |||
=== command 0x85 (read CO2 + alarm level?) === | |||
This command gives a response as follows: | This command gives a response as follows: | ||
<pre> | <pre> | ||
Line 113: | Line 153: | ||
* SS is some kind of status byte, this byte always has only one bit set! | * SS is some kind of status byte, this byte always has only one bit set! | ||
* Uh/Ul is some unknown value, perhaps related to pressure? After booting the sensor, it starts out at 15000 exactly, then typically settles to about 10500. | * Uh/Ul is some unknown value, perhaps related to pressure? After booting the sensor, it starts out at 15000 exactly, then typically settles to about 10500. | ||
According to the geektimes.ru page, this unknown value is related to the minimum CO2 uncorrected concentration measured in the past day. So I guess this is a relevant parameter from the ABC-algorithm. | |||
* Cs is the checksum | * Cs is the checksum | ||
Line 138: | Line 179: | ||
<pre> | <pre> | ||
HH LL TT SS U1 U2 | HH LL TT SS U1 U2 | ||
RAW: 00 80 47 01 3A 98 | RAW: 00 80 47 01 3A 98 -> CO2 = 128 ppm | ||
RAW: 07 D0 47 01 3A 98 -> 0x7d0 = 2000 decimal, this is the configured measurement range of the sensor with command 0x99 | RAW: 07 D0 47 01 3A 98 -> 0x7d0 = 2000 decimal, this is the configured measurement range of the sensor with command 0x99 | ||
RAW: 00 05 47 01 3A 98 | RAW: 00 05 47 01 3A 98 -> CO2 = 5 ppm | ||
RAW: 01 2D 47 01 3A 98 | RAW: 01 2D 47 01 3A 98 -> CO2 = .. | ||
RAW: 28 97 47 01 3A 98 | RAW: 28 97 47 01 3A 98 -> CO2 = 10391 | ||
RAW: 28 97 47 01 3A 98 | RAW: 28 97 47 01 3A 98 | ||
RAW: 28 97 47 01 3A 98 | RAW: 28 97 47 01 3A 98 |
Latest revision as of 22:52, 1 October 2019
Project MHZ19 | |
---|---|
Some research into the MH-Z19 CO2 sensor | |
Status | Completed |
Contact | bertrik |
Last Update | 2019-10-01 |
Introduction
This page is about the MH-Z19 CO2 sensor and some experiments done with it.
This sensor gives a digital (serial) output of the CO2 concentration in air, in parts-per-million (ppm). It uses the optical measurement principle of measuring CO2, which should be much more accurate than the inexpensive electro-chemical sensors you can find. As far as I know, the optical measurement principle uses a broadband light-source to send some light through an air-sample. The sensor then looks at the relative intensity of the light at two different frequencies. The CO2 gas inside the air absorbs light strongly at very specific wavelengths, allowing a determination of the concentration (ppm) of CO2. This is then compensated for temperature (and pressure?) for increased accuracy. See also wikipedia for this measurement principle. A new measurement is started every 5 seconds, you can actually see a small amount of light coming out of the sensor while it's measuring.
The MH-Z19 is the cheapest optical CO2 sensor I could find on AliExpress, about E22,-.
UPDATE: See also this page on geektimes.ru that explains some of the mysteries around this sensor that I didn't figure out.
See also: MH-Z19B
Status
The sensor works as intended, some software has been written to read the CO2 level and publish measurements it on MQTT. It is also clear how to change the measurement range from 0-2000ppm to 0-5000ppm in software.
Currently, it's running in Space3, publishing its value on a topic on MQTT, you can read this as follows:
mosquitto_sub -h revspace.nl -t revspace/sensors/co2/# -v
See also https://revgraph.bewaar.me/dashboard/db/all-co2
Next steps:
- play a bit more with the MH-Z19B commands and document what works and what doesn't.
- in particular, look at the alarm output (officially this output is not supported) and try to find if it is possible to set the alarm limit setting. The alarm output seems to toggle low/high on reboot of the sensor, so at least the required hardware to pull it low / high is present inside the sensor module.
- figure out what the unknown value is that comes out of the sensor.
Hardware and reference data
See the manufacturer MH-Z19 page.
Here's a teardown of the MH-Z19 showing what it actually looks like inside. Basically a light source and curved mirror focusing the light onto a photo diode with an optical filter tuned to a CO2 peak. It has an STM32F103 processor inside, the same kind as the one on the famous 'blue pill' board.
Software
See this github repo for code using this sensor with an ESP8266 board (WeMos D1 mini). It publishes the CO2 concentration to a MQTT topic every 5 seconds.
To connect the Wemos to the MHZ19:
- GND to both the Wemos and MHZ19 GND pin
- 5V to the Wemos +5V pin and MHZ19 Vin pin
- Wemos D1 pin to the MHZ19 TX pin
- Wemos D2 pin to the MHZ19 RX pin
It seems that support for this sensor was recently added to ESPEasy.
Setting the measurement range
The following command sequences can be used to configure the measurement range of the sensor:
- 1000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x03, 0xE8, 0x7B
- 2000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x8F
- 3000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x0B, 0xB8, 0xA3
- 5000 ppm range: 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB
Experimentation
Normally, this sensor is read out using a command/response sequence over serial (9600,8N1). The following commands are known from the datasheet:
- 0x86: gas concentration reading
- 0x87: calibrate zero point, I advise to AVOID SENDING THIS COMMAND, unless you know exactly what you are doing
- 0x88: calibrate span point, I advise to AVOID SENDING THIS COMMAND, unless you know exactly what you are doing
The MH-Z19B datasheet additionally mentions to following commands:
- 0x79: ABC logic on/off (ABC = automatic baseline correction)
- 0x99: Sensor detection range setting, this command can be used to set the measurement range (e.g. 0-2000ppm or 0-5000ppm)
It appears that these commands work on the MH-Z19 too, although with a little different command layout. Specifically, the range for command 0x99 is not put into bytes 3/4 but in bytes 6/7 of the command.
This paragraph describes some experiments done to discover other commands than the ones mentioned in the datasheet.
command 0x00-0x30
No response.
command 0x31-0x6F
Untested.
command 0x62 (device name / id)
According to the information at https://geektimes.ru/post/285572/
this command should return a kind of string, I couldn't get this to work unfortunately
Command 0x62 seems to query the sensor for a device name, either "KB200" or "MODBUS"
command 0x70
This command gives a response as follows:
9F 83 07 7D 00 00
The value 0x9F83 (40835 in decimal) looks similar to the output of command 0x84.
The value 0x077d (1917 in decimal) looks similar to the output of command 0x85.
command 0x71, 0x72
These commands give a response as follows:
00 00 00 00 00 00
command 0x73
9F 86 00 00 3D 40
0x9F86 = 40838 decimal, looks similar to output of command 0x84. 0x3d40 = 15680 decimal.
07 8C 00 00 3D 04
- 0x078c = 1932 decimal
- the 0x3D is temperature I think
- the 0x04 is the 'status' byte
command 0x80 - 0x83
No response.
command 0x84
This command gives a response as follows:
9F 88 00 00 00 00
The 0x88 byte seems to go up and down a bit.
Some other time
9F A1 00 80 00 00
command 0x85 (read CO2 + alarm level?)
This command gives a response as follows:
07 7D 01 D5 27 10
The 0x1D5 value is the same as the CO2 concentration reading from command 0x86! 0x077D is equivalent to 1917 decimal. 0x2710 is equivalent to 10000 decimal exactly.
Perhaps the 0x2710 value is the alarm value?
Command 0x86 (read concentration)
Command 0x86 is the command to send to just read out the most recent ppm value.
A response to command 0x86 typically looks like this:
0xFF CM HH LL TT SS Uh Ul CS
where
- CM is the command repeated back
- HH/LL is the CO2 ppm value, high/low part
- TT is the temperature in degrees Celcius, plus 40. For example, when temperature is 25 deg C, then TT = 0x41
- SS is some kind of status byte, this byte always has only one bit set!
- Uh/Ul is some unknown value, perhaps related to pressure? After booting the sensor, it starts out at 15000 exactly, then typically settles to about 10500.
According to the geektimes.ru page, this unknown value is related to the minimum CO2 uncorrected concentration measured in the past day. So I guess this is a relevant parameter from the ABC-algorithm.
- Cs is the checksum
Values TT, SS and Uh/Ul are undocumented.
command 0x89-0x8F
No response is returned, however command 0x8d seems to reset the sensor.
command 0x99 (range)
According to the MH-Z19B datasheet, you can configure the measurement range by putting the desired range in byte 3 and 4. However, unlike what the MH-Z19B datasheet says, you can set the range using the following command (in this case 0x07d0 = 2000 ppm in byte 6 and 7):
0xFF 0x01 0x99 0x00 0x00 0x00 0x07 0xD0 0x8F <cmd> ^-range-^
Experimenting a bit: Sending all zeros gives a response of
01 00 00 00 00 00
Log of MH-Z19 response at startup
Below is a log of the sensor response to the 0x86 measurement command while starting up. The first couple of measurement seem to be invalid.
HH LL TT SS U1 U2 RAW: 00 80 47 01 3A 98 -> CO2 = 128 ppm RAW: 07 D0 47 01 3A 98 -> 0x7d0 = 2000 decimal, this is the configured measurement range of the sensor with command 0x99 RAW: 00 05 47 01 3A 98 -> CO2 = 5 ppm RAW: 01 2D 47 01 3A 98 -> CO2 = .. RAW: 28 97 47 01 3A 98 -> CO2 = 10391 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 01 3A 98 RAW: 28 97 47 40 3A 98 RAW: 28 97 47 40 3A 98 RAW: 03 84 47 40 2B 43 -> first sample with U different from 0x3a98 (15000 decimal) RAW: 03 85 47 40 2B 19 RAW: 03 86 47 40 2A F9 RAW: 03 87 47 40 2A E1 -> CO2 = 903 ppm, T = 31 deg C, Status = 0x40, Unknown = 10977
The first measurement shows a ppm value of 128, a temperature of 31 degrees C, a "status" byte of 01 and the "unknown" value of 0x3a98 (= 15000). The second measurement shows a high ppm value of 2000 ppm (the max value within the ppm range). The third measurement shows a low ppm value of 5 ppm. The fourth measurement shows a ppm value of 301 ppm. The fifth measurement shows a very high ppm value of 10391. The final measurement shows a realistic indoors ppm value of 0x387 = 903 ppm. The "unknown value" (byte 4/5) typically settles down to 10500 or so.
So, it takes some time before the measurement stabilizes, proposed heuristic for a valid reading:
- "status byte" has to be 0x40
- "unknown value" has to be lower than 15000