skip to Main Content

RPI Node-Red: MH-Z19 CO2 Sensor

Setup an MH-Z19 using node-red and start reading data

What you will learn:
All about the MH-Z19

What you will need:

*image of the MHZ with its fancy new connector*


The MH-Z19 is an Infrared CO2 sensor that talks to the raspberry pi over a serial port. It works by shooting an infrared beam through a sample of some air and measuring the specific frequency of light that CO2 absorbs, then it can calculate the Parts per million CO2 content.


Getting Started:



Power (Red Wire) –> 2 (5v)

Ground (Black Wire) –> 6 (GND)

Tx (Blue Wire) –> 8 (#14 TXD)

Rx (Green Wire –> 10 (#15 RXD)


There are a few old versions of the air-quality nodes floating around, so the first thing we’ll have to do is update them or install them.

First open a terminal, and navigate to the node-red directory using the ‘cd’ command

cd .node-red

Next update the install nodes using npm

npm update

This could take a few minutes, next to make sure the air-quality nodes install we’ll do a manual install.

npm i node-red-contrib-easybotics-air-quality


setting up node-red:

After you’ve installed the air-quality nodes you can refresh the interface and it should update the node-palette on the left to include the new nodes.

First drag in the MH-Z19 node.


The red triangle on the node warns us that it needs some configuration, double clicking on the node opens the settings screen.
Click on the pen icon to make a new sensor-manager, this is a hidden config node that controls all the air-quality sensors in the background. This means you can have multiple C02 sensor nodes all running at the same time without interfering with each-other, because they’ll all just talk to the single sensor-manager node.

Here we have the choice to set Automatic CO2 calibration on or off.

If you have the option of taking your rpi outside to zero the sensor manually, you should pick the manual option. Otherwise the automatic mode should work okay.

Then select ‘Add’ to actually build the config node.


Reading data:

Unlike many sensor nodes, the MH-Z19 node doesn’t need to be injected; instead if automatically outputs the CO2 reading every second or so.

Attaching a debug node and deploying is all that’s needed to start looking at live CO2 PPM data.


Zeroing the CO2 sensor:

By default on ‘Automatic Mode’ the MHZ-19 will zero itself to the lowest CO2 concentration it measures in 24 hours. This will usually work in a well ventilated area, and outdoors, but in a few edge cases it’ll go out of wack.  For example if its kept in an office where people are working 20 hours a day, or never gets any fresh air at all, in these cases you’ll have to zero the sensor yourself.

First double click on the MH-Z19 node, and click the edit button here to open the sensor-manager settings

And then use this drop-down to set the calibration to manual.



The absolute best way to calibrate your CO2 sensor is to take it outside, using an extension cord to power your rpi. If you can’t get it outside, you could try sticking it out a window, or trying to get loads of fresh air into a room with one one else inside.

Once you’ve situated your sensor properly you can actually zero it

First drag in an inject node and hook it up to the sensor node like this.

Then double click on the inject node and set it to ‘string’ mode and set the string to ‘zero’


Now that it’s setup you can just hit the inject button whenever you’re ready.


You should see the CO2 PPM slowly trend to ~400 (which is the atmospheric baseline) after zeroing.


What’s Next?

Like any other sensor, the possibilities when combined with node-red are almost unlimited.
But here are some ideas to get you started


Back To Top