Library for CCS811 CO2/VOC sensor for testing.

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Library for CCS811 CO2/VOC sensor for testing.

Post by p0lyg0n1 »

Hi,
Finally I have ported a library for CCS811 (AMS) . You can get it here for tests (see attachmens). I'll add it to next beta.
You have to use clock stretching enable Wire library from there https://forum.z-wave.me/viewtopic.php?f=3427&t=28957 to get it work.
Test sketch code

Code: Select all

#define MY_SERIAL Serial0
#include <ZUNO_CCS811.h> 

ZUNO_CCS811 ccs811;
CCS811_Environment env_data = {500, 2500}; // <--- You need an external humidity & temperature sensor like DHT22 or BME280 to make it accurate, Here is default values 50.0% 25.00*C
void setup() {
    
    MY_SERIAL.begin(115200);
    if(ccs811.begin()){
        MY_SERIAL.println("CCS811 found!");
        ccs811.setEnvironmentalData(&env_data);
    }


}
void loop() {
    if(ccs811.readData() & CCS811_STATUS_DATAREADY){
         MY_SERIAL.print("CO2: ");
         MY_SERIAL.print(ccs811.geteCO2());
         MY_SERIAL.println(" ppm");
         MY_SERIAL.print("VOC: ");
         MY_SERIAL.print(ccs811.getTVOC());
         MY_SERIAL.println(" ppb");
    }
    delay(5000);
}
Have FUN )
Attachments
ZUNO_CCS811.zip
(3.72 KiB) Downloaded 423 times
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by michap »

Nice work!
Can confirm that lib is working with my sensor too.
As discussed, my sensor had another device-ID, so I had to modify the setting.

Would suggest to add a flag, that sensor was not found in readData, so that not invalid values can be given back.

BTW: code is working only from 2.1.4 b5 , here the new libs are integrated, with older versions you will get a compiler error.

Thanks,
Michael
AlexML
Posts: 1
Joined: 10 May 2018 04:58

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by AlexML »

Hi,
I tried the library for CCS811. However, I found the incorrect operation of setDriveMode(). Correctly works only modes CCS811_DRIVE_MODE_250MS and CCS811_DRIVE_MODE_10SEC, attempts to use any modes other thanCCS811_DRIVE_MODE_10SEC, CCS811_DRIVE_MODE_250MS resulted in failure.
Do you have new versions of the library, other than the published one?
Alexey.
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by michap »

@AlexML,
I'm just "burning in" the sensor (first 48 hours, regarding datasheet), so I haven't tested the other modes or any more details.
Just the sample above with the library in latest beta before 2.1.4 release.
Will test more when sensor is "ready" for use :)

Michael
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by michap »

I just have tested the different modes and they are working fine.

If you change the sample sketch - please note about the requirements (regarding documentation)
When a sensor operating mode is changed to a new mode with a lower sample rate (e.g. from Mode 1 to Mode 3), it should be placed in Mode 0 (Idle) for at least 10 minutes before enabling the new mode. When a sensor operating mode is changed to a new mode with a higher sample rate (e.g. from Mode 3 to Mode 1), there is no requirement to wait before enabling the new mode.
Additional - there should not be a request of sensor values in shorter time as the given interval - I have tested with twice the interval as sample (for 1sec - delay(2000), for 10sec - delay(20000),... etc.)

Of course you could ignore the status flag (new value available) - then you will get the values in any time.

Michael
Miklux
Posts: 31
Joined: 01 Oct 2018 17:02

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by Miklux »

Hi all,

please I need your help.
I'm working on a ZWave Air quality sensor with the CCS811 device.
The device is connected to the ZUNO device and I've also connected a temperature and humidity sensor in the same I2C bus.

The device is configured as a FLIRS device, then when a packet is received, the device awakes , set the nWAKE pin to LOW state (awake the CCS811) , and uses the CCS811 library in order to read the CO2 and VOC values.

First I read the temperature and humidity values and next i read the CO2 and VOC values...

