Package: mh-z19
mh-z19Read CO2 concentration from mh-z19 sensorinstallpip install mh_z19installshow to useUse as python script.pi@raspberrypi:~/mh-z19/pypi $ sudo python -m mh_z19
{'co2': 500}Import module and call read()pi@raspberrypi:~/mh-z19/pypi $ sudo python
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mh_z19
>>> mh_z19.read()
{'co2': 477}
>>>Thesudomight be necessary because mh_z19 module use Serial.The differences of the interface between each Raspberry Pi model are resolved inside this module. For example, serial device name is difference between Raspberry Pi 3 and older model, but mh-z19 module automatically detect the model and read from appropriate serial device.To use mh-z19, once you need to set up enabling serial port device on the Raspberry Pi.
FollowingWikipage might be informative.cablingConnect RPi & mh-z19 as:5V on RPi and Vin on mh-z19GND(0v) on RPi and GND on mh-z19TxD and RxD are connected to cross between RPi and mh-z18Followings are example of cabling, but you can free to use other 5v and 0v Pin on the RPi.pi@raspberrypi:~/mh-z19 $ gpio readall
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |  <---- Vin
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |  <---- Gnd
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |  <---- RxD
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |  <---- TxD
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+Watch CO2 concentration on your browserMONITOR™is a free Remote Monitoring Service to show latest data on the web. You can see the current CO2 concentration value measured by your MH-Z19 device on your smartphone. For detail, please refer thisblog.Calibration, Detection range settings, and ABC(Automatic Baseline Correction) logic on/off.Features about calibration (both MH-Z19 & MH-Z19B), detection range change (MH-Z19B) and ABC logic on/off(MH-Z19B) are implemented at version 0.2.1 or later.I'm afraid I've just only implemented these without test due to lack necessary devices and apparatus for the test, fx: standard concentration CO2 gas, also MH-Z19B module.
If you have these devices or apparatus and try to use these functions generously, I'm really appreciate yourissue reportregardless result were positive or negative.For detail please refer thiswiki.Undocumented response values of 0x86 command.TheRevspace/MHZ19shows values undocumented on the official datasheets(MH-Z19,MH-Z19B). In accordance with this,--alloption add these values in the return json value as follows:sudo python -m mh_z19 --all
{"SS": 232, "UhUl": 10752, "TT": 61, "co2": 818, "temperature": 21}

sudo python3 -m mh_z19 --all
{"TT": 61, "co2": 807, "SS": 232, "temperature": 21, "UhUl": 10752}or callread_all()function as follows:>>> import mh_z19
>>> mh_z19.read_all()
{'SS': 232, 'UhUl': 10738, 'TT': 61, 'co2': 734, 'temperature': 21}
>>>Use specific serial device.In case you should use specific serial device insted of Raspberry Pi default serial device which this library automatically select, for example in case to need to use /dev/ttyUSB0 forFT232 usb-serial converterasissue#12, you can specify serial device by--serial_deviceoption as follows:sudo python -m mh_z19 --serial_device /dev/ttyUSB0How to use without root permission.See thiswiki.How to use in your program.See thiswiki.PWM support.See thiswiki.In case you can't get the value.Even if cabling seems no problem and uart seems to be prepateted well but you can't get sensor value. Asnincube8suggested that thepull upby1-5kΩ registercan be working solution. Thank younincube8!Q&AThe forum is avai athere. Any questions, suggestions, reports are welcome!BlogHow to Measure ROOM CO2 concentration with 20$ sensor "MH-Z19" and Raspberry Pi.Monitoring all over the world with 3G Network for not more than 10$ monthly payment.How to make shareable SD card by Raspberry Pi & PC.ReferencesMH-H19B DataSheet version 1.5MH-H19 DataSheet version 1.0MH-H19B DataSheet version 1.0RevSpacehistory0.1.1  2018.11.05  first version self-forked fromslider.0.1.3  2018.11.06  fix Readme.0.1.4  2018.11.15  revise Readme.0.1.6  2018.11.29  revise Readme.0.2.1  2019.01.18  add followings without test (sorry)
abc_on(), abc_off(), span_point_calibration(),
xero_point_calibration(), detection_range_5000(),
detection_range_2000(), checksum()0.3.5  2019.01.22  Both Python2 & Python3 support0.3.6  2019.01.22  MergePull Request #3&Pull Request #4. ThanksDavid!0.3.7  2019.02.25  Add --all option which requested asissue#5, thanksRafał!0.3.8  2019.04.16  MergePull Request #7. ThanksAlexander!0.3.8.5  2019.04.21  MergePull Request #9. ThanksWO15!0.3.9  2019.05.06  Revise the serial port selection logic. Support usingPL011uart on Raspberry PiModel 3 and Zero Wwhich is selected by setting dtoverlay=pi3-miniuart-btor dtoverlay=pi3-disable-bt. Thanks片岡さんfor your kindlyreportincluding this issue!0.4.1 2019.08.11 Add --serial_device option as solution ofissue#12. ThanksActpohomocandTBR-BRD!0.5.1 2020.05.16 Add--serial_console_untouchedoption to supportexecution without sudoasked asissue#17. Thanksralphbe91!0.5.2 2020.06.30 Update the link for datasheet of MH-Z19B from version 1.0 to version 1.5 based be pointed it out asissue#18. ThanksWO15!0.6.1 2020.07.07 Add--detection_range_10000option to supportSet 0~10000ppm detection rangeasked asissue#19. ThanksWO15!0.6.3 2020.08.27 Fixissue#21. Thanksidegre!3.0.0 2021.02.05PWM support.3.0.1 2021.02.17 Fix a degradation of not running with python3. Thank youMasahiko OHKUBOsan for your report.3.0.2 2021.03.25 Fix to support RPi4 correctly asissue#29. Thanksiperniaf!3.0.3 2021.11.08 Fixissue#35. Thanksfalse!3.0.4 2011.11.11 Fixissue#36. ThanksDavid Bock3.0.5 2022.01.01 Fixissue#38. Thankskzehnter!3.1.0 2022.01.22 add--co2valueonlyoption by mergingissue#39andissue#40. Thanksjonesthefox!3.1.1 2022.01.23 remove code clone.3.1.2 2022.01.31 Fixissue#41. ThanksChristopher M. Pierce!3.1.3 2022.02.25 Fixissue#43. ThanksJannis Möller!