Close Menu
Circuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

Instagram WhatsApp
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
Circuit of ThingsCircuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc
Hire us
Circuit of Things
Home»Uncategorized»ESP32 MH-Z19B: Building a DIY CO₂ Monitor for Smarter Air Quality Projects
Uncategorized

ESP32 MH-Z19B: Building a DIY CO₂ Monitor for Smarter Air Quality Projects

Sai Preetham KoyyalaBy Sai Preetham KoyyalaMay 28, 2025No Comments4 Mins Read
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email
Contents
  • Introduction
  • Understanding the MH-Z19B CO₂ Sensor
  • Why Choose the ESP32?
  • Wiring the MH-Z19B to the ESP32
  • Programming the ESP32
  • Integrating Wi-Fi and Cloud Services
  • Calibration and Maintenance
  • Conclusion

Introduction

Indoor air quality has become a significant concern, especially in the wake of increased awareness about health and environmental factors. Monitoring CO₂ levels is crucial for ensuring a healthy living and working environment. The MH-Z19B CO₂ sensor, when paired with the versatile ESP32 microcontroller, offers a cost-effective solution for real-time air quality monitoring. This guide will walk you through the process of setting up the MH-Z19B sensor with the ESP32, enabling you to create a smart, Wi-Fi-enabled CO₂ monitoring system.

Understanding the MH-Z19B CO₂ Sensor

The MH-Z19B is a compact, non-dispersive infrared (NDIR) CO₂ sensor known for its accuracy and reliability. It measures CO₂ concentrations ranging from 0 to 5000 ppm, making it suitable for various indoor applications. Key features include:

  • High Accuracy: ±50 ppm + 5% of reading.
  • Dual Output Modes: UART (serial) and PWM.
  • Operating Voltage: 4.5V to 5.5V DC.
  • Preheat Time: Approximately 3 minutes.
  • Long Lifespan: Over 5 years of continuous operation.YouTube+1Python in Plain English+1

The sensor’s UART interface operates at 3.3V logic levels, making it compatible with the ESP32 without the need for level shifters.

Why Choose the ESP32?

The ESP32 is a powerful microcontroller with integrated Wi-Fi and Bluetooth capabilities. Its dual-core processor and ample GPIO pins make it ideal for IoT applications. When combined with the MH-Z19B sensor, the ESP32 can:

  • Collect real-time CO₂ data.
  • Transmit data over Wi-Fi to cloud services or local servers.
  • Trigger alerts or control ventilation systems based on CO₂ levels.
  • Display data on web interfaces or mobile apps.

This combination allows for the creation of smart air quality monitors that can be integrated into home automation systems.

Wiring the MH-Z19B to the ESP32

Proper wiring is crucial for accurate data transmission between the MH-Z19B sensor and the ESP32. Here’s how to connect them:

MH-Z19B PinFunctionESP32 Pin
1 (Vin)Power Supply (5V)5V (Vin)
2 (GND)GroundGND
3 (TXD)UART TransmitGPIO16 (RX)
4 (RXD)UART ReceiveGPIO17 (TX)

Note: Ensure that the sensor’s TXD connects to the ESP32’s RX pin and vice versa. Also, provide a stable 5V power supply to the sensor, as the ESP32’s 3.3V output is insufficient.

Programming the ESP32

To read data from the MH-Z19B sensor, you’ll need to program the ESP32 using the Arduino IDE. Here’s a basic example:

cppCopyEdit#include <SoftwareSerial.h>

#define RX_PIN 16 // ESP32 RX
#define TX_PIN 17 // ESP32 TX

SoftwareSerial mySerial(RX_PIN, TX_PIN);

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
}

void loop() {
  byte cmd[9] = {0xFF, 0x01, 0x86, 0, 0, 0, 0, 0, 0x79};
  mySerial.write(cmd, 9);
  delay(1000);

  if (mySerial.available()) {
    byte response[9];
    mySerial.readBytes(response, 9);

    if (response[0] == 0xFF && response[1] == 0x86) {
      int co2 = (response[2] << 8) + response[3];
      Serial.print("CO2 Concentration: ");
      Serial.print(co2);
      Serial.println(" ppm");
    }
  }

  delay(2000);
}

