Component guide · Intermediate · 3 min read
MH-Z19 CO2 Sensor

Pinout, Wiring and Code
By Geekobot Learning Team · Updated 29 Oct 2026
On this page
The MH-Z19 from Winsen is an NDIR (infrared) CO2 sensor that measures real carbon dioxide from 400 to 5,000 ppm. It runs from 5 V and sends readings over 3.3 V UART at 9600 baud, or as a PWM signal. Use it for classroom and office CO2 monitors that show when a room needs fresh air.
Key specs at a glance
- Measures
- CO2, 0 to 5,000 ppm (400 to 5,000 ppm useful)
- Accuracy
- ±(50 ppm + 5 % of reading)
- Method
- NDIR (non-dispersive infrared)
- Supply
- 4.5 to 5.5 V, about 20 mA average, 150 mA peak
- Interface
- UART 9600 baud (3.3 V logic), PWM output
- Warm-up
- 3 minutes
What the MH-Z19 is
NDIR sensors shine infrared light through a small chamber. CO2 absorbs a specific infrared wavelength, so the less light reaches the detector, the more CO2 there is. Unlike VOC sensors, this measures CO2 itself, which is why it is the standard for ventilation monitoring.
Outdoor air is about 420 ppm. Well-ventilated rooms stay under about 800 ppm; above 1,000 to 1,500 ppm people start to feel drowsy and it is time to open a window.
MH-Z19 pinout
The pins on the MH-Z19 and what each one does.
| Pin | Function |
|---|---|
| Vin | 4.5 to 5.5 V supply |
| GND | Ground |
| TX | UART transmit (3.3 V) |
| RX | UART receive (3.3 V) |
| PWM | PWM output: high time encodes the CO2 level |
| HD | Zero calibration: pull low for 7 s in 400 ppm air (fresh outdoor air) |
- Vin
- 4.5 to 5.5 V supply
- GND
- Ground
- TX
- UART transmit (3.3 V)
- RX
- UART receive (3.3 V)
- PWM
- PWM output: high time encodes the CO2 level
- HD
- Zero calibration: pull low for 7 s in 400 ppm air (fresh outdoor air)
Wiring the MH-Z19
The sensor's RX is 3.3 V; a 1 kΩ / 2 kΩ divider from the Uno's TX keeps it safe. The ESP32 connects directly.
| MH-Z19 pin | Arduino Uno | ESP32 |
|---|---|---|
| Vin | 5V | 5V (VIN) |
| GND | GND | GND |
| TX | D2 (SoftwareSerial RX) | GPIO 16 (RX2) |
| RX | D3 (SoftwareSerial TX) via 1k/2k divider | GPIO 17 (TX2) |
- Vin
- Arduino Uno:
- 5V
- ESP32:
- 5V (VIN)
- GND
- Arduino Uno:
- GND
- ESP32:
- GND
- TX
- Arduino Uno:
- D2 (SoftwareSerial RX)
- ESP32:
- GPIO 16 (RX2)
- RX
- Arduino Uno:
- D3 (SoftwareSerial TX) via 1k/2k divider
- ESP32:
- GPIO 17 (TX2)
Example code
The response is 9 bytes: 0xFF, 0x86, CO2 high byte, CO2 low byte, then four other bytes and a checksum. Readings in the first 3 minutes after power-up are not valid.
#include <SoftwareSerial.h>
SoftwareSerial co2(2, 3); // RX, TX
const byte READ[9] = {0xFF, 0x01, 0x86, 0, 0, 0, 0, 0, 0x79};
void setup() {
Serial.begin(115200);
co2.begin(9600);
}
void loop() {
co2.write(READ, 9);
byte r[9];
if (co2.readBytes(r, 9) == 9 && r[0] == 0xFF && r[1] == 0x86) {
byte sum = 0;
for (int i = 1; i < 8; i++) sum += r[i];
if ((byte)(0xFF - sum + 1) == r[8]) {
int ppm = r[2] * 256 + r[3];
Serial.print(ppm); Serial.println(" ppm CO2");
}
}
delay(5000);
}Setup and tuning
the sensor assumes it sees fresh air (400 ppm) at least once every 24 hours. In rooms that never air out, turn ABC off with its command and calibrate manually outdoors.
take it outside for 20 minutes and pull HD low for 7 seconds.
at head height, away from windows, doors and people's breath.
use a solid 5 V supply that handles the 150 mA lamp pulses.
What you need
Shop this list4 of 5 found in our store · ₹3,008
MH-Z19 infrared CO2 sensor module: the sensor this guide coversMH-Z19 Infrared CO2 Sensor Module – UART/PWM Output₹2,431Closest match, check the specs
ESP32-WROOM-32 38-pin development board: for Wi-Fi logging and dashboardsESP32-WROOM-32 38-Pin Development Board₹389- Uno R3 development board: the controller used in the wiring tableFind this part
0.96 inch OLED display 128x64: to show readings without a computer0.96" OLED Display 128x64₹139Closest match, check the specs
Dupont jumper wires: for the connectionsDupont Jumper Wires (40 pcs)₹49Closest match, check the specs
Alternatives and when to choose them
- SCD40 and SCD41: smaller, I2C, with temperature and humidity.
- CO2 sensors compared: MH-Z19, SCD40/41, SCD30 and S8.
- CCS811: estimates CO2 from VOCs, not a true CO2 sensor.
Try it: Uart baud rate error calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Leaving ABC on in a room that never gets fresh air, so readings drift low.
- Feeding the RX pin with 5 V logic.
- Trusting readings in the first 3 minutes.
- Mounting it where people breathe on it directly.
Frequently asked questions
What range does the MH-Z19 measure?
0 to 5,000 ppm CO2 on the common version; the useful range starts at outdoor level, about 400 ppm.
Is the MH-Z19 a real CO2 sensor?
Yes, it is NDIR, measuring CO2 itself rather than estimating it from VOCs.
What baud rate does it use?
9600 baud, 8 data bits, no parity, 1 stop bit.
What is a good indoor CO2 level?
Under about 800 ppm is well ventilated; over 1,000 to 1,500 ppm means open a window.
Does it work with ESP32?
Yes, power it from 5 V and connect the 3.3 V UART pins directly.
Parts for this guide
Calculators
Keep learning
Sources
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

