MH-Z19B Sensor: All about the NDIR CO2 sensor, its operation, calibration, and applications

  • The MH-Z19B is an NDIR sensor specialized in measuring CO2 accurately, reliably, and with great ease of integration into electronic and home automation projects.
  • Its multiple outputs (UART, PWM and analog) and self-calibration make it ideal for environmental monitoring in homes, schools and HVAC systems.
  • Regular calibration, proper installation, and the selection of original versions are essential for accurate and reliable results.

MH-Z19B Sensor

The world of gas sensors has expanded enormously in recent years, especially due to growing concerns about air quality and health in indoor environments. One of the sensors that has generated the most interest in the maker, educational, and professional communities is the MH-Z19B. Its reputation is due to its ability to accurately measure carbon dioxide (CO2), its easy integration with platforms such as Arduino and ESP8266/ESP32, and a more affordable price compared to professional alternatives. If you're wondering What is the MH-Z19B, how does it work, what makes it so special, and how can you use it in your projects?, You have come to the right place.

In this article you will go into all the details of the MH-Z19B sensor, from its operating principles and technical specifications to best practices for use, calibration, and real-world applications. All presented in a comprehensive, accessible, and useful way for those who want to go beyond the data sheets and experiment with CO2 measurement in different scenarios.

What is the MH-Z19B sensor?

El MH-Z19B It is a gas sensor specialized in the CO2 measurement environmental. Its compact design, low cost, and ease of use have made it a popular choice for technology enthusiasts, professionals, and educators alike. Manufactured by the Chinese company Winsen, this sensor uses the principle of non-dispersive infrared (NDIR) to quantify the concentration of carbon dioxide in the air, avoiding interference with other gases and ensuring greater precision than electrochemical or metal oxide sensors.

The beginning NDIR It is based on the fact that CO2 absorbs infrared light at a certain wavelength. Inside the sensor is a small chamber where this beam of light is passed through an air sample. The detector measures the attenuation produced by the presence of CO2, allowing the amount present in the air to be calculated. parts per million (ppm)Thanks to this technology, the MH-Z19B offers low cross-sensitivity (i.e., it is barely affected by other gases such as oxygen), a long service life, and good reliability.

Technical specifications and variants of the MH-Z19B

The MH-Z19B stands out, above all, for its versatility and ease of integrationIt's not just designed for advanced users: it's often used in educational projects, home automation installations, home weather stations, or even environmental monitoring systems in schools and offices. Let's take a closer look at its main specifications:

Average current <60 mA

Feature Price
Sensor Type NDIR (Non-dispersive infrared)
Gas detected Carbon dioxide (CO2)
Measuring ranges 0–2000 ppm, 0–5000 ppm, 0–10000 ppm (depending on version)
Precision ±50 ppm + 3% of reading
Operating voltage 4.5-5.5V DC
Peak current 150 mA
Signal output UART (Serial), PWM, Analog
Interface level 3.3 V (5 V compatible)
Warm-up time 3 minutes
Response time <120 s (T90)
Operating temperature 0-50 ° C
RH 0–90% RH (non-condensing)
Dimensions 33 x 20 x 9 mm
Weight 5 grams
Lifespan More than 5 years

The MH-Z19B shares a family with other models such as the MH-Z19C and MH-Z19D. Each of them has slight differences, especially in terms of accuracy and power voltage tolerance. In fact, the MH-Z19C requires a very narrow power supply range (4.9 to 5.1 V), which can complicate its use without a well-stabilized power supply. On the other hand, the MH-Z19D is more recent and is still awaiting in-depth analysis by the community.

Additional sensor features

  • Low cross-sensitivity: It is not affected by the presence of oxygen or other common gases, which increases the reliability of the measurements.
  • Gold-plated gas chamber: This gives it corrosion resistance and extends its useful life.
  • Internal temperature compensation: Although the sensor includes an internal thermistor to adjust its measurements, the reported temperature is not intended for external use.
  • Multiple output: You can choose between UART, PWM and analog output to extract the information depending on your needs.
  • Integrated water filter: prevents damage or contamination from moisture.
  • ABC Autocalibration: Automatically adjusts the baseline CO2 level if the sensor is exposed to clean air for at least 20 minutes over a 24-hour period, ensuring accurate readings over time.