The problem is that when I associate the device to my ZWAVE network (I use ZWAY on a Raspberry PI 3 with a UZB stick) the CO2 and VOC values are always 0 (zero)... the temperature and humidity values are correct...

Do you have some suggestion ?

in the following post i will place my code
Thanks in advance
Mik
Miklux
Posts: 31
Joined: 01 Oct 2018 17:02

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by Miklux »

This is my code:

Code: Select all

#include <ZUNO_CCS811.h> 
#include <Wire.h>

ZUNO_SETUP_CHANNELS(ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, 
                                          SENSOR_MULTILEVEL_SCALE_CELSIUS,
                                          SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                                          SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, 
                                          temperature_getter),
                    ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY, 
                                          SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, 
                                          SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                                          SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, 
                                          humidity_getter),
                    ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_CO2_LEVEL, 
                                          SENSOR_MULTILEVEL_SCALE_PARTS_PER_MILLION, 
                                          SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                                          SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, 
                                          CO2_getter),
                    ZUNO_SENSOR_MULTILEVEL(0x27, 
                                          0x01, 
                                          SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                                          SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, 
                                          VOC_getter));

                                          // il sensore VOC non è implementato nella libreria hw ZUno
                                          // 0x27 fa riferimento alla classe sensori VOC della ZWave alliance
                                          // 0x01 è la scala PPM della ZWave alliance 

// next macro sets up the sleeping mode
// device will wake up by user request and regulary listening for packets
ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_FREQUENTLY_AWAKE);
// Setting up battery capacity according to millivolts on 3V pin of Z-Uno
// 2650 = 0%
// 3300 = 100%
ZUNO_SETUP_BATTERY_LEVELS(2650, 3300);

#define WAKEUP_BEAMS_TIME 60 // every 1 minutes measure and send reports  

#define I2CAddress_TH 0x44 //indirizzo I2C del sensore T ed RH 

float T; //temperatura
float RH; //umidità relativa
int T_int; // 16 bit temperatura
int RH_int; //16 bit umidità relativa
uint8_t dataTH[6];

float VOCvalue;
float CO2value;
int VOC_int;
int CO2_int;
ZUNO_CCS811 ccs811;
CCS811_Environment env_data; // = {500, 2500}; // <--- You need an external humidity & temperature sensor like DHT22 or BME280 to make it accurate, Here is default values 50.0% 25.00*C


void setup() {
  pinMode(1, OUTPUT);      // sets the digital pin as output
  pinMode(0, OUTPUT);      // sets the digital pin as output
  Serial.begin(115200);
  Wire.begin();
  digitalWrite(0,LOW); //attivo il sensore CCS811
  ccs811.begin(0x5A);
  ccs811.setDriveMode(0x10);
  digitalWrite(0,HIGH); //metto in sleep il sensore CCS811
  
  T_int=0;
  RH_int=0;
  T=0.0;
  RH=0.0;

  VOCvalue=0.0;
  CO2value=0.0;
  VOC_int=0;
  CO2_int=0;
  
}


void loop() {
  // Send device to sleep ANYWAY ) 
  digitalWrite(1, HIGH); // accendo i sensori
  SCC30ReadData();
  Serial.print(T_int/10);
  Serial.println(" gradi Celsius");
  Serial.print(RH_int/10);
  Serial.println(" Percento");
  //env_data.temperature = (word) T_int;
  //env_data.humidity = RH_int;
  delay(100);
  digitalWrite(0,LOW); //attivo il sensore CCS811
  delay(50);
  //while(!ccs811.available());
  //ccs811.setEnvironmentalData(&env_data); 
  if(ccs811.readData() & CCS811_STATUS_DATAREADY){
     Serial.print("CO2: ");
     CO2_int=ccs811.geteCO2();
     Serial.print(CO2_int);
     Serial.println(" ppm");
     Serial.print("VOC: ");
     VOC_int=ccs811.getTVOC();
     Serial.print(VOC_int);
     Serial.println(" ppb");
  }
  digitalWrite(0,HIGH); // metto in sleep il sensore CCS811
  digitalWrite(1, LOW); // spengo i sensori

  
  if(zunoGetWakeReason() == ZUNO_WAKEUP_REASON_WUT)
  {
    
    zunoSendReport(1);
    zunoSendReport(2);
    zunoSendReport(3);
    zunoSendReport(4);
     
  }
  
  // Set a timer to wakeup the device
  
   
  //zunoSetBeamCountWU(WAKEUP_BEAMS_TIME);
  zunoSendDeviceToSleep();
  //delay(2000);
}

