arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

CCS811_OLED_Demo.ino (1928B)


      1 /* This demo shows how to display the CCS811 readings on an Adafruit I2C OLED. 
      2  * (We used a Feather + OLED FeatherWing)
      3  */
      4  
      5 #include <SPI.h>
      6 #include <Wire.h>
      7 #include <Adafruit_GFX.h>
      8 
      9 #include <Adafruit_SSD1306.h>
     10 #include "Adafruit_CCS811.h"
     11 
     12 Adafruit_CCS811 ccs;
     13 Adafruit_SSD1306 display = Adafruit_SSD1306();
     14 
     15 void setup() {  
     16   Serial.begin(115200);
     17 
     18   if(!ccs.begin()){
     19     Serial.println("Failed to start sensor! Please check your wiring.");
     20     while(1);
     21   }
     22   
     23   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
     24   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
     25 
     26   // Show image buffer on the display hardware.
     27   // Since the buffer is intialized with an Adafruit splashscreen
     28   // internally, this will display the splashscreen.
     29   display.display();
     30   delay(500);
     31 
     32   // Clear the buffer.
     33   display.clearDisplay();
     34   display.display();
     35 
     36   //calibrate temperature sensor
     37   while(!ccs.available());
     38   float temp = ccs.calculateTemperature();
     39   ccs.setTempOffset(temp - 25.0);
     40   
     41   Serial.println("IO test");
     42 
     43   // text display tests
     44   display.setTextSize(1);
     45   display.setTextColor(WHITE);
     46 }
     47 
     48 
     49 void loop() {
     50   display.setCursor(0,0);
     51   if(ccs.available()){
     52     display.clearDisplay();
     53     float temp = ccs.calculateTemperature();
     54     if(!ccs.readData()){
     55       display.print("eCO2: ");
     56       Serial.print("eCO2: ");
     57       float eCO2 = ccs.geteCO2();
     58       display.print(eCO2);
     59       Serial.print(eCO2);
     60       
     61       display.print(" ppm\nTVOC: ");
     62       Serial.print(" ppm, TVOC: ");      
     63       float TVOC = ccs.getTVOC();
     64       display.print(TVOC);
     65       Serial.print(TVOC);
     66       
     67       Serial.print(" ppb   Temp:");
     68       display.print(" ppb\nTemp: ");
     69       Serial.println(temp);
     70       display.println(temp);
     71       display.display();
     72     }
     73     else{
     74       Serial.println("ERROR!");
     75       while(1);
     76     }
     77   }
     78   delay(500);
     79 }
     80