Example4_SetBaseline.ino (5298B)
1 /****************************************************************************** 2 Adjust baseline 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 demonstrates usage of the baseline register. 12 13 To use, wait until the sensor is burned in, warmed up, and in clean air. Then, 14 use the terminal to save the baseline to EEPROM. Aftewards, the sensor can be 15 powered up in dirty air and the baseline can be restored to the CCS811 to help 16 the sensor stablize faster. 17 18 EEPROM memory usage: 19 20 addr: data 21 ---------- 22 0x00: 0xA5 23 0x01: 0xB2 24 0x02: 0xnn 25 0x03: 0xmm 26 27 0xA5B2 is written as an indicator that 0x02 and 0x03 contain a valid number. 28 0xnnmm is the saved data. 29 30 The first time used, there will be no saved data 31 32 Hardware Connections (Breakoutboard to Arduino): 33 3.3V to 3.3V pin 34 GND to GND pin 35 SDA to A4 36 SCL to A5 37 38 Resources: 39 Uses Wire.h for i2c operation 40 Uses EEPROM.h for internal EEPROM driving 41 42 Development environment specifics: 43 Arduino IDE 1.8.1 44 45 This code is released under the [MIT License](http://opensource.org/licenses/MIT). 46 47 Please review the LICENSE.md file included with this example. If you have any questions 48 or concerns with licensing, please contact techsupport@sparkfun.com. 49 50 Distributed as-is; no warranty is given. 51 ******************************************************************************/ 52 #include <Wire.h> 53 #include <SparkFunCCS811.h> //Click here to get the library: http://librarymanager/All#SparkFun_CCS811 54 #include <EEPROM.h> 55 56 #define CCS811_ADDR 0x5B //Default I2C Address 57 //#define CCS811_ADDR 0x5A //Alternate I2C Address 58 59 CCS811 mySensor(CCS811_ADDR); 60 61 void setup() 62 { 63 Serial.begin(115200); 64 Serial.println(); 65 Serial.println("CCS811 Baseline Example"); 66 67 Wire.begin(); 68 69 //This begins the CCS811 sensor and prints error status of .beginWithStatus() 70 CCS811Core::CCS811_Status_e returnCode = mySensor.beginWithStatus(); 71 Serial.print("CCS811 begin exited with: "); 72 //Pass the error code to a function to print the results 73 Serial.print(mySensor.statusString(returnCode)); 74 Serial.println(); 75 76 //This looks for previously saved data in the eeprom at program start 77 if ((EEPROM.read(0) == 0xA5) && (EEPROM.read(1) == 0xB2)) 78 { 79 Serial.println("EEPROM contains saved data."); 80 } 81 else 82 { 83 Serial.println("Saved data not found!"); 84 } 85 Serial.println(); 86 87 Serial.println("Program running. Send the following characters to operate:"); 88 Serial.println(" 's' - save baseline into EEPROM"); 89 Serial.println(" 'l' - load and apply baseline from EEPROM"); 90 Serial.println(" 'c' - clear baseline from EEPROM"); 91 Serial.println(" 'r' - read and print sensor data"); 92 } 93 94 void loop() 95 { 96 char c; 97 unsigned int result; 98 unsigned int baselineToApply; 99 CCS811Core::CCS811_Status_e errorStatus; 100 if (Serial.available()) 101 { 102 c = Serial.read(); 103 switch (c) 104 { 105 case 's': 106 //This gets the latest baseline from the sensor 107 result = mySensor.getBaseline(); 108 Serial.print("baseline for this sensor: 0x"); 109 if (result < 0x100) 110 Serial.print("0"); 111 if (result < 0x10) 112 Serial.print("0"); 113 Serial.println(result, HEX); 114 //The baseline is saved (with valid data indicator bytes) 115 EEPROM.write(0, 0xA5); 116 EEPROM.write(1, 0xB2); 117 EEPROM.write(2, (result >> 8) & 0x00FF); 118 EEPROM.write(3, result & 0x00FF); 119 break; 120 case 'l': 121 if ((EEPROM.read(0) == 0xA5) && (EEPROM.read(1) == 0xB2)) 122 { 123 Serial.println("EEPROM contains saved data."); 124 //The recovered baseline is packed into a 16 bit word 125 baselineToApply = ((unsigned int)EEPROM.read(2) << 8) | EEPROM.read(3); 126 Serial.print("Saved baseline: 0x"); 127 if (baselineToApply < 0x100) 128 Serial.print("0"); 129 if (baselineToApply < 0x10) 130 Serial.print("0"); 131 Serial.println(baselineToApply, HEX); 132 //This programs the baseline into the sensor and monitors error states 133 errorStatus = mySensor.setBaseline(baselineToApply); 134 if (errorStatus == CCS811Core::CCS811_Stat_SUCCESS) 135 { 136 Serial.println("Baseline written to CCS811."); 137 } 138 else 139 { 140 Serial.print("Error writing baseline: "); 141 Serial.println(mySensor.statusString(errorStatus)); 142 } 143 } 144 else 145 { 146 Serial.println("Saved data not found!"); 147 } 148 break; 149 case 'c': 150 //Clear data indicator and data from the eeprom 151 Serial.println("Clearing EEPROM space."); 152 EEPROM.write(0, 0x00); 153 EEPROM.write(1, 0x00); 154 EEPROM.write(2, 0x00); 155 EEPROM.write(3, 0x00); 156 break; 157 case 'r': 158 if (mySensor.dataAvailable()) 159 { 160 //Simply print the last sensor data 161 mySensor.readAlgorithmResults(); 162 163 Serial.print("CO2["); 164 Serial.print(mySensor.getCO2()); 165 Serial.print("] tVOC["); 166 Serial.print(mySensor.getTVOC()); 167 Serial.print("]"); 168 Serial.println(); 169 } 170 else 171 { 172 Serial.println("Sensor data not available."); 173 } 174 break; 175 default: 176 break; 177 } 178 } 179 delay(10); 180 }