co2 sensor garbage characters on serial monitor

Grove is a modulated, ready-to-use tool set.
Much like Lego, it takes a building block approach to assembling electronics.

Moderators: lily.li, violet, salmanfarisvp, jeremy882, crail.lyu969

Post Reply
regeier
Pre-kindergarten
Pre-kindergarten
Posts: 8
Joined: Mon Oct 15, 2018 7:20 pm

co2 sensor garbage characters on serial monitor

Post by regeier » Mon Oct 15, 2018 7:25 pm

I am trying to get the grove c02 sensor working. The sensor is plugged in to port D2 on the grove expansion board on a seeeduino. I copied and pasted the example code given for the sensor but on the serial monitor I just get garbage characters output, but no temperature or co2ppm.

Can this sensor be plugged into D2 or is there anything that needs to be changed with the following configuration ?

#include <SoftwareSerial.h>
SoftwareSerial s_serial(2, 3); // TX, RX

seeed techsupport team
Staff
Staff
Posts: 44
Joined: Mon Mar 19, 2018 11:40 am

Re: co2 sensor garbage characters on serial monitor

Post by seeed techsupport team » Tue Oct 16, 2018 2:48 pm


regeier
Pre-kindergarten
Pre-kindergarten
Posts: 8
Joined: Mon Oct 15, 2018 7:20 pm

Re: co2 sensor garbage characters on serial monitor

Post by regeier » Tue Oct 16, 2018 7:41 pm

I fixed the code from that post and uploaded it, but still get garbage characters out.

Code: Select all

#include <SoftwareSerial.h>
const int pinRx = 9;
const int pinTx = 8;

SoftwareSerial s_serial(pinTx,pinRx);
#define sensor s_serial

unsigned char flg_get = 0; // if get sensor data

const unsigned char cmd_get_sensor[] =
{
    0xff, 0x01, 0x86, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x79
};

void setup()
{
  Serial.begin(115200);
  sensor.begin(9600);
}

bool sendCmdGetDta(int *gas_strength, int *temperature)
{
  for(int i=0; i<sizeof(cmd_get_sensor); i++)
  {
    sensor.write(cmd_get_sensor[i]);
  }

long cnt_timeout = 0;
while(!sensor.available()) // wait for data
{
  delay(1);
  cnt_timeout++;

  if(cnt_timeout>1000)return 0; // time out
}

int len = 0;
unsigned char dta[20];

while(sensor.available())
  {
    dta[len++] = sensor.read();
  }

  if((9 == len) && (0xff == dta[0]) && (0x86 == dta[1])) // data ok
  {
    *gas_strength = 256*dta[2]+dta[3];
    *temperature = dta[4] - 40;
    return 1;
  }
  return 0;
}

void loop()
{
  flg_get = 0;
  int gas, temp;

  if(sendCmdGetDta(&gas, &temp)) // get data ok
  {
    Serial.println("get data ok: ");
    Serial.print("gas_strength = ");
    Serial.println(gas);
    Serial.print("\ttemperature = ");
    Serial.println(temp);
  }
  else
  {
    Serial.println("get data error");
  }
  delay(1000);
}

void serialEvent()
{
while (Serial.available())
  {
    char c = Serial.read();
    if(c == 'g')flg_get = 1;
  }
}

seeed techsupport team
Staff
Staff
Posts: 44
Joined: Mon Mar 19, 2018 11:40 am

Re: co2 sensor garbage characters on serial monitor

Post by seeed techsupport team » Wed Oct 17, 2018 3:02 pm

Hi there,

please post a picture about the hardware connection. we can follow your hardware connection to simulate the issue. thanks.

regeier
Pre-kindergarten
Pre-kindergarten
Posts: 8
Joined: Mon Oct 15, 2018 7:20 pm

Re: co2 sensor garbage characters on serial monitor

Post by regeier » Wed Oct 17, 2018 3:46 pm

