I am trying to use MH-Z19B CO2 sensor (spec: http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf) with Arduino UNO. But asbolutely no luck.
Wiring:
sensor Vin → Arduino pin 5V
sensor GND → Arduino pin GND
sensor PWM → Arduino pin 9
sensor RX → Arduino pin 6
sensor TX → Arduino pin 7
#include <SoftwareSerial.h>
SoftwareSerial co2Serial(7, 6); // define MH-Z19 RX TX
void setup() {
Serial.begin(9600);
co2Serial.begin(9600);
pinMode(9, INPUT);
}
void loop() {
readCO2();
delay(3000);
}
int readCO2(){
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
byte response[9]; // for answer
co2Serial.write(cmd, 9); //request PPM CO2
memset(response, 0, 9);
co2Serial.readBytes(response, 9);
// print response in hexa
for (int i = 0; i < 9; i++) {
Serial.print(String(response[i], HEX));
Serial.print(" ");
}
Serial.println("");
//CO2 via pwm
unsigned long th, tl, ppm;
do {
th = pulseIn(9, HIGH, 1004000) / 1000;
tl = 1004 - th;
ppm = 5000 * (th-2)/(th+tl-4);
} while (th == 0);
Serial.println(ppm);
return;
}
Code is only for debugging purposes as the UART response is mostly all zeroes. (only at the beginning there are some values). And the PWM returns something between 20 to 40.
ff 0 0 0 0 0 0 0 0
20
ff ff 0 0 0 0 0 0 0
20
ff ff ff 0 0 0 0 0 0
20
ff ff 0 0 0 0 0 0 0
20
ff 18 6 10 38 ff 0 0 0
20
0 0 0 0 0 0 0 0 0
30
0 0 0 0 0 0 0 0 0
20
0 0 0 0 0 0 0 0 0
35
0 0 0 0 0 0 0 0 0
20
0 0 0 0 0 0 0 0 0
20
Sensor LEDs are blinking so it looks it is alive. But have no idea how to check it better. I have also tried to wait until the sensor will heat up (about 3 mins according to spec), but also no luck.
Hope my wiring or code have some big mistake you will see (and I cannot see after several hours of try-and-fail).
Thanks a lot.