Dfrobot Gravity UART CO2

This is the sample code from dfrobot website, although I ordered the sensor from arduino.
A couple red flags are going off for me.

  1. There is no one on youtube with a tutorial for this thing
  2. j=0? wtf is this even there for in the i=0;i<10;i++ loop
  3. Why is it that in the loop that when i==2, i==3, i==8, it is taking a reading?
  4. Did I just buy some expensive bullshit sensor? the author is lg.gang@qq.com....? qq gang
/***************************************************
* Infrared CO2 Sensor 0-50000ppm(Wide Range)
* ****************************************************
* The follow example is used to detect CO2 concentration.

* @author lg.gang(lg.gang@qq.com)
* @version  V1.0
* @date  2016-6-6

* GNU Lesser General Public License.
* See <http://www.gnu.org/licenses/> for details.
* All above must be included in any redistribution
* ****************************************************/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order
void setup() {

  Serial.begin(9600);
  while (!Serial) {

  }
  mySerial.begin(9600);

}

void loop() {
   mySerial.write(hexdata,9);
   delay(500);

 for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {
     long hi,lo,CO2;
     int ch=mySerial.read();

    if(i==2){     hi=ch;   }   //High concentration
    if(i==3){     lo=ch;   }   //Low concentration
    if(i==8) {
               CO2=hi*256+lo;  //CO2 concentration
      Serial.print("CO2 concentration: ");
      Serial.print(CO2);
      Serial.println("ppm");
               }

  }

 }

 }

Is the TX on the adapter suppose to go to RX on the arduino? I have an Arduino UNO and RX is on the digital pwn 0 and tx is on the 1

Notice this part of the code. Pin 10 is RX and 11 is TX.
Paul

red flag #5 is that it has longs being assigned to ints. So Why bother having them declared as long? 6. This thing doesn't even tell me the sensor is a MH-Z16.

Does it work?
Paul

Haven't gotten it to work yet. No. I just switch 10 and 11 wires up back and forth, I'm getting nothing. The problem is at the if (mySerial.available()>0) {

Did you connect all the grounds, and provide power?

if mySerial.available() hangs, the device is not sending characters.

It has proper power. I'm a little hung up on this code. It doesn't seem right to me. Does it not seem right to anyone else? I'm pretty sure it's whacky code, but then again, maybe there is something I don't know and just want someone else's feedback.

I guess what I might not understand is the int ch=mySerial.read();

Perhaps it reads in a certain order and it is keeping track of its location.

I increased the delay, switched the 11 and 10 pins around for the 30th time and I now have readings.

This thread can be closed. Just luck getting it working honestly. But hey, better lucky than good.

See page 6 of the device spec to understand what is at the different locations of the 9 byte reply to the read C02 command.
https://www.winsen-sensor.com/d/files/MH-Z16.pdf

The links to this spec are on the DF robot page for the Gravity: UART Infrared CO2 sensor under the

DOCUMENTS

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.