Different versions of the MH-Z19 and their particularities

The MH-Z19 line has several iterations that can be found on the market. The model MH-Z19B It is the most widely used and is generally recognized as the best-balanced option between price, precision, and ease of integration. However, there are other variants with important differences to consider:

  • MH-Z19 (original): 2000 and 5000 ppm versions, accuracy of ±50 ppm + 5% of reading. Somewhat dated but still functional.
  • MH-Z19B: 2000, 5000, and 10000 ppm versions, with improved accuracy of ±50 ppm + 3%. This is the improved version and is best suited for general use.
  • MH-Z19C: Same range of 2000, 5000, or 10000 ppm, accuracy of ±50 ppm + 5%, and requiring a very precise power supply (4.9-5.1V). This limitation makes it less suitable for most users.
  • MH-Z19D: the most recent development, with a behavior very similar to C, although it is still being studied by enthusiasts and lacks extensive documentation in English.

A crucial issue is the proliferation of fake sensors on the market, especially on import platforms. It is often noted that sensors MH-Z19B with black PCB, instead of the traditional green or blue, can be unofficial versions or clones, with uncertain performance. To avoid reliability issues, it's best to purchase the sensor from reputable distributors.

How the MH-Z19B works: NDIR principle

The heart of the sensor is its NDIR technology, widely used in professional sensors. The operation of the MH-Z19B can be summarized in several key steps:

  1. An air sample is introduced into the sensor's internal chamber, which contains an infrared light source and a detector.
  2. Infrared light will pass through the camera, but part of that light will be absorbed by CO2 molecules depending on their concentration.
  3. The detector measures how much light reaches it after passing through the sample air, and calculates the amount of CO2 present.
  4. The sensor provides the CO2 value in parts per million (ppm).

Thanks to this indirect method, the measurement is very insensitive to the presence of humidity, temperature, or gases other than CO2, as long as the operating limits indicated in the datasheet are respected.

Data outputs available on the MH-Z19B

One of the many advantages of the MH-Z19B is its flexibility when obtaining dataYou can choose between three output principles, depending on your preference for ease, precision, or integration into your system:

  • UART (TTL Serial at 3.3V/5V): This is the simplest option since the sensor sends the value directly in ppm. It only requires a baud rate of 9600, and you can easily connect it to a microcontroller. To expand your knowledge, you can also consult Our guide to environmental quality sensors and learn how to integrate different sensors into your projects.
  • PWM: The sensor generates a pulse-modulated signal, the duty cycle of which reflects the CO2 concentration. It's ideal if you need a straightforward direct output and want to take advantage of pulse readings on your microcontroller.
  • Analog Output: Less common, but allows you to read a signal proportional to the CO2 concentration. It may require additional calibration and is not the preferred option for most users.

Each of these methods has its advantages. Arduino or ESP, the UART output is by far the most practical and accurate, since it does not require additional calculations and the interpretation of the data is straightforward.

bm680
Related article:
Complete Guide to the BME680 Sensor: Environmental Quality with Arduino

Calibrating the MH-Z19B: Ensuring Accuracy

The accuracy of any sensor may vary over time due to environmental factors and the aging of the component itself. MH-Z19B incorporates two calibration methods to ensure that readings remain reliable:

Manual Calibration (Zero Point Calibration)

If you want to make sure your sensor is properly adjusted, you can perform a manual calibration. This involves exposing the sensor to well-ventilated air, ideally outdoors, where the CO2 concentration is around 400 ppmAfter at least 20 minutes of stable operation, a simple hardware command (connecting the HD and GND pins for 7 seconds) or software command marks that point as the zero reference for the sensor. It's important to keep in mind that this calibration is essential after transport or if the ambient conditions of the environment where the sensor is installed have changed.

