ESP-IDF driver for the MH-Z19B NDIR CO₂ sensor connected to UART.
The sensor owns a plain UART line: it is configured for 9600 baud, 8 data bits,
no parity and one stop bit, and every exchange is a fixed nine-byte frame that
starts with 0xFF 0x01, carries the command and ends with a checksum. The driver
builds the requests, verifies the header, the echoed command and the checksum of
the answer, and converts the two data bytes into a CO₂ concentration in ppm.
- Documentation
- Repository
- Issues
- Discussions and questions
- Component page at the ESP Component Registry
- CO₂ concentration in ppm, read with the standard "read CO₂" command.
- Detection range of 2000 or 5000 ppm: selecting it and reading it back.
- Automatic baseline correction: switching it on and off, and reading its state.
- Manual zero-point calibration at 400 ppm.
- Firmware version read, which is what distinguishes the variants of the family.
- Warm-up handling: the datasheet requires three minutes of warm-up after power-on, and the driver also offers a "smart" check for the case where the sensor stayed powered while the MCU was reset.
- A minimum interval between two readings, the same rate at which the sensor blinks its LED, so that a tight loop does not read the same value over and over.
- Rejection of corrupted frames: a wrong header, a wrong echoed command or a wrong checksum is reported as an error instead of being turned into a plausible-looking number.
The driver is written for the MH-Z19B and uses the command set that the datasheet documents for it. It does not identify the variant, so a sensor of the family that answers the same frames will work, and a sensor that does not will fail the header or the checksum check.
The sensor needs a 5 V supply and talks 3.3 V logic, so its TX line can be
connected to the ESP32 directly. TX of the sensor goes to the RX of the
ESP32 and RX of the sensor to the TX of the ESP32.
| MH-Z19B pin | ESP32 | Note |
|---|---|---|
VIN |
5 V | the sensor does not run from 3.3 V |
GND |
GND | common ground is required |
TX |
CONFIG_EXAMPLE_UART_RX |
sensor output, ESP32 input |
RX |
CONFIG_EXAMPLE_UART_TX |
sensor input, ESP32 output |
The example defaults are 12 and 13 on esp32, esp32s2 and esp32s3, and 4
and 5 on esp32c2, esp32c3, esp32c5, esp32c6, esp32c61, esp32h2 and
esp32p4. Both numbers are configurable under "Example configuration" in
idf.py menuconfig.
#include <mhz19b.h>
mhz19b_dev_t dev;
int16_t co2;
ESP_ERROR_CHECK(mhz19b_init(&dev, UART_NUM_1, GPIO_NUM_12, GPIO_NUM_13));
while (!mhz19b_detect(&dev))
vTaskDelay(pdMS_TO_TICKS(1000));
while (mhz19b_is_warming_up(&dev, true))
vTaskDelay(pdMS_TO_TICKS(1000));
while (1) {
if (mhz19b_is_ready(&dev) && mhz19b_read_co2(&dev, &co2) == ESP_OK)
ESP_LOGI(TAG, "CO2: %d ppm", co2);
vTaskDelay(pdMS_TO_TICKS(1000));
}mhz19b_init() installs the UART driver on the given port and attaches the
given pins to it; every pin is configured on every target. mhz19b_free()
releases the receive buffer of the descriptor but leaves the UART driver
installed, because the port may be shared with other devices; call
uart_driver_delete() yourself when the port is no longer needed.
See examples/default.
- A reading taken during the first three minutes after power-on is not valid.
mhz19b_is_warming_up(dev, false)waits the full three minutes;mhz19b_is_warming_up(dev, true)reads the sensor and treats a change in the value as the end of the warm-up, which is what happens when the sensor was already powered while the MCU restarted. - The sensor is read at most once per five seconds. The interval is not in the
datasheet; it matches the rate at which the built-in LED blinks, and the
driver exposes it through
mhz19b_is_ready(). - Automatic baseline correction assumes that the room drops to about 400 ppm of fresh air at least once a day, and the sensor slowly re-zeroes itself to that value. In a closed space that never sees fresh air (a greenhouse, an incubator, a fermentation chamber) the correction drifts the readings, so it has to be switched off and the zero point calibrated by hand.
- The manual zero-point calibration sets the current reading to 400 ppm. The sensor has to stay in fresh air for at least 20 minutes before the call, and the calibration is meant to be done with automatic baseline correction off.
- Span calibration (command
0x88) is not implemented. - Reading the range, the state of the automatic baseline correction and the firmware version uses commands that the datasheet does not document. They work on the parts they were tested with and may answer nothing on others; the driver reports a timeout or a bad frame in that case.
- The driver has no mutex: one descriptor per UART port, and calls from one task.
- The nine-byte answer has to arrive within 120 ms, which the datasheet's
response time fits at 9600 baud; a slower or a busier sensor shows up as
ESP_ERR_TIMEOUT.
idf.py add-dependency esp-idf-lib/mhz19bFor questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.
BSD-3-Clause, see LICENSE.