co2 sensor garbage characters on serial monitor

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

Hi there,



please refer to <URL url="https://forum7.seeedstudio.com/t/co2-project-using-mh-z16/5848/1]<LINK_TEXT text=“viewtopic.php?f=16&t=7754&p=26798&hilit … CO2#p26798”>https://forum7.seeedstudio.com/t/co2-project-using-mh-z16/5848/1]. thanks.

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

[code]#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 = 256dta[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;
}
}[/code]

Hi there,



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

[attachment=0]co2.jpeg[/attachment]
co2.jpeg

Were you able to repeat the problem ?

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.




  2. use the serial monitor tool to monitor the output




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



    thanks.

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.

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?

This code just save my day.

Thank you Regeier.



u1080665

hi I’ve tried this code too but my serial monitor shows “get data error” I’ve tried other codes too and it doesn’t work.

Im using Arduino mega with mega shield to connect. Any help will be appreciated thanks.


[/quote]