Automatic ABC calibration (Automatic Baseline Correction)

The MH-Z19B incorporates a self-calibration algorithm that detects the lowest CO24 value every 2 hours and uses it to adjust its reference value. This is very convenient in living environments where fresh air is periodically supplied. It is essential that the sensor has access to clean air for at least 20 minutes every 24 hours.Otherwise, the correction could be inaccurate. If you install the sensor in closed, poorly ventilated spaces (such as greenhouses), it's best to disable this mode and perform periodic manual calibrations.

gas detector
Related article:
Module to measure air quality with Arduino (gas detector)

Finally, there is a method to calibrate the high point or Span Point, but it is not usually recommended for home users, as it requires a stable atmosphere with a known and controlled CO2 concentration (e.g., 2000 ppm) and specific laboratory equipment.

Applications and usage examples of the MH-Z19B

The versatility of the MH-Z19B is such that its application extends far beyond typical home air quality metersBelow are some of the most popular environments and use cases:

  • HVAC and air conditioning systems: Monitors CO2 concentration to optimize air renewal in homes, offices and classrooms.
  • Home automation and smart homes: Integrate the sensor into platforms such as Home Assistant or similar to control fans and air purifiers based on the detected levels.
  • Educational monitoring: Numerous educational centers have implemented MH-Z19B sensors to ensure adequate ventilation in classrooms, especially in the wake of the COVID-19 pandemic.
  • Greenhouses and precision agriculture: Controlling CO2 levels in greenhouses allows for optimizing plant growth.
  • Air purifiers and quality systems: Some commercial purifiers integrate NDIR sensors to operate according to the actual needs of the environment.
  • DIY Projects: It is common to find tutorials for installing CO2 traffic lights that alert using LEDs or acoustic warnings when the concentration exceeds certain thresholds.

The MH-Z19B sensor can also be found integrated into environmental monitoring systems, portable meters, home weather stations, and low-budget laboratory equipment.

How to connect the MH-Z19B to microcontrollers

The MH-Z19B's electrical connection is notable for its simplicity. It has pins clearly marked on the sensor's silkscreen, and the basic configuration requires only four connections (power and data). Here's an example of how the pins would be assigned for an Arduino MEGA:

Arduino MEGA MH-Z19B
19 (RX1) TX
18 (TX1) RX
GND GND
5V Wine

Logic level converters may be required on other board or microcontroller models, although the MH-Z19B supports both 3.3V and 5V inputs. As a good practice, always check the pinout of your specific sensor, as slight variations may occur depending on the batch or manufacturer.

Reading and processing data from the MH-Z19B

Getting useful data from the MH-Z19B is as simple as connecting the UART output to the development board and reading the transmitted data.Communication is at 9600 baud, with 8 data bits, 1 stop bit, and no parity. You can use a dedicated library to simplify working with Arduino, for example: MHZ19.h, or send the commands manually according to the manufacturer's data sheet.

Basic example of use in an Arduino program:

#include MHZ19mhz(&Serial19); // Using Serial1 for communication with the sensor void setup() { Serial.begin(1); Serial115200.begin(1); } void loop() { MHZ9600_RESULT response = mhz.retrieveData(); if (response == MHZ19_RESULT_OK) { Serial.print("CO19: "); Serial.println(mhz.getCO2()); } delay(2); }

The result will be the CO2 concentration in ppm. It is also possible to access the internal sensor temperature, although, as mentioned above, this data is intended for internal compensation and should not be used as a reference for ambient temperature.

PWM mode and manual CO2 calculation

If you prefer to use the PWM output, interpreting the signal requires a small calculation to convert the pulse duration into the corresponding CO2 concentration. The typical equation provided for the 2000 ppm range is:

CO2(ppm) = 2000 · (T_H – 2ms) / (T_H + T_L – 4ms)

