Skip to content

Commit 16a8d9e

Browse files
Cleaned up example comments, made ESP32 and Teensy 3.2 compatible.
1 parent 7b08507 commit 16a8d9e

File tree

10 files changed

+513
-324
lines changed

10 files changed

+513
-324
lines changed

examples/BME280Compensated/BME280Compensated.ino

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
1+
/******************************************************************************
2+
BME280Compensated.ino
13
2-
/*
3-
CCS811 Air Quality Sensor Example Code
4-
By: Nathan Seidle
5-
SparkFun Electronics
6-
Date: February 7th, 2017
7-
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
4+
Marshall Taylor @ SparkFun Electronics
85
9-
Sends the humidity and temperature from a separate sensor to the CCS811 so
10-
that the CCS811 can adjust its algorithm.
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.
1113
1214
Hardware Connections (Breakoutboard to Arduino):
13-
3.3V = 3.3V
14-
GND = GND
15-
SDA = A4
16-
SCL = A5
17-
WAKE = D2
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).
1827
19-
Serial.print it out at 9600 baud to serial monitor.
20-
*/
28+
Please review the LICENSE.md file included with this example. If you have any questions
29+
or concerns with licensing, please contact [email protected].
2130
22-
#include <Wire.h>
23-
#include <SPI.h>
31+
Distributed as-is; no warranty is given.
32+
******************************************************************************/
2433
#include <SparkFunBME280.h>
2534
#include <SparkFunCCS811.h>
2635

27-
#define CCS811_ADDR 0x5B //7-bit unshifted default I2C Address
36+
#define CCS811_ADDR 0x5B //Default I2C Address
37+
//#define CCS811_ADDR 0x5A //Alternate I2C Address
38+
2839
#define PIN_NOT_WAKE 5
2940

