Get readings from Grove CO2 sensor [SOLVED]

Hello!

I’m trying to log Readings from my Grove CO2-sensor ( You can find it here; https://www.seeedstudio.com/Grove-CO2-Sensor-p-1863.html ).

I’ve tried some example codes, but they all seem quite complicated. Since I use Particle Build there are also quite a few of the libraries used in those codes which are not there. I 've tried to add the needed libraries but those libraries also use other libraries (it’s an eternal struggle). Does anyone know a basic code to get readings from this sensor?

I’m Grateful for any tip.

@tlintved, this sensor is 5v powered and has a UART so you will need to connect the sensor to the Photon this way (assuimg Photon is USB powered):

Photon   Sensor
---------------
Vin      power
GND      GND
RX       TX
TX       RX

This uses the Serial1 port of the Photon versus the SoftwareSerial of the sample code on the sensor’s wiki. The code needs to be adapted slightly:

#define DEBUG 0
 
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()
{
    Serial1.begin(9600);    // Sensor uses Serial1 on Photon
    Serial.begin(115200);  // This is the USB serial port on the Photon
    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++)
    {
        Serial1.write(cmd_get_sensor[i]);
    }
    delay(10);
    //begin reveiceing data
    if(Serial1.available())
    {
        while(Serial1.available())
        {
            for(int i=0;i<9; i++)
            {
                data[i] = Serial1.read();
            }
        }
    }
 
#if DEBUG
    for(int j=0; j<9; j++)
    {
        Serial.print(data[j]);
        Serial.print(" ");
    }
    Serial.println("");
#endif
 
    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;
}

:smiley:

@peekay123 Thank you, it works !

Right now my photon is USB Powered, but I’m planning to hook it up with a battery later on. Are you telling me this will affect how I connect the sensor to the photon and the code as well?

@tlintved, the sensor needs a minimum of 4.5v to work. I suggest that for battery operation, you consider either a buck or boost regulator to get 5v to power the sensor and possibly, the Photon, depending on your design requirements. The code is not affected.

2 Likes