where T_H is the high cycle time in milliseconds, and T_L is the low cycle time. For the 5000 ppm range, simply replace the 2000 with 5000.

Commands and advanced operation

For those who want to squeeze all the functions of the sensor, the MH-Z19B responds to different control commands via UART. Some of the most relevant ones:

  • CO2 concentration reading: command 0x86
  • Zero point calibration: command 0x87
  • SPAN Point Calibration: command 0x88
  • Activate/deactivate autocalibration: command 0x79
  • Measurement range query: command 0x99

Sensor responses always include a CRC code for verification, which increases security against transmission errors.

Recommendations for use, maintenance and precautions

The operation of the MH-Z19B is quite robust, but there are certain Key recommendations to extend its useful life and ensure the quality of measurements:

  • Avoid touching the pins directly to minimize risks of static electricity.
  • Do not apply pressure to the housing while soldering.: The body is not just a wrapper, but an active part of the sensor.
  • Avoid extreme temperatures and direct sun exposure in permanent installations.
  • Keep the installation area ventilated, especially the diffusion window of the sensor.
  • Avoid dust accumulation for extended periods of use.
  • Check the supply voltage: The source must be well stabilized and supply at least 150 mA to avoid erroneous readings or malfunctions.
  • Do not use wave or dip soldering: Limit soldering time to 3 seconds per pin and allow cooling between connections.
  • Perform periodic calibrations, especially if the sensor is moved, subjected to sudden temperature changes, or installed in poorly ventilated areas.

All of these measures will extend the life of the sensor and ensure more consistent and reliable measurements.

Similar models and alternative sensors

While the MH-Z19B is a favorite for its price-performance ratio, there are other models and alternatives with similar or complementary features. One of the sensors that could be considered in this context is the MH-Z1311A, also from Winsen, which boasts very low energy consumption and is suitable for battery-powered projects. On the other hand, the MG811 and the family MQ (such as the MQ-135) have historically been popular, although their readings can be more affected by humidity, temperature, or other gases, and they require more calibration and adjustment to obtain reliable results. In applications where true CO2 accuracy is critical, the MH-Z19B often outperforms these models in terms of ease of use and accuracy.

Notes on fake sensors and measurement quality

One of the current challenges is the proliferation of MH-Z19B sensors of dubious originThe most obvious clue is usually the color of the plate (black instead of green or blue) and the absence of original documentation or labels. These sensors may appear functional, but their measurements may be unreliable and their durability substantially reduced. It is advisable to purchase the sensor from reputable distributors. and avoid excessive bargains on unverified platforms.

Myths and little-known aspects of the MH-Z19B

Not everything about this type of sensor is gold, and it's worth clarifying some points that often generate doubts:

  • The integrated temperature sensor is not reliable for ambient measurement.: Its function is exclusively internal, to adjust the CO2 reading. Do not use it as a thermometer.
  • The 'U' value reported by some commands is an internal parameter: It is not documented and does not provide useful information to the user.
  • Not all range variants are physically different: It is possible, via software commands, to change the maximum detection range of the sensor, although optimal accuracy is guaranteed up to 2000 ppm.
  • The quality of the power supply is crucial: On sensors like the MH-Z19C, the slightest deviation outside the 4.9–5.1V range can invalidate readings, so a good precision regulator or source is critical.

Sources of information and additional resources

The universe of documentation on the MH-Z19B is vast. In addition to the official datasheets which you can consult on the Winsen manufacturer's website, there are numerous blogs, forums, and GitHub repositories with examples and libraries for different microcontrollers. Among the most comprehensive sources are:

  • Manufacturer datasheets: for instructions, hexadecimal commands, and more precise technical details.
  • Technical blogs and forums: espEasy, Prometec, Emariete, among others, offer tutorials, comparisons, and resolution of doubts in Spanish.
  • GitHub: to find updated source code and driver libraries for the MH-Z19B.

Additionally, there are active user groups in maker communities, where it's possible to share experiences, tips, and common problems.