MH-Z19 CO2 sensor reader, logger and visualiser

MH-Z19 CO2 sensor reader, logger and visualiser

https://user-images.githubusercontent.com/862951/52826593-a98a5400-3115-11e9-868a-72a763b6d587.jpg

  • Reads data from UART(serial)-connected MH-Z19 (or MH-Z14) sensor using python 3.
  • If you dare to install nodejs you can visualise the logged data (using html and plotly.js library).

Repository: sammcj/CO2-Logger

Note this post is from 2016, in 2021 I replaced my custom Co2 loggers with an Aranet4. While very expensive, is and excellent off-the-shelf solution, with many features.

Sensor can be queried using 3.3v UART at 9600 bps. Sensor main feed voltage is 5v.

Can be connected to computer using almost any USB-UART converter if voltage matches.

  • Python3
  • pip3 install serial pyserial
  • A USB -> Serial UART connector (Could also be used with Raspberry Pi, ESP8266, Arduino etc…)
  • MH-Z19 Co2 Sensor (Around $25AUD on eBay)
$ python3 CO2Reader.py --tty=/dev/tty.SLAB_USBtoUART --timeout=2
Connected to /dev/tty.SLAB_USBtoUART
2019-02-25 07:44:48 558 58
2019-02-25 07:44:49 558 58
2019-02-25 07:44:51 558 58
...

3 fields separated by tab: timestamp, CO2 concentration (ppm), internal sensor temperature (Celsius)

Use stream redirection to save data series to file:

touch co2.log
python3 CO2Reader.py --tty=/dev/tty.SLAB_USBtoUART --timeout=2 >>co2.log
  • --tty (optional) tty to connect to (default is /dev/tty.SLAB_USBtoUART)
  • --single (optional) perform a single measurement and exit
  • --quit (optional) connect to the sensor and quit immediately
  • --timeout (optional) time between requests (defaults to 10 seconds)
  • --command (optional) raw command to pass to the sensor - use carefully (default is to read Co2 levels)

Raw commands can be passed to the Co2 sensor, these to be used with caution.

Example:

python3 CO2Reader.py --tty=/dev/tty.SLAB_USBtoUART --timeout=1 --command='0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79'

0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79

0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6

0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86

(Note: Untested)

  • 1000 = 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x03, 0xE8, 0x7B
  • 2000 = 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x8F
  • 3000 = 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x0B, 0xB8, 0xA3
  • 5000 = 0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB
  • Run this for 10mins + in 400ppm Environment

WARNING: DO NOT RUN THIS UNLESS YOU KNOW WHAT YOU ARE DOING!

0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78

  1. Install npm dependencies npm install
  2. Start server python3 -m http.server 8088 --bind 127.0.0.1
  3. Open browser at http://127.0.0.1:8088/plot.html
  4. Select your log file in input field
AttributeValue
Target gasCarbon Dioxide CO2
Operating Voltage3.6 to 5.5 Vdc
Operating current< 18mA average
Interface levels3.3 Vdc
Output signal formatUART or PWM
Preheat time3 min
Response time<60 s
Accuracy± (50 ppm+5% reading value)
Measuring range0 to 5000 ppm
Operating temperature range0 to + 50°C
Dimensions33mmx20mmx9mm(LxWxH)
FunctionUART / SignalMH-Z19 pin
Vcc +5V+5V6 Vin
GNDGND7 GND
UARTTXD02 RXD
UARTRXD03 TXD

https://user-images.githubusercontent.com/862951/52826907-c7a48400-3116-11e9-9c2e-c5fde2cf8f1d.jpg

https://user-images.githubusercontent.com/862951/53372770-d48f6600-39a7-11e9-8c05-9aca871de7d0.jpg https://user-images.githubusercontent.com/862951/53372771-d527fc80-39a7-11e9-80e6-17ade8eb4b19.jpg

https://user-images.githubusercontent.com/862951/52826018-38e23800-3113-11e9-92f3-18c99c902ae5.jpg

Don’t use this function unless you know what you are doing or you may incorrectly calibrate the sensor!

(Australia has an average of around 400-410PPM and Sensor zero point is 400PPM)

https://user-images.githubusercontent.com/862951/52827251-21597e00-3118-11e9-9ebc-ddbbc9fb02a8.jpg

MIT licence

Related Content