MH-Z19 Carbon Dioxide Sensor

From Leo's Notes
Last edited on 15 February 2022, at 05:35.

The Winsen MH-Z19 carbon dioxide sensor can be purchased for around $20 USD.

Usage

MH-Z19 CO2 Sensor glowing every 5 seconds
MH-Z19 CO2 Sensor. It glows approximately every 5 seconds (pictured on left)

These CO2 modules cost about $17 USD. They come in 3 models: 0-2000PPM, 0-5000PPM, and 0-10000PPM (though not according to the datasheet?). These modules can be sensed with either the PWM output or via serial/UART.

The modules automatically measure the CO2 levels every 5 seconds which can be seen by the slight orange glow that's emitted from inside the module. It measures CO2 levels regardless of whether any requests are made via UART, which makes sense since it needs up-to-date data for the PWM output to be accurate. However, this also means you can't lengthen its life by only polling periodically.

Wiring

Provide the module with 5V power. The serial output uses 3.3v logic level outputs and you can safely connect it directly to your 3.3v microprocessor such as the ESP8266 or ESP32.

Connect TX from the MH-Z19 to RX of your UART and vice versa. If you are using Tasmota, configure the TX pin of the UART to 'MHZ19 TX' and the RX pin to 'MHZ19 RX'.

Measurement

MH-Z19 PWM
MH-Z19 PWM

The PWM output has a period of 1 second. The amount of time that the signal is high corresponds linearly with the sensor's maximum ppm.

Since timing the PWM output takes 1 second per reading, it may be desirable to just use the UART interface to poll the sensor.

For the ESP8266, you can use this project to interface with both the UART and the PWM output:

This project however hardcodes a preheating time interval of 3 minutes since the start (or reset) of the ESP. There is also a 'response time' interval of 2 minutes for the MH-Z19B which isn't specified anywhere in the datasheet and locks out the ability to do frequent polling for a CO2 measurement. This is mostly pointless since the PWM measurement is always constant, meaning the module measures the CO2 measurement constantly. Fix this by editing the MHZ.cpp file and set the preheating time to 0 and the response time to 1000ms.

See Also