Source: https://mokxi.com/parts/mhz19
Updated: 2026-11-18

Part

# CO2 sensor, MH-Z19B

CO2 in ppm from the 0x86 serial read at 9600 baud, a 1004 ms PWM and a 0.4 to 2 V output, with zero and span calibration, for MHZ19.h.

See every part

- 7 pins
- 3 settings
- In 1 project

Drawn live by the editor's own code, at the size you see it.

At a glance

## The CO2 sensor, MH-Z19B in brief

What it is, and every setting the properties panel gives it.

The MH-Z19B is an infrared (NDIR) carbon dioxide sensor from Winsen. It reports CO2 in parts per million three ways: over serial at 9600 baud, as a PWM signal, and as an analog voltage. Every serial command is nine bytes, and the read answer carries the CO2 split into a high and low byte, plus a temperature byte. Mokxi models the command frames, the check byte, the read answer, the PWM cycle and formula, the analog voltage scale, the 7 second HD zero calibration and the 4.5 V minimum supply, all from the Winsen manual V1.3. The zero and span calibrations move readings the way they do on the real sensor, and the range setting holds the serial reading, the PWM and the analog output alike. The real sensor needs 3 minutes to warm up and takes up to 2 minutes to follow a change in the air. In Mokxi the reading follows the slider at once, exactly, with none of its 50 ppm plus 5 percent error.

Family

Sensors

Pins

7

co2

600

temperature

25

range

5000 (or 2000, 10000)

A circuit built around it, running and explained: the CO2 sensor simulator.

Pins

## 7 pins

Named as the editor names them, so a wire lands where you expect it.

### Input or output2

- VIN
- GND

### Output3

- VO
- PWM
- TX

### Input2

- HD
- RX

How to use it

## How to use the MH-Z19B CO2 sensor with an Arduino

This page features a built-in project called MH-Z19B CO2 monitor. An MH-Z19B infrared CO2 sensor sits on an Arduino Uno's SoftwareSerial, read with the MHZ19 library. It runs beside these words, and it opens in the editor as it is drawn here.

The wiring is simple. The sensor's TX goes to pin 2, RX to pin 3, VIN to 5 V, and GND to GND. The sketch prints the CO2 level, the temperature, and some advice. Fresh outdoor air is about 420 ppm. A closed room full of people climbs past 1000 ppm, which is when a window wants opening.

Running the example with the default settings, the serial monitor shows: "MH-Z19B CO2 monitor" and then "CO2 650 ppm, 25 C, fresh".

Change the sensor's co2 setting to 1500 and the sketch answers differently: "MH-Z19B CO2 monitor" and then "CO2 1500 ppm, 25 C, air the room now". Only the CO2 value and the advice changed. Drag the CO2 slider on the sensor and watch it happen live.

Part pin

Goes to

GND

Arduino Uno R3 pin GND

RX

Arduino Uno R3 pin 3

TX

Arduino Uno R3 pin 2

VIN

Arduino Uno R3 pin 5V

Click to open it in the editor

This is the simulator itself, running here. Click anything to open it in the editor.

Open this project in the editor Read the full tutorial

sketch.ino · Arduino Uno R3 · the first 45 lines

Common errors

## Common CO2 sensor, MH-Z19B errors and fixes

What goes wrong

The fix

The sketch prints no answer from the MH-Z19B.

Check that TX goes to pin 2, RX goes to pin 3, and VIN gets 5 V. Swapped TX and RX is the usual cause.

The sensor reports 0 ppm.

The simulator reports 0 when the sensor is unpowered. Make sure VIN is wired to 5 V, since under 4.5 V the sensor does nothing.

Readings slowly drift down on their own.

Automatic baseline correction is on from the factory and treats the lowest daily reading as 400 ppm. Turn it off with mhz19.autoCalibration(false) if your space is not aired every night.

The temperature or CO2 reading looks odd.

Check the errorCode after getCO2(). It is RESULT_OK after a good read, so only trust the values when it is.

Questions

## Questions and answers

### How do I read CO2 with an MH-Z19B?

Use the MHZ19 library. Call mhz19.begin(co2Serial) at 9600 baud, then mhz19.getCO2() returns the CO2 in ppm. Check that errorCode is RESULT_OK.

### What CO2 level is bad?

Fresh outdoor air is about 420 ppm. Past 1000 ppm a room is getting stuffy and a window wants opening. Past 1500 ppm, air the room now.

### What voltage does the MH-Z19B need?

It needs 4.5 V to 5.5 V on VIN. Under 4.5 V the sensor does nothing, so wire it to the Arduino's 5 V pin.

### Can I change the sensor range?

Yes. The range can be 2000, 5000 or 10000 ppm. Every output, serial, PWM and analog, is held to it. The library's setRange() sends the command.

How it is modeled

## What is true about the CO2 sensor, MH-Z19B, here

In its help article’s own words: what the simulation gets right, and where it stops.

### Modeled

The command frames, the check byte, the read answer, the PWM cycle and formula, the Vo
scale, the 7 second HD calibration and the 4.5 V minimum supply are the Winsen manual's
(V1.3). The zero and span calibrations move the readings the way they do on the real
sensor, and the range holds the serial reading, the PWM and Vo alike.

### Not modeled

The real sensor needs 3 minutes to warm up and takes up to 2 minutes to follow a change in
the air; here the reading follows the slider at once, exactly, with none of its 50 ppm plus
5 percent error. The automatic baseline correction runs once a day on the real sensor; the
command is taken here, but no run lasts a day. Commands the datasheet does not list (the
library's getRange(), getABC(), getVersion() and the unlimited read 0x85) get no
answer, because the datasheet does not say what the sensor sends back.

From MH-Z19B CO2 sensor, in full.

Projects

## See the CO2 sensor, MH-Z19B in a project

One built-in project uses it. It opens in the editor, already wired.

Boards

## The boards it is shown on

This board has a built-in project with the CO2 sensor, MH-Z19B in it.

- Arduino Uno R3

Keep going

## Keep going

- CO2 sensor, SCD41
- Air quality sensor, MQ-135

More parts

## The rest of the bench

Every one of these is drawn and simulated the same way.

Full-size breadboard

A real 0.1 inch grid with the rails and the center channel, 63 columns wide.

Jumper wires

Drag from any pin or hole to any other. Corners snap, and you can drag them.

Power

A supply rail at the voltage you choose.

Ground

The other end of every circuit.

LED

Lights when current flows. Five colors, and the brightness is what your eye would see.

Filament lamp

A tungsten bulb in a holder: an inrush when it is switched on cold, tens of milliseconds to warm up, and dim on half its voltage.

Resistor

Any value you like, with the color bands drawn to match.

ESP32-C3-DevKitM-1

A RISC-V board that runs your firmware at 160 MHz on the real memory map.

Pushbutton

A 12 mm tactile switch. Hold it while the simulation runs.

Raspberry Pi Pico

The RP2040 board on our own Cortex-M0+ core. Pick a program and press Run.

Raspberry Pi Pico W

Raspberry Pi Pico W (RP2040). The same board and the same pinout as a Pico, with the CYW43439 on it. The WiFi is simulated (no radio; real internet through the Cloud panel’s switch), and the on-board LED, which hangs off that chip rather than off GP25, is driven through the same path. Everything else is the Pico.

Raspberry Pi 4 Model B

Raspberry Pi 4 Model B: the 40-pin header, driven by main.py with RPi.GPIO or gpiozero.

See every part

## Wire up the CO2 sensor, MH-Z19B

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.

Start building free Open the editor
