MH-Z19B

It meassures the CO2 concentration
Supplies




Used materials:
- 3 jumper wires.
- Arduino-board UNO 65139 ATMega328.
- USB for Arduino.
- MH-Z19B sensor.
- Computer or laptop.
- Arduino Editor to import the code (https://create.arduino.cc/editor).
CONNECT THE WIRES
MH-Z19 CO2 Sensor Arduino Uno
VCC ================================ ⇒ 5V
GND ================================ ⇒ GND
PWM ================================ ⇒ D10
RX ================================== ⇒ A0
TX ================================== ⇒ A1
Programming


#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A1); // RX, TX
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
char response[9];
#define pwmPin 10
int prevVal = LOW;
long th, tl, h, l, ppm, ppm2 = 0.0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(pwmPin, INPUT);
}
void loop(){
mySerial.write(cmd,9);
mySerial.readBytes(response, 9);
int responseHigh = (int) response[2];
int responseLow = (int) response[3];
ppm = (256*responseHigh)+responseLow;
//CO2 via pwm
do {
th = pulseIn(pwmPin, HIGH, 1004000) / 1000.0;
tl = 1004 - th;
ppm2 = 2000 * (th-2)/(th+tl-4);
} while (ppm2 < 0.0);
Serial.println(ppm);
Serial.println(ppm2);
Serial.println("-----------");
delay(5000);
}
Save and Upload



- Press the save button
- Press upload
- Press on monitor and see the results