K-30 Co2 sensor shows wrong value from time to time.

Hello,

I am using a K30 from the Link below.

I want to add Co2 to a grow room and control the Co2 unit with the arduino but my sensor keep giving me wrong results, mostly -295ppm.

Is there anything I can do to change that?
This sketch is only for the Sensor, in the full sketch I also have others sensors, lcd and data logging shield but the problems with the K30 I also have with this simple sketch

#include "kSeries.h"
#include <Wire.h>
kSeries K_30(8,9);

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

}
void loop() 
{
  double co2 = K_30.getCO2('p');
 Serial.println(co2);
 
  delay(7000);

Edit:
I am using an Arduino Uno R3
and downloaded the library for the K30 here:
http://www.co2meters.com/Documentation/AppNotes/AN126-K3x-sensor-arduino-uart.zip

Can you post a link to the library?
and the datasheet of the device?

try this relative check

#include "kSeries.h"

#include <Wire.h>

kSeries K_30(8,9);

double lastCO2= 0;

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


void loop() 
{
  double co2 = K_30.getCO2('p');

  // don't expect a drop more than 20 ppm (adjust if needed)
  // check for 0 is for the first time.
  if (abs (co2 - lastCO2) < 20 || lastCO2 == 0); 
  {
    Serial.println(co2);
    lastCO2 = co2;
  }
  delay(7000);
}

or do an absolute check

void loop() 
{
  double co2 = K_30.getCO2('p');

  if (co2 > 100); // abs minimum expected
  {
    Serial.println(co2);
  }
  delay(7000);
}

have you checked the wires?

Thank you very much for the reply.

Here is the Library I am using: http://www.co2meters.com/Documentation/AppNotes/AN126-K3x-sensor-arduino-uart.zip

Here is the data sheet: http://co2meters.com/Documentation/Datasheets/DS30-01%20-%20K30.pdf

This is my first Arduino project and I have zero coding experience, so I don't understand what the code you gave me is doing but I ran it and still have the same issue, here the result from the serial:

681.00
681.00
681.00
680.00
681.00
681.00
680.00
679.00
680.00
679.00
679.00
678.00
-295.00
675.00
674.00
674.00
673.00
673.00
673.00

For now the Sensor is still on a breadboard, so I have switched cabled and even the breadboard but no change.

Just in case the slow print speed and use of delay was causing the problem, try this.
(change your print speed to 115200 in the serial monitor)

#include "kSeries.h"
#include <Wire.h>
kSeries K_30(8, 9);

unsigned long previousMillis = 0;
const long interval = 7000;

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    double co2 = K_30.getCO2('p');
    Serial.println(co2);
  }
}

Here, I changed baudrate back to 9600. Changed co2 variable from double to unsigned long.

#include "kSeries.h"
#include <Wire.h>
kSeries K_30(8, 9);

unsigned long previousMillis = 0;
const long interval = 7000;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    unsigned long co2 = K_30.getCO2('p');
    Serial.println(co2);
  }
}

Thanks dlloyd, I have tried it but the result is also not good:

624
624
624
623
4294967003
4294967001
623
623
623
622

This will not solve whatever is causing the negative read (could be connections), however if a negative read is made for co2, it will read again. (checks twice).

include "kSeries.h"
#include <Wire.h>
kSeries K_30(8, 9);

unsigned long previousMillis = 0;
const long interval = 7000;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    double co2 = K_30.getCO2('p');
    (co2 < 0) ? co2 = K_30.getCO2('p') : co2; //re-read if negative
    (co2 < 0) ? co2 = K_30.getCO2('p') : co2; //re-read if negative
   Serial.println(co2);
  }
}

EDIT: There would need to be 3 negative reads in a row before co2 would end up negative.

Hey dlloyd,

I tested it with the re reading and think this is good enough, even though it doesn't solve the root cause.
Thank you very much for your help!

Found this app note:

Application Note AN144: K30 Sensor Lockups with Arduino

Wow, thank you very much.
So it seems the problem should be the 5V, at the moment the the Sensor is in use and will log data for a week, after that i will check if i can solve the problem with the suggestions from the Application note.

Hi, I actually have the same problem as you, always a negative value. I tried to do as Application Note AN144: K30 Sensor Lockups with Arduino recommend but still the same issue. Drunken1337: do you have any progress on the issue?