Built-in project · Arduino Uno R3

MH-Z19B CO2 monitor on an Arduino Uno

An MH-Z19B infrared CO2 sensor on the Uno’s SoftwareSerial, read with the MHZ19 library: the serial monitor prints the ppm and says when to open a window. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $49. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sensors
  • School
  • Using the CO2 sensor
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

An MH-Z19B infrared CO2 sensor on the Uno’s SoftwareSerial, read with the MHZ19 library: the serial monitor prints the ppm and says when to open a window.

Arduino Uno R3 runs the sketch below. The circuit around it: CO2 sensor, MH-Z19B. delay waits 2000 milliseconds. Serial.begin opens the serial port at 115200 baud.

Parts list

2 parts, plus the jumper wires. They retail for about $49; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
CO2 sensor, MH-Z19B pin GNDArduino Uno R3 pin GND
CO2 sensor, MH-Z19B pin RXArduino Uno R3 pin 3
CO2 sensor, MH-Z19B pin TXArduino Uno R3 pin 2
CO2 sensor, MH-Z19B pin VINArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// MH-Z19B CO2 monitor: an infrared CO2 sensor on SoftwareSerial.
//
//   MH-Z19B TX -> pin 2    MH-Z19B RX <- pin 3    VIN -> 5 V    GND -> GND
//
// The sensor answers a nine byte read command at 9600 baud with the CO2 in
// parts per million. The MHZ19 library sends the command and checks the
// answer, and errorCode says whether one came back. Fresh outdoor air is
// about 420 ppm; a closed room full of people climbs past 1000 ppm, which is
// when a window wants opening.
//
// Automatic baseline correction is off here: it treats the lowest reading of
// each day as 400 ppm, which is right for an office aired every night and
// wrong for a greenhouse or a bedroom.
//
// Drag the CO2 slider on the sensor and watch the advice change.

#include <MHZ19.h>
#include <SoftwareSerial.h>

SoftwareSerial co2Serial(2, 3);  // RX (the sensor's TX), TX (the sensor's RX)
MHZ19 mhz19;

const char *advice(int ppm) {
  if (ppm < 800) return "fresh";
  if (ppm < 1000) return "getting stuffy";
  if (ppm < 1500) return "open a window";
  return "air the room now";
}

void setup() {
  Serial.begin(115200);
  co2Serial.begin(9600);
  mhz19.begin(co2Serial);
  mhz19.autoCalibration(false);
  Serial.println("MH-Z19B CO2 monitor");
}

void loop() {
  int ppm = mhz19.getCO2();
  if (mhz19.errorCode != RESULT_OK) {
    Serial.println("No answer from the MH-Z19B: check TX, RX and 5 V");
    delay(2000);
    return;
  }
  float celsius = mhz19.getTemperature();

  Serial.print("CO2 ");
  Serial.print(ppm);
  Serial.print(" ppm, ");
  Serial.print(celsius, 0);
  Serial.print(" C, ");
  Serial.println(advice(ppm));
  delay(2000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(115200);
  co2Serial.begin(9600);
  mhz19.begin(co2Serial);
  mhz19.autoCalibration(false);
  Serial.println("MH-Z19B CO2 monitor");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  int ppm = mhz19.getCO2();
  if (mhz19.errorCode != RESULT_OK) {
    Serial.println("No answer from the MH-Z19B: check TX, RX and 5 V");
    delay(2000);
    return;
  }
  float celsius = mhz19.getTemperature();

  Serial.print("CO2 ");
  Serial.print(ppm);
  Serial.print(" ppm, ");
  Serial.print(celsius, 0);
  Serial.print(" C, ");
  Serial.println(advice(ppm));
  delay(2000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.