void SCC30ReadData() {
  Wire.beginTransmission(I2CAddress_TH);
  Wire.write(0x24); 
  Wire.write(0x00);
  Wire.endTransmission();
  delay(16);
  Wire.requestFrom(I2CAddress_TH, 6);
  while(!Wire.available());
  for(int i=0; i<6; i++) dataTH[i]=Wire.read();

  // convert to Temperature/Humidity
  uint16_t val;
  float mA=-45;
  float mB=175;
  float mC=65535;
  float mX=0;
  float mY=100;
  float mZ=65535;
  
  val = (dataTH[0] << 8) + dataTH[1];
  T = mA + mB * (val / mC);

  val = (dataTH[3] << 8) + dataTH[4];
  RH = mX + mY * (val / mZ);

  T_int=( int) T*10;
  RH_int=( int) RH*10;
  
}


int temperature_getter()
{
  return T_int;
}
int humidity_getter()
{
  return RH_int;
}

int CO2_getter()
{
  return CO2_int;
}

int VOC_getter()
{
  return VOC_int;
}




michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by michap »

Hi,
Miklux wrote:
10 Nov 2021 19:20
The problem is that when I associate the device to my ZWAVE network (I use ZWAY on a Raspberry PI 3 with a UZB stick) the CO2 and VOC values are always 0 (zero)...
What give the
Serial.print(CO2_int);
Serial.print(VOC_int);
in your application as result? Real values?

So is it Z-Way related?

Michael
Miklux
Posts: 31
Joined: 01 Oct 2018 17:02

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by Miklux »

Hi Michael,

when i connect the device in the ZWay network I can't see the serial monitor because when the device goes in sleep mode the Serial connection is closed.
Then I can't read the printed values and I don't know what Serial.print(CO2) and Serial.print(VOC) gives...

Do you have some suggestion?

The temperature and humidity sensor is ok: I read the values in the dashboard via the ZWay software but still CO2 and VOC are 0.

It's strage because if i try to use the Zuno device only as a master I2C, without the Zwave conection and without the sleep moed, i can print all the values correctly.

Same code but without zwave and sleepmode parts... I'm going crazy :roll:
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: Library for CCS811 CO2/VOC sensor for testing.

Post by michap »

Hi,

for checking the output you could use Serial0 and pins on TX0 and RX0 together with an USB to serial converter.

But as you wrote the output is normal if device is not sleeping...

At first I would try to check the Z-Wave configuration.

Change your code to any fixed value, as sample:

Code: Select all

int CO2_getter()
{
  CO2_int= 765;
  return CO2_int;
}

int VOC_getter()
{
  VOC_int= 234;
  return VOC_int;
}
If this will report the values you can be sure that code is ok.

Then I would check the used mode cor CCS811 - there are 5 modes, depending on the modes the CCS will return the values for CO2 and TVOC.
As sample in mode 3 you will get the values every 60 seconds...

In your code I see:

Code: Select all

if(ccs811.readData() & CCS811_STATUS_DATAREADY){...
But you cannot be sure that there are ready data, right?
Maybe the default value ("0") will be reported (for testing change the default value...).

In such case you could make a while loop (delay while data is not ready - with timeout) and read then the value - but this can take of course some time and battery (I think it will be a battery FLiRS device...).
In datasheet:
"Mode 3: Low power pulse heating mode IAQ measurement every 60 seconds" - so there should be every 60 seconds a new value available.

Michael
Post Reply