co2.jpeg
co2.jpeg (118.77 KiB) Viewed 2770 times

regeier
Pre-kindergarten
Pre-kindergarten
Posts: 8
Joined: Mon Oct 15, 2018 7:20 pm

Re: co2 sensor garbage characters on serial monitor

Post by regeier » Mon Oct 22, 2018 6:43 pm

Were you able to repeat the problem ?

bill.yu
Junior-7
Junior-7
Posts: 735
Joined: Mon Apr 02, 2018 11:05 am

Re: co2 sensor garbage characters on serial monitor

Post by bill.yu » Tue Oct 23, 2018 11:19 am

Hi there,

I can not repeat the issue. So please try below method to confirm if the hardware is good. thanks.

1. hardware connection, connect the co2 module to usb to ttl module., gng->gnd, Vcc-->5v, tx->rx, rx->tx.
Image

2. use the serial monitor tool to monitor the output
Image

3. you can calculate the co2 ppm by High*256+Low

thanks.

regeier
Pre-kindergarten
Pre-kindergarten
Posts: 8
Joined: Mon Oct 15, 2018 7:20 pm

Re: co2 sensor garbage characters on serial monitor

Post by regeier » Tue Oct 23, 2018 3:26 pm

Thanks for helping with this. I have now found the problem and fixed it. The sensor is working.

The baud rate on the serial monitor did not match the baud rate set at the arduino end. Once they were set the same the output is correct.

u1080665
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Mon Apr 01, 2019 1:32 am

Re: co2 sensor garbage characters on serial monitor

Post by u1080665 » Mon Jul 08, 2019 5:09 pm

This code works very well. The one posted on the website did not work for me. There must be an error with it. Can you guys look into it?
regeier wrote:
Tue Oct 16, 2018 7:41 pm
I fixed the code from that post and uploaded it, but still get garbage characters out.

Code: Select all

#include <SoftwareSerial.h>
const int pinRx = 9;
const int pinTx = 8;

SoftwareSerial s_serial(pinTx,pinRx);
#define sensor s_serial

unsigned char flg_get = 0; // if get sensor data

const unsigned char cmd_get_sensor[] =
{
    0xff, 0x01, 0x86, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x79
};

void setup()
{
  Serial.begin(115200);
  sensor.begin(9600);
}

bool sendCmdGetDta(int *gas_strength, int *temperature)
{
  for(int i=0; i<sizeof(cmd_get_sensor); i++)
  {
    sensor.write(cmd_get_sensor[i]);
  }

long cnt_timeout = 0;
while(!sensor.available()) // wait for data
{
  delay(1);
  cnt_timeout++;

  if(cnt_timeout>1000)return 0; // time out
}

int len = 0;
unsigned char dta[20];

while(sensor.available())
  {
    dta[len++] = sensor.read();
  }

  if((9 == len) && (0xff == dta[0]) && (0x86 == dta[1])) // data ok
  {
    *gas_strength = 256*dta[2]+dta[3];
    *temperature = dta[4] - 40;
    return 1;
  }
  return 0;
}

void loop()
{
  flg_get = 0;
  int gas, temp;

  if(sendCmdGetDta(&gas, &temp)) // get data ok
  {
    Serial.println("get data ok: ");
    Serial.print("gas_strength = ");
    Serial.println(gas);
    Serial.print("\ttemperature = ");
    Serial.println(temp);
  }
  else
  {
    Serial.println("get data error");
  }
  delay(1000);
}

void serialEvent()
{
while (Serial.available())
  {
    char c = Serial.read();
    if(c == 'g')flg_get = 1;
  }
}

u1080665
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Mon Apr 01, 2019 1:32 am

Re: co2 sensor garbage characters on serial monitor

Post by u1080665 » Mon Jul 08, 2019 5:11 pm

This code just save my day.
Thank you Regeier.

u1080665

Post Reply