Important Tips:

  • Wait for the sensor’s preheat time (~3 minutes) before taking readings.
  • Ensure the baud rate matches the sensor’s default (9600 bps).
  • Use a stable power supply to prevent voltage fluctuations.

Integrating Wi-Fi and Cloud Services

One of the ESP32’s strengths is its Wi-Fi capability. You can enhance your CO₂ monitor by:

  • Sending Data to Cloud Platforms: Use services like ThingSpeak or Blynk to visualize data remotely.
  • Creating a Web Server: Host a local web page on the ESP32 to display real-time CO₂ levels.
  • Implementing MQTT Protocol: Integrate with home automation systems like Home Assistant for automated responses.

These integrations allow for real-time monitoring and control, making your CO₂ monitor a smart device.

Calibration and Maintenance

For accurate readings, periodic calibration of the MH-Z19B sensor is recommended. The sensor supports:

  • Automatic Baseline Correction (ABC): Enabled by default, it calibrates the sensor to 400 ppm over a 24-hour period.
  • Manual Calibration: Expose the sensor to fresh air (approximately 400 ppm CO₂) and send the calibration command via UART.

Maintenance Tips:

  • Avoid exposing the sensor to high concentrations of volatile organic compounds (VOCs).
  • Keep the sensor away from dust and moisture.
  • Ensure proper ventilation around the sensor for accurate measurements.

Conclusion

Combining the MH-Z19B CO₂ sensor with the ESP32 microcontroller provides a powerful platform for monitoring indoor air quality. This setup is not only cost-effective but also offers flexibility for integration into various smart home systems. By following this guide, you can build a reliable CO₂ monitoring system that contributes to a healthier living environment.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleESP32 vs Raspberry Pi Pico W: Which One Should You Choose for Your Next IoT Project? (2025 Ultimate Comparison Guide)
Sai Preetham Koyyala
  • Website
  • Facebook
  • X (Twitter)
  • Instagram
  • LinkedIn

Hello, I am Sai Preetham Koyyala. I'm an electronics engineer by profession, a DIY enthusiast by passion. ESP32 and Arduino are the main topics of my work.

Related Posts

Uncategorized

ESP32 vs Raspberry Pi Pico W: Which One Should You Choose for Your Next IoT Project? (2025 Ultimate Comparison Guide)

May 9, 2025
Add A Comment
Leave A Reply Cancel Reply

skillshare
Hire me
saipreetham0
Fiverr
Seller
As a skilled and experienced freelancer, I offer a range of services to help clients achieve their goals. I have a strong background in computer science and engineering and am proficient in a variety of programming languages and frameworks. I have experience in building custom websites, mobile apps, and IoT solutions and am always looking for new challenges. I value open communication and collaboration with my clients and am dedicated to meeting deadlines and ensuring high-quality work. If you are looking for a reliable and skilled freelancer, please don't hesitate to contact me. Thank you.
Top Posts

ESP8266-01 Pinout: A Comprehensive Guide to Pin Configuration

June 25, 2023

Get Started with Arduino: A Beginner’s Guide to the World of Electronics

June 7, 2023

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023
Stay In Touch
  • Telegram

Subscribe to Updates

Get the latest tech news from FooBar about tech, design and biz.

Most Popular

ESP8266-01 Pinout: A Comprehensive Guide to Pin Configuration

June 25, 2023

Get Started with Arduino: A Beginner’s Guide to the World of Electronics

June 7, 2023

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023
Our Picks

ESP32 MH-Z19B: Building a DIY CO₂ Monitor for Smarter Air Quality Projects

May 28, 2025

ESP32 vs Raspberry Pi Pico W: Which One Should You Choose for Your Next IoT Project? (2025 Ultimate Comparison Guide)

May 9, 2025

Build an ESP32 DHT11 Webserver for Real-Time Temperature and Humidity Monitoring

December 9, 2024

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

X (Twitter) Instagram YouTube WhatsApp
  • Book an Appointment
  • My Bookings
  • Support Us
© 2025 Circuit of Things. Designed by Sai Preetham Koyyala.

Type above and press Enter to search. Press Esc to cancel.