Running Grove CO2 Sensor mh-z16 with WIO Terminal

Hey

I’m trying to set up the Grove Sensor mh-z16 with the Wio Terminal using UART.

I tried to connect the sensor to the right Grove port of Wio terminal and running this code from seeedstudio:

Grove - CO2 Sensor | Seeed Studio Wiki

/*
  This test code is write for Arduino AVR Series(UNO, Leonardo, Mega)
  If you want to use with LinkIt ONE, please connect the module to D0/1 and modify:

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

  #define sensor Serial1
*/


#include <SoftwareSerial.h>
SoftwareSerial s_serial(2, 3);      // 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 temperature;
int CO2PPM;
 
void setup()
{
    sensor.begin(9600);
    Serial.begin(115200);
    Serial.println("get a 'g', begin to read from sensor!");
    Serial.println("********************************************************");
    Serial.println();
}
 
void loop()
{
    if(dataRecieve())
    {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print("  CO2: ");
        Serial.print(CO2PPM);
        Serial.println("");
    }
    delay(1000);
}
 
bool dataRecieve(void)
{
    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("");
 
    if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]))) != data[8])
    {
        return false;
    }
    
    CO2PPM = (int)data[2] * 256 + (int)data[3];
    temperature = (int)data[4] - 40;
 
    return true;
}

My output is in the serial terminal is only:

60 22 0 32 109 78 0 0 1
60 22 0 32 109 78 0 0 1
60 22 0 32 109 78 0 0 1

Also get the same if I remove the senor from the WIO terminal.

I did some search for how to use UART on the WIO terminal and found also, that it is necessary to switch the cables for RX/TX when using the right grove port:

[Wio Terminal: grove port on the right and UART modules – lucadentella.it](https://Wio Terminal: grove port on the right and UART modules – lucadentella.it)

However, this also did not help to get the signals. I’m also not sure if I can use UART while using the USB connection? Do I have to use software UART with the left port?

Can you propose me some setup to read the values? Do you think it is possible to run the sensor using USB supply, or is the power consumption of the sensor to high?

Hi there,
I find it easier to connect these to the TTL to RS232 converter hardware and test with the terminal port on windows to verify the comms. Then move to the code platform and know the sensor is working prior too.
That way the rest is easier to complete, IMO. YMMV
HTH
GL :slight_smile: PJ

Also you can’t hurt it if you connect it up backwards. TX/RX wise. :v:

Thank you very much! Will try this