Arduino Projects

MH-Z19B NDIR CO2 Sensor with Arduino, MHZ19B

MH-Z19b NDIR  Sensor:

MH-Z19b NDIR CO2 Sensor with Arduino– In this article, you will learn about the MH-Z19B NDIR sensor and how to use it with Arduino and display the corresponding readings on the OLED display module. The term “NDIR” stands for Non-Dispersive Infrared which uses specific light wavelengths to calculate the amount within the air. The Grove –   Sensor Module is an infrared sensor with high sensitivity and high resolution. Infrared sensor MH-Z19B is a universally applicable, small sensor that enables the use of non-dispersive infrared (NDIR) for detection of the carbon dioxide in the air with good selectivity. This sensor is easy to use and has a built-in temperature sensor that can do temperature compensation. This sensor has a UART serial interface and Pulse width modulation Outputs. It can be widely used in HVAC, refrigeration indoor air quality monitoring, and in industrial processes to monitor the level.

MH-Z19B NDIR CO2 Sensor


We know that the atmosphere consists of different gasses. As we know that during breathing we take in oxygen and take out carbon dioxide. So we consider that the concentration of the oxygen and carbon dioxide are equal but in reality, the concentration of the carbon dioxide will be maximum as the industries, burning fossil fuels and cars, etc also releases carbon dioxide due to which the level of the carbon dioxide is increasing day by day due to which greenhouse effect is also increasing.

Let’s take an example of a room in which some persons are present. When we close the door of the room the level in the room will start increasing if there is no exhaust system in the room; up to 2000 PPM level we will face difficulties in breathing. So we will open the door or window to decrease the level of carbon dioxide ( ). In schools, industries or in our houses we are using exhaust fans to make our room cool by decreasing the level of carbon dioxide which consume a lot of energy because it is running all the time, by using this sensor when the carbon dioxide level will increase we will start the exhaust fan or we can set it in an automatic state in which we will define a level of carbon dioxide when it will cross the limit the exhaust system will automatically turn ON. By using this sensor we will be able to know the level of and make the atmosphere neat and clean. This sensor uses an infrared light source and inlet and outlet of airflow and can measure the level of by reading the amount of light that we will receive at the receiver.



MH-Z19B Pin description:

Vin: Power Positive (Vin)

GND: Negative Power Supply (GND)

Vo: Analog output

PWM: PWM

HD: HD (zero for school, low for more than 7 seconds)

Rx: UART(RXD) TTL Level Data Input

Tx: UART (TXD) TTL level data output

Pin 1: Analog Output Vo

Pin 2: None

Pin 3: Negative Power Supply (GND)

Pin 4: Positive Power Supply (Vin)

Pin 5: UART(RXD) TTL Level Data Input

Pin 6: UART (TXD) TTL Level Data Output

Pin 7: None

MH-Z19B NDIR CO2 Sensor


Features: 

  1. High sensitivity, High resolution, Low power consumption
  2. Output method: UART, PWM wave
  3. Quick response, Good stability
  4. Temperature compensation,
  5. Excellent linear output
  6. Long lifespan
  7. Anti-water vapor interference

Specification:

  1. The measurement range is 2000 parts per million (PPM)
  2. Resolution of 1 PPM 0-2000 parts per million (PPM)
  3. Accuracy of 200 PPM
  4. A warm – Operating time 3 minutes
  5. Response time <90s
  6. Operating temperature: 0. 50 ° C
  7. Operating humidity 0% ~ 90% RH
  8. Storage temperature: -20 to 60 ° C
  9. Operating voltage: 4.5 V to 6 V DC


MH-Z19B Application:

MH-Z19B, NDIR CO2 gas sensor module, is mostly used in industrial equipment.

Measuring temperature, humidity, and Carbon dioxide concentration:

Now in order to measure the temperature, humidity, and carbon dioxide concentration we will connect the DHT temperature sensor and MHZ-19B sensor with Arduino Uno and display the readings on the SSD1306 i2c supported OLED display module.

Amazon Links:

MH-Z19B CO2 Sensor

Arduino

SSD1306 Oled display Module

DHT11 Temperature and humidity sensor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


MH-Z19B interfacing with Arduino:

MH-Z19B NDIR CO2 Sensor

Now in order to connect the OLED with Arduino Uno:

  • Connect the SDA pin of the OLED with A4 pin of the Arduino
  • Connect the SCL pin of the OLED with A5 pin of the Arduino
  • Connect the VCC pin of the OLED with the 5V of the Arduino
  • Connect the ground pin of the OLED with the ground of the Arduino

After that we will connect the MH-Z19B sensor with the Arduino:

As we have defined D2 as TX pin and D3 as RX pin in the Arduino code. So, therefore, we will connect the TX pin of the MH-Z19B with RX pin D3 of the Arduino. Connect the RX pin of the MH-Z19B with the TX pin D2 of the Arduino. Connect the VCC of the MHZ-19B with 5V of the Arduino. Connect the ground of the MH-Z19B with the ground of the Arduino.  

Now we will connect DHT11 temperature sensor with the Arduino:

  • Connect the VCC of the DHT11 with 5V of the Arduino
  • Connect the Ground of the DHT11 with the ground of the Arduino
  • Connect the signal pin of the DHT11 sensor with the D4 of the Arduino



Required libraries:

Before uploading the code, we will install the MH-Z19 library for that click on library manager and then search the MHZ19 sensor, and install it.

MH-Z19B NDIR CO2 Sensor

Then we will install the temperature and humidity sensor by writing DFRobot_Dht in the library manager.

MH-Z19B NDIR CO2 Sensor


MH-Z19B Arduino Programming:

// CO2
#include <Arduino.h>
#include <MHZ19.h>
#include <SoftwareSerial.h>
// Temp
#include <DFRobot_DHT20.h>
// Display
 #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
// CO2
#define RX_PIN 3                                          // Rx pin of the MHZ19 Tx pin is attached to D2
#define TX_PIN 2                                          // Tx pin of the MHZ19 Rx pin is attached to D3
#define BAUDRATE 9600                                     // Device to MH-Z19 Serial baudrate (should not be changed)
MHZ19 myMHZ19;                                             // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN);                   // (Uno example) create device to MH-Z19 serial
// Temperature
DFRobot_DHT20 dht20;
// Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  Serial.begin(9600);
  // Start Co2
  mySerial.begin(BAUDRATE);                               // (Uno example) device to MH-Z19 serial start
  myMHZ19.begin(mySerial);                                // *Serial(Stream) refence must be passed to library begin().
  myMHZ19.autoCalibration();                              // Turn auto calibration ON (OFF autoCalibration(false))

  // Start Temp
  while (dht20.begin()) {
    Serial.println("Initialize sensor failed");
    delay(1000);
  }

  // Start Display
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop()
{
  //Get CO2
  int CO2;
  CO2 = myMHZ19.getCO2();                             // Request CO2 (as ppm)
 display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print(dht20.getTemperature());
  display.print('\n');
  display.print(dht20.getHumidity());
  display.print("%");
  display.print('\n');
  display.print(CO2);
  display.print(" ppm");
  display.display();
  delay(2000);
}

After uploading the code, the carbon dioxide concentration and temperature will be displayed on the OLED.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

  1. Why you don’t use any logic-level converters whereas the TTL level for MH-Z19B is 3.3v as mentioned in its datasheet?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button