0

I want to get CO2 value by using MH-Z19. I tried this code with Arduino uno R3 but I always get this result (CRC 0/0). I didn't understand where is the problem. I linked A0 with TX of sensor and A1 with RX, I powered the card with 3.3V/Vin to visualize the result on PC via USB port.

 #include <SoftwareSerial.h>;

 SoftwareSerial mySerial(A0, A1); // A0 - к TX сенсора, A1 - к RX

 byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; 
  unsigned char response[9];

 void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

 void loop() 
{
mySerial.write(cmd, 9);
 memset(response, 0, 9);
 mySerial.readBytes(response, 9);

  int i;
byte crc = 0;
 for (i = 1; i < 8; i++) crc+=response[i];
 crc = 255 - crc;
 crc++;

  if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) 
 {
Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
  } else {
unsigned int responseHigh = (unsigned int) response[2];
unsigned int responseLow = (unsigned int) response[3];
unsigned int ppm = (256*responseHigh) + responseLow;
Serial.println(ppm);
}
2
  • Start by modifying your sketch to print out the return value of readBytes() and the actual byte values in the receive buffer. If you have a scope or logic analyzer you might try monitoring the command and response directly. If SoftwareSerial has the same 1 second default timeout as hardware serial, it is probably never getting a response and timing out with an empty buffer still at the initialized zeroes. Perhaps you should check that the return value of readBytes() indicates the expected number of characters have been received. Jun 9 '17 at 21:59
  • @ChrisStratton thank you for your response. I changed the softwareSerial pins to Digital one ( pin 0 Arduino (RX) linked to TX of sensor and pin 1 Arduino (TX) linked to RX of sensor), I get this as output: 32 - 30 - 20 - 2D - 20 - 33 - 33 - 20 - 2D - END I think it is not correct since according to the datasheet the first byte should always be 0xFF Jun 13 '17 at 18:52
1

From the datasheet, working voltage for MH-Z19 is 3.6V to 5.5V, so 3.3V is not enough. (Although the interface runs at 3.3V).

enter image description here

Also, the Uno might not run reliably at 16MHz and 3.3V.

enter image description here

Edited image from datasheet page 303.

3
  • thank you for your response. I have the same result (CRC 0/0) even with Wemos D1 powered with 5V. May 23 '17 at 21:35
  • @user3921905 -- You should probably add the details of all you have tested to the question. Also please post a pic of your setup with wiring, etc.
    – Wesley Lee
    May 23 '17 at 21:43
  • I updated my question. Jun 9 '17 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.