Close Menu
Circuit of Things
  • Home
  • About Us
  • Contact Us
  • 3D Print
  • Shop Now !
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • KSP Tools

Get Free Tutorials & Discounts!

Subscribe for the latest IoT tutorials and exclusive KSP Electronics discount codes.

Instagram WhatsApp
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
  • Contact Us
Circuit of Things Circuit of Things
  • Home
  • About Us
  • Contact Us
  • 3D Print
  • Shop Now !
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • KSP Tools
Hire us
Circuit of Things
Home»ESP32 Projects»ESP32 MH-Z19B: Building a DIY CO₂ Monitor for Smarter Air Quality Projects
ESP32 Projects

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

Sai Preetham KoyyalaBy Sai Preetham KoyyalaMay 28, 2025Updated:May 7, 2026No 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.

Related Tutorials

  • ESP32 MQTT Tutorial: Publish and Subscribe with HiveMQ
  • How to Build a DIY Robotic Hand using Flex Sensors and Arduino
  • Arduino I2C LCD Display Tutorial: Show Sensor Data on Screen
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)
Next Article Powerful Arduino Bluetooth Home Automation (HC-05 & KSP App 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

ESP32 Projects

Step-by-Step Guide: Interface DHT11 and DHT22 Sensors with ESP32

May 6, 2026
ESP32 Projects

An Introduction to the ESP32 Development Board

May 6, 2026
ESP32 Projects

The Complete ESP32 Pinout Guide: Which Pins to Use?

May 6, 2026
Add A Comment

Comments are closed.

⚡

Circuit of Things

Free IoT tutorials & electronics projects — powered by KSP Electronics.

📸 Instagram 💬 Community
✓ Official Partner Store

Everything for Your
Next IoT Build

Boards, sensors, modules & kits — fast delivery across India.

ESP32 Arduino Sensors Raspberry Pi 3D Print
🛒 Shop KSP Electronics →
Top Posts

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

July 14, 2023

Step-by-Step Guide: Interface DHT11 and DHT22 Sensors with ESP32

May 6, 2026

Esp8266 / NodeMCU Pinout: A Comprehensive Guide for Beginners

June 25, 2023
Available for Projects

Hire Us for IoT Development

End-to-end solutions for your needs:

  • → ESP32 & Embedded Systems
  • → LoRaWAN & Cloud Dashboards
  • → Custom Hardware Design
  • → IoT Product Prototyping
View Services →

Get Free Tutorials & Discounts!

Subscribe to get the latest IoT tutorials and exclusive hardware discount codes for KSP Electronics delivered directly to your inbox.

Follow Us
  • Instagram
  • Telegram
About Circuit of Things

We are a community-driven engineering platform dedicated to IoT, Robotics, and DIY electronics tutorials.

Proudly partnered with KSP Electronics.

Quick Links
  • Home
  • ESP32 Projects
  • Arduino Projects
  • IoT Tutorials
  • Sensors & Modules
  • Shop on KSP Electronics
  • Electro Calc
Legal & Support
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
  • Contact Us
📩

Get Free Tutorials & Discounts!

Subscribe for IoT tutorials and exclusive KSP discount codes.

Subscription Form


By subscribing, you agree to our Privacy Policy. No spam, ever.

X (Twitter) Instagram YouTube WhatsApp
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
  • Contact Us
© 2026 Circuit of Things. Designed by Sai Preetham Koyyala.

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