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[i]);
}
delay(10);
//begin reveiceing data
if(sensor.available())
{
while(sensor.available())
{
for(int i=0;i<9; i++)
{
data[i] = 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);
}
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
So i use D8 as RX and D9 as TX?
Hi,yes, please try and see if it works.thanks.
Seeed techsupport team
Bill
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[i]);
}
delay(10);
//begin reveiceing data
if(sensor.available())
{
while(sensor.available())
{
for(int i=0;i<9; i++)
{
data[i] = 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
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
The value im getting. is negative
Hi there,
Here is my hardware and software.
#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[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 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.
Bill