Co2 project Using MH-Z16

Arduino, Seeeduino Serials and mutants. Share your problems and experence on arduino compatible board such as seeeduino/stalker, etc.

Moderators: violet, jessie

Post Reply
jiatian247
Pre-kindergarten
Pre-kindergarten
Posts: 10
Joined: Thu Jul 05, 2018 1:41 pm

Co2 project Using MH-Z16

Post by jiatian247 » Thu Jul 19, 2018 9:32 am

hi currently im using the sample code for MH-Z16. however it doesnt work. the value i get i over 20000 . Im using seeduino cloud and the grove base shield V2.

This is my code :

#include <SoftwareSerial.h>

#define pinTx 7
#define pinRx 6
SoftwareSerial s_serial(pinTx,pinRx); // TX, RX

#define sensor s_serial

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

unsigned char dataRevice[9];
int CO2PPM;

void setup() {
Serial.begin(9600);
Serial.println("get a 'g', begin to read from sensor!");
Serial.println("********************************************************");
Serial.println();

}

void loop() {
byte data[9];
int i = 0;

//transmit command data
for(i=0; i<sizeof(cmd_get_sensor); i++)
{
sensor.write(cmd_get_sensor);
}
delay(10);
//begin reveiceing data
if(sensor.available())
{
while(sensor.available())
{
for(int i=0;i<9; i++)
{
data = sensor.read();
}
}
}

for(int j=0; j<9; j++)
{
Serial.print(data[j]);
Serial.print(" ");
}
Serial.println("");

CO2PPM = (int)data[2] * 256 + (int)data[3];

Serial.print(" CO2: ");
Serial.print(CO2PPM);
Serial.println("");;
delay(1000);
}

hong.jiang
Staff
Staff
Posts: 200
Joined: Tue Apr 17, 2018 11:03 am

Re: Co2 project Using MH-Z16

Post by hong.jiang » Fri Jul 20, 2018 9:48 am

Hi,
For the Software Serial library has the following known limitations:

If using multiple software serial ports, only one can receive data at a time.
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
On Arduino or Genuino 101 the current maximum RX speed is 57600bps
On Arduino or Genuino 101 RX doesn't work on Pin 13

For more info, please refer to https://www.arduino.cc/en/Reference/SoftwareSerial

For the Seeeduino Cloud, it uses ATmega32U4, the same as Leonardo. So the Pin6 does not support the softwareserial RX. Please try 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI) for RX. thanks.

Seeed techsupport team
Bill

jiatian247
Pre-kindergarten
Pre-kindergarten
Posts: 10
Joined: Thu Jul 05, 2018 1:41 pm

Re: Co2 project Using MH-Z16

Post by jiatian247 » Fri Jul 20, 2018 1:08 pm

So i use D8 as RX and D9 as TX?

hong.jiang
Staff
Staff
Posts: 200
Joined: Tue Apr 17, 2018 11:03 am

Re: Co2 project Using MH-Z16

Post by hong.jiang » Mon Jul 23, 2018 9:26 am

Hi,yes, please try and see if it works.thanks.

Seeed techsupport team
Bill

jiatian247
Pre-kindergarten
Pre-kindergarten
Posts: 10
Joined: Thu Jul 05, 2018 1:41 pm

Re: Co2 project Using MH-Z16

Post by jiatian247 » Tue Jul 24, 2018 9:30 am

Hi, The value is still very off for me.

Code:
#include <SoftwareSerial.h>
#include <math.h>

#define pinTx 8
#define pinRx 7
SoftwareSerial s_serial(pinTx,pinRx); // TX, RX

#define sensor s_serial

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

unsigned char dataRevice[9];
int CO2PPM;

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

void loop() {
byte data[9];
int i = 0;

//transmit command data
for(i=0; i<sizeof(cmd_get_sensor); i++)
{
sensor.write(cmd_get_sensor);
}
delay(10);
//begin reveiceing data
if(sensor.available())
{
while(sensor.available())
{
for(int i=0;i<9; i++)
{
data = sensor.read();
}
}
}

for(int j=0; j<9; j++)
{
Serial.print(data[j]);
Serial.print(" ");
}
Serial.println("");

CO2PPM = (int)data[2] * 256 + (int)data[3];

Serial.print(s_serial);
Serial.print(" CO2: ");
Serial.print(CO2PPM);
Serial.println("");;
delay(1000);
}

The value :

2 98 46 24 222 164 61 9 104
1 CO2: 11800
2 98 46 24 222 164 61 9 104
1 CO2: 11800
2 98 46 24 222 164 61 9 104
1 CO2: 11800
2 98 46 24 222 164 61 9 104
1 CO2: 11800

hong.jiang
Staff
Staff
Posts: 200
Joined: Tue Apr 17, 2018 11:03 am

Re: Co2 project Using MH-Z16

Post by hong.jiang » Tue Jul 24, 2018 9:49 am

Hi there,

please try below config. Coz RX support pins: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
#define pinTx 7
#define pinRx 8

thanks.

Seeed techsupport team
Bill

jiatian247
Pre-kindergarten
Pre-kindergarten
Posts: 10
Joined: Thu Jul 05, 2018 1:41 pm

Re: Co2 project Using MH-Z16

Post by jiatian247 » Wed Jul 25, 2018 10:15 am

The value im getting. is negative

bill.yu
Elementary-2
Elementary-2
Posts: 296
Joined: Mon Apr 02, 2018 11:05 am

Re: Co2 project Using MH-Z16

Post by bill.yu » Wed Jul 25, 2018 12:58 pm

Hi there,
Here is my hardware and software.

Image

#include <SoftwareSerial.h>


const int pinRx = 9;
const int pinTx = 8;


SoftwareSerial sensor(pinTx,pinRx);


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


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


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

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 setup()
{
Serial.begin(115200);
sensor.begin(9600);
}


void loop()
{

// Serial.println("get a 'g', begin to read from sensor!");
Serial.println("********************************************************");
Serial.println();
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;

}
}


Here is the output.

Image

Bill

Post Reply