Go Down

Topic: Problem with MH-Z19 serial communication with arduino mega (Read 1 time) previous topic - next topic

rahulasok

I am using MH-Z19 NDIR sensor with my arduino mega. I write a command through serial 0 and get the response.
Code: [Select]


//CO2 from MH-Z19 NDIR
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte response[9];
Serial.write(cmd,9);
Serial.readBytes(response,9);

if (response[0] != 0xFF)
  {
//Error
predictCo2 = 0;

  }

else if (response[1] != 0x86)
  {
//Error
predictCo2 = 1;

  }

  else{

  predictCo2=((response[2]*256)+response[3]);

  }




I expect the output to be in a particular format. response[0] has to be 0xFF and response[1] has to be 0x86.

I am having issues getting the output in the above mentioned format occasionally. But, sometimes it starts reading in the proper format straightaway.

Once its starts reading in the proper format, it never goes back to the error state unless I power it off. If I power the device off, I will have to plug out plug in multiple times until I see the values in proper format.

I would like to know if I am missing something in the code here. Should I be  using serial.flush() somewhere in the code to clean the serial stream before I send and receive data? Or will this be a problem with the sensor itself? 

Wawa

I write a command through serial 0...
Serial 0 is already used by the onboard USB/serial chip.

There are three other unused serial channels on a Mega.
No experience with that module, but I see it's 3.3volt logic.
Might have to use a 1:2 voltage divider on the module's RX pin (if used).
Leo..

rahulasok

Wawa,

 Thanks for the reply.

This is a customized version of mega board where in we have just 2 channels, serial 0 and serial 1. We are already using serial1 for communicating with a wifi chip. And yes, voltage divider circuit is there too.

Gimli007

Hi,
I have the same problem with this sensor, was connected only once. In other cases there was problem with response[0].
Did you find please, the solution?

A33a

I would like to know if I am missing something in the code here. Should I be  using serial.flush() somewhere in the code to clean the serial stream before I send and receive data? Or will this be a problem with the sensor itself? 
Hi,

I have the same issue dealing with a serial communication with this sensor. It works fine when the serial monitor of Arduino IDE is activated, but when it runs without monitoring it probably not synchronizes the serial communication, so reading from the stream leads to wrong sequence of bytes.

In my case I just synchronized the serial communication with the following piece of code before reading from the stream:

Code: [Select]

  Serial.write(cmd, 9);

  while(Serial.available() > 0 && (unsigned char)Serial.peek() != 0xFF) {
    Serial.read();
  }
 
  Serial.readBytes(response, 9);


So we skip all the bytes in the stream until we reach the first byte of the response (0xFF).

In this case calling a flush() function before reading is probably better way to do it, but apparently it didn't work for me.

Go Up