# Dfrobot Gravity UART CO2

**URL:** <https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144>\
**Category:** Sensors\
**Created:** [October 9, 2021, 4:57am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144 "2021-10-09T04:57:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 9, 2021, 4:57am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/1 "2021-10-09T04:57:04Z")

</div>

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](mailto:lg.gang@qq.com)....? qq gang

```cpp
/ ***************************************************
* 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");
               }

  }

 }

 }

```

---

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 9, 2021, 4:58am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/2 "2021-10-09T04:58:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [October 9, 2021, 5:05am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/3 "2021-10-09T05:05:20Z")

</div>

> [@virtual\_virus](#):
>
> ```arduino
> #include <SoftwareSerial.h>
> SoftwareSerial mySerial(10, 11); // RX, TX
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 9, 2021, 5:06am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/4 "2021-10-09T05:06:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [October 9, 2021, 5:07am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/5 "2021-10-09T05:07:30Z")

</div>

Does it work?  
Paul

---

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 9, 2021, 5:08am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/6 "2021-10-09T05:08:58Z")

</div>

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) {`

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 9, 2021, 2:03pm UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/7 "2021-10-09T14:03:09Z")

</div>

Did you connect all the grounds, and provide power?

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

---

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 10, 2021, 12:20am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/8 "2021-10-10T00:20:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![virtual\_virus](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@virtual\_virus](https://forum.arduino.cc/u/virtual_virus)\
**Post date:** [October 10, 2021, 12:45am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/9 "2021-10-10T00:45:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [October 10, 2021, 1:01am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/10 "2021-10-10T01:01:41Z")

</div>

> [@virtual\_virus](#):
>
> 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.

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](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

- [Product wiki](https://www.dfrobot.com/wiki/index.php/Infrared_CO2_Sensor_0-50000ppm_SKU:_SEN0220)
- [More Documents](https://www.dfrobot.com/wiki/index.php/Infrared_CO2_Sensor_0-50000ppm_SKU:_SEN0220#More)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 8, 2022, 1:01am UTC](https://forum.arduino.cc/t/dfrobot-gravity-uart-co2/913144/11 "2022-04-08T01:01:41Z")

</div>

Closed on April 8, 2022, 1:01am UTC

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