41+
//Global sensor objects
3042
CCS811 myCCS811(CCS811_ADDR);
31-
32-
//Global sensor object
3343
BME280 myBME280;
34-
//---------------------------------------------------------------
44+
3545
void setup()
3646
{
3747
Serial.begin(9600);
3848
Serial.println();
3949
Serial.println("Apply BME280 data to CCS811 for compensation.");
4050

51+
//This begins the CCS811 sensor and prints error status of .begin()
4152
status_t returnCode = myCCS811.begin();
4253
Serial.print("CCS811 begin exited with: ");
54+
//Pass the error code to a function to print the results
4355
printDriverError( returnCode );
4456
Serial.println();
4557

@@ -67,9 +79,12 @@ void setup()
6779
//---------------------------------------------------------------
6880
void loop()
6981
{
82+
//Check to see if data is available
7083
if (myCCS811.dataAvailable())
7184
{
72-
myCCS811.readAlgorithmResults(); //Calling this function updates the global tVOC and CO2 variables
85+
//Calling this function updates the global tVOC and eCO2 variables
86+
myCCS811.readAlgorithmResults();
87+
//printInfoSerial fetches the values of tVOC and eCO2
7388
printInfoSerial();
7489

7590
float BMEtempC = myBME280.readTempC();
@@ -81,10 +96,12 @@ void loop()
8196
Serial.println(BMEhumid);
8297
Serial.println();
8398

99+
//This sends the temperature data to the CCS811
84100
myCCS811.setEnvironmentalData(BMEhumid, BMEtempC);
85101
}
86102
else if (myCCS811.checkForStatusError())
87103
{
104+
//If the CCS811 found an internal error, print it.
88105
printSensorError();
89106
}
90107

@@ -94,11 +111,13 @@ void loop()
94111
//---------------------------------------------------------------
95112
void printInfoSerial()
96113
{
114+
//getCO2() gets the previously read data from the library
97115
Serial.println("CCS811 data:");
98116
Serial.print(" CO2 concentration : ");
99117
Serial.print(myCCS811.getCO2());
100118
Serial.println(" ppm");
101119

120+
//getTVOC() gets the previously read data from the library
102121
Serial.print(" TVOC concentration : ");
103122
Serial.print(myCCS811.getTVOC());
104123
Serial.println(" ppb");
@@ -137,6 +156,11 @@ void printInfoSerial()
137156

138157
}
139158

159+
//printDriverError decodes the status_t type and prints the
160+
//type of error to the serial terminal.
161+
//
162+
//Save the return value of any function of type status_t, then pass
163+
//to this function to see what the output was.
140164
void printDriverError( status_t errorCode )
141165
{
142166
switch( errorCode )
@@ -153,14 +177,16 @@ void printDriverError( status_t errorCode )
153177
case SENSOR_INTERNAL_ERROR:
154178
Serial.print("INTERNAL_ERROR");
155179
break;
180+
case SENSOR_GENERIC_ERROR:
181+
Serial.print("GENERIC_ERROR");
182+
break;
156183
default:
157184
Serial.print("Unspecified error.");
158185
}
159186
}
160187

161-
//Displays the type of error
162-
//Calling this causes reading the contents of the ERROR register
163-
//This should clear the ERROR_ID register
188+
//printSensorError gets, clears, then prints the errors
189+
//saved within the error register.
164190
void printSensorError()
165191
{
166192
uint8_t error = myCCS811.getErrorRegister();

examples/BaselineOperator/BaselineOperator.ino

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
/*
2-
CCS811 Air Quality Sensor Example Code
3-
By: Nathan Seidle
4-
SparkFun Electronics
5-
Date: February 7th, 2017
6-
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
1+
/******************************************************************************
2+
BaselineOperator.ino
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
710
811
This example demonstrates usage of the baseline register.
912
@@ -32,13 +35,25 @@ Hardware Connections (Breakoutboard to Arduino):
3235
SDA to A4
3336
SCL to A5
3437
35-
*/
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
3644
37-
#include <Wire.h>
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 [email protected].
49+
50+
Distributed as-is; no warranty is given.
51+
******************************************************************************/
3852
#include <SparkFunCCS811.h>
3953
#include <EEPROM.h>
4054

41-
#define CCS811_ADDR 0x5B //7-bit unshifted default I2C Address
55+
#define CCS811_ADDR 0x5B //Default I2C Address
56+
//#define CCS811_ADDR 0x5A //Alternate I2C Address
4257

4358
CCS811 mySensor(CCS811_ADDR);
4459

@@ -53,7 +68,7 @@ void setup()
5368
printDriverError( returnCode );
5469
Serial.println();
5570

56-
//Check for valid data already saved
71+
//This looks for previously saved data in the eeprom at program start
5772
if((EEPROM.read(0) == 0xA5)&&(EEPROM.read(1) == 0xB2))
5873
{
5974
Serial.println("EEPROM contains saved data.");
@@ -84,11 +99,13 @@ void loop()
8499
switch(c)
85100
{
86101
case 's':
102+
//This gets the latest baseline from the sensor
87103
result = mySensor.getBaseline();
88104
Serial.print("baseline for this sensor: 0x");
89105
if(result < 0x100) Serial.print("0");
90106
if(result < 0x10) Serial.print("0");
91107
Serial.println(result, HEX);
108+
//The baseline is saved (with valid data indicator bytes)
92109
EEPROM.write(0, 0xA5);
93110
EEPROM.write(1, 0xB2);
94111
EEPROM.write(2, (result >> 8) & 0x00FF);
@@ -98,11 +115,13 @@ void loop()
98115
if((EEPROM.read(0) == 0xA5)&&(EEPROM.read(1) == 0xB2))
99116
{
100117
Serial.println("EEPROM contains saved data.");
118+
//The recovered baseline is packed into a 16 bit word
101119
baselineToApply = ((unsigned int)EEPROM.read(2) << 8) | EEPROM.read(3);
102120
Serial.print("Saved baseline: 0x");
103121
if(baselineToApply < 0x100) Serial.print("0");
104122
if(baselineToApply < 0x10) Serial.print("0");
105123
Serial.println(baselineToApply, HEX);
124+
//This programs the baseline into the sensor and monitors error states
106125
errorStatus = mySensor.setBaseline( baselineToApply );
107126
if( errorStatus == SENSOR_SUCCESS )
108127
{
@@ -119,6 +138,7 @@ void loop()
119138
}
120139
break;
121140
case 'c':
141+
//Clear data indicator and data from the eeprom
122142
Serial.println("Clearing EEPROM space.");
123143
EEPROM.write(0, 0x00);
124144
EEPROM.write(1, 0x00);
@@ -128,6 +148,7 @@ void loop()
128148
case 'r':
129149
if (mySensor.dataAvailable())
130150
{
151+
//Simply print the last sensor data
131152
mySensor.readAlgorithmResults();
132153

133154
Serial.print("CO2[");
@@ -149,6 +170,11 @@ void loop()
149170
delay(10);
150171
}
151172

173+
//printDriverError decodes the status_t type and prints the
174+
//type of error to the serial terminal.
175+
//
176+
//Save the return value of any function of type status_t, then pass
177+
//to this function to see what the output was.
152178
void printDriverError( status_t errorCode )
153179
{
154180
switch( errorCode )
@@ -165,7 +191,10 @@ void printDriverError( status_t errorCode )
165191
case SENSOR_INTERNAL_ERROR:
166192
Serial.print("INTERNAL_ERROR");
167193
break;
194+
case SENSOR_GENERIC_ERROR:
195+
Serial.print("GENERIC_ERROR");
196+
break;
168197
default:
169198
Serial.print("Unspecified error.");
170199
}
171-
}
200+
}
Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,84 @@
1-
/*
2-
CCS811 Air Quality Sensor Example Code
3-
By: Nathan Seidle
4-
SparkFun Electronics
5-
Date: February 7th, 2017
6-
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
1+
/******************************************************************************
2+
BasicReadings.ino
73
8-
Read the TVOC and CO2 values from the SparkFun CSS811 breakout board
4+
Marshall Taylor @ SparkFun Electronics
5+
Nathan Seidle @ SparkFun Electronics
96
10-
A new sensor requires at 48-burn in. Once burned in a sensor requires
11-
20 minutes of run in before readings are considered good.
7+
April 4, 2017
128
13-
Hardware Connections (Breakoutboard to Arduino):
14-
3.3V = 3.3V
15-
GND = GND
16-
SDA = A4
17-
SCL = A5
9+
https://github.com/sparkfun/CCS811_Air_Quality_Breakout
10+
https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library
1811
19-
Serial.print it out at 9600 baud to serial monitor.
20-
*/
12+
Read the TVOC and CO2 values from the SparkFun CSS811 breakout board
2113
22-
//This is the simplest example. It throws away most error information and
23-
//runs at the default 1 sample per second.
14+
This is the simplest example. It throws away most error information and
15+
runs at the default 1 sample per second.
2416
25-
//#include <Wire.h>
17+
A new sensor requires at 48-burn in. Once burned in a sensor requires
18+
20 minutes of run in before readings are considered good.
19+
20+
Hardware Connections (Breakoutboard to Arduino):
21+
3.3V to 3.3V pin
22+
GND to GND pin
23+
SDA to A4
24+
SCL to A5
25+
26+
Resources:
27+
Uses Wire.h for i2c operation
28+
29+
Development environment specifics:
30+
Arduino IDE 1.8.1
31+
32+
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
33+
34+
Please review the LICENSE.md file included with this example. If you have any questions
35+
or concerns with licensing, please contact [email protected].
36+
37+
Distributed as-is; no warranty is given.
38+
******************************************************************************/
2639
#include "SparkFunCCS811.h"
2740

28-
#define CCS811_ADDR 0x5B //7-bit unshifted default I2C Address
41+
#define CCS811_ADDR 0x5B //Default I2C Address
42+
//#define CCS811_ADDR 0x5A //Alternate I2C Address
2943

3044
CCS811 mySensor(CCS811_ADDR);
3145

3246
void setup()
3347
{
3448
Serial.begin(9600);
3549
Serial.println("CCS811 Basic Example");
36-
50+
51+
//It is recommended to check return status on .begin(), but it is not
52+
//required.
3753
status_t returnCode = mySensor.begin();
3854
if(returnCode != SENSOR_SUCCESS)
3955
{
4056
Serial.println(".begin() returned with an error.");
41-
while(1);
57+
while(1); //Hang if there was a problem.
4258
}
4359
}
4460

4561
void loop()
4662
{
63+
//Check to see if data is ready with .dataAvailable()
4764
if (mySensor.dataAvailable())
4865
{
66+
//If so, have the sensor read and calculate the results.
67+
//Get them later
4968
mySensor.readAlgorithmResults();
5069

5170
Serial.print("CO2[");
71+
//Returns calculated CO2 reading
5272
Serial.print(mySensor.getCO2());
5373
Serial.print("] tVOC[");
74+
//Returns calculated TVOC reading
5475
Serial.print(mySensor.getTVOC());
5576
Serial.print("] millis[");
77+
//Simply the time since program start
5678
Serial.print(millis());
5779
Serial.print("]");
5880
Serial.println();
5981
}
6082

61-
delay(1000); //Wait for next reading
83+
delay(10); //Don't spam the I2C bus
6284
}

0 commit comments

Comments
 (0)