Example2_BME280Compensation.ino (5403B)
1 /****************************************************************************** 2 Compensating the CCS811 with humidity readings from the BME280 3 4 Marshall Taylor @ SparkFun Electronics 5 6 April 4, 2017 7 8 https://github.com/sparkfun/CCS811_Air_Quality_Breakout 9 https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library 10 11 This example uses a BME280 to gather environmental data that is then used 12 to compensate the CCS811. 13 14 Hardware Connections (Breakoutboard to Arduino): 15 3.3V to 3.3V pin 16 GND to GND pin 17 SDA to A4 18 SCL to A5 19 20 Resources: 21 Uses Wire.h for i2c operation 22 23 Development environment specifics: 24 Arduino IDE 1.8.1 25 26 This code is released under the [MIT License](http://opensource.org/licenses/MIT). 27 28 Please review the LICENSE.md file included with this example. If you have any questions 29 or concerns with licensing, please contact techsupport@sparkfun.com. 30 31 Distributed as-is; no warranty is given. 32 ******************************************************************************/ 33 #include <Wire.h> 34 #include <SparkFunBME280.h> //Click here to get the library: http://librarymanager/All#SparkFun_BME280 35 #include <SparkFunCCS811.h> //Click here to get the library: http://librarymanager/All#SparkFun_CCS811 36 37 #define CCS811_ADDR 0x5B //Default I2C Address 38 //#define CCS811_ADDR 0x5A //Alternate I2C Address 39 40 #define PIN_NOT_WAKE 5 41 42 //Global sensor objects 43 CCS811 myCCS811(CCS811_ADDR); 44 BME280 myBME280; 45 46 void setup() 47 { 48 Serial.begin(115200); 49 Serial.println(); 50 Serial.println("Apply BME280 data to CCS811 for compensation."); 51 52 Wire.begin(); 53 54 //This begins the CCS811 sensor and prints error status of .beginWithStatus() 55 CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus(); 56 Serial.print("CCS811 begin exited with: "); 57 Serial.println(myCCS811.statusString(returnCode)); 58 59 //For I2C, enable the following and disable the SPI section 60 myBME280.settings.commInterface = I2C_MODE; 61 myBME280.settings.I2CAddress = 0x77; 62 63 //Initialize BME280 64 //For I2C, enable the following and disable the SPI section 65 myBME280.settings.commInterface = I2C_MODE; 66 myBME280.settings.I2CAddress = 0x77; 67 myBME280.settings.runMode = 3; //Normal mode 68 myBME280.settings.tStandby = 0; 69 myBME280.settings.filter = 4; 70 myBME280.settings.tempOverSample = 5; 71 myBME280.settings.pressOverSample = 5; 72 myBME280.settings.humidOverSample = 5; 73 74 //Calling .begin() causes the settings to be loaded 75 delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. 76 myBME280.begin(); 77 } 78 //--------------------------------------------------------------- 79 void loop() 80 { 81 //Check to see if data is available 82 if (myCCS811.dataAvailable()) 83 { 84 //Calling this function updates the global tVOC and eCO2 variables 85 myCCS811.readAlgorithmResults(); 86 //printInfoSerial fetches the values of tVOC and eCO2 87 printInfoSerial(); 88 89 float BMEtempC = myBME280.readTempC(); 90 float BMEhumid = myBME280.readFloatHumidity(); 91 92 Serial.print("Applying new values (deg C, %): "); 93 Serial.print(BMEtempC); 94 Serial.print(","); 95 Serial.println(BMEhumid); 96 Serial.println(); 97 98 //This sends the temperature data to the CCS811 99 myCCS811.setEnvironmentalData(BMEhumid, BMEtempC); 100 } 101 else if (myCCS811.checkForStatusError()) 102 { 103 //If the CCS811 found an internal error, print it. 104 printSensorError(); 105 } 106 107 delay(2000); //Wait for next reading 108 } 109 110 //--------------------------------------------------------------- 111 void printInfoSerial() 112 { 113 //getCO2() gets the previously read data from the library 114 Serial.println("CCS811 data:"); 115 Serial.print(" CO2 concentration : "); 116 Serial.print(myCCS811.getCO2()); 117 Serial.println(" ppm"); 118 119 //getTVOC() gets the previously read data from the library 120 Serial.print(" TVOC concentration : "); 121 Serial.print(myCCS811.getTVOC()); 122 Serial.println(" ppb"); 123 124 Serial.println("BME280 data:"); 125 Serial.print(" Temperature: "); 126 Serial.print(myBME280.readTempC(), 2); 127 Serial.println(" degrees C"); 128 129 Serial.print(" Temperature: "); 130 Serial.print(myBME280.readTempF(), 2); 131 Serial.println(" degrees F"); 132 133 Serial.print(" Pressure: "); 134 Serial.print(myBME280.readFloatPressure(), 2); 135 Serial.println(" Pa"); 136 137 Serial.print(" Pressure: "); 138 Serial.print((myBME280.readFloatPressure() * 0.0002953), 2); 139 Serial.println(" InHg"); 140 141 Serial.print(" Altitude: "); 142 Serial.print(myBME280.readFloatAltitudeMeters(), 2); 143 Serial.println("m"); 144 145 Serial.print(" Altitude: "); 146 Serial.print(myBME280.readFloatAltitudeFeet(), 2); 147 Serial.println("ft"); 148 149 Serial.print(" %RH: "); 150 Serial.print(myBME280.readFloatHumidity(), 2); 151 Serial.println(" %"); 152 153 Serial.println(); 154 } 155 156 //printSensorError gets, clears, then prints the errors 157 //saved within the error register. 158 void printSensorError() 159 { 160 uint8_t error = myCCS811.getErrorRegister(); 161 162 if (error == 0xFF) //comm error 163 { 164 Serial.println("Failed to get ERROR_ID register."); 165 } 166 else 167 { 168 Serial.print("Error: "); 169 if (error & 1 << 5) 170 Serial.print("HeaterSupply"); 171 if (error & 1 << 4) 172 Serial.print("HeaterFault"); 173 if (error & 1 << 3) 174 Serial.print("MaxResistance"); 175 if (error & 1 << 2) 176 Serial.print("MeasModeInvalid"); 177 if (error & 1 << 1) 178 Serial.print("ReadRegInvalid"); 179 if (error & 1 << 0) 180 Serial.print("MsgInvalid"); 181 Serial.println(); 182 } 183 }