Skip to content

Commit 0362e03

Browse files
committed
formatting, added example, added sensor presence check
1 parent 903c7d2 commit 0362e03

File tree

11 files changed

+134
-49
lines changed

11 files changed

+134
-49
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example reads the sensors calculated CO2 and TVOC values
1414
*/
1515

@@ -24,7 +24,10 @@ void setup() {
2424
//Sensor supports I2C speeds up to 400kHz
2525
Wire.setClock(400000);
2626
//Initialize sensor
27-
mySensor.begin();
27+
if (mySensor.begin() != SUCCESS) {
28+
Serial.println("No SGP30 Detected. Check connections.");
29+
while (1);
30+
}
2831
//Initializes sensor for air quality readings
2932
mySensor.initAirQuality();
3033
}

examples/Example2_RawSignals/Example2_RawSignals.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
13-
This example reads the sensors calculated CO2 and
12+
13+
This example reads the sensors calculated CO2 and
1414
TVOC values and the raw values for H2 and ethanol.
1515
*/
1616

@@ -25,7 +25,10 @@ void setup() {
2525
//Sensor supports I2C speeds up to 400kHz
2626
Wire.setClock(400000);
2727
//Initialize sensor
28-
mySensor.begin();
28+
if (mySensor.begin() != SUCCESS) {
29+
Serial.println("No SGP30 Detected. Check connections.");
30+
while (1);
31+
}
2932
//Initializes sensor for air quality readings
3033
mySensor.initAirQuality();
3134

examples/Example3_Humidity/Example3_Humidity.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example gets relative humidity from a sensor, convertts it to absolute humidty,
1414
and updates the SGP30's humidity compensation with the absolute humidity value.
1515
*/
@@ -28,7 +28,10 @@ void setup() {
2828
Wire.begin();
2929
Wire.setClock(400000);
3030
//Initialize the SGP30
31-
mySensor.begin();
31+
if (mySensor.begin() != SUCCESS) {
32+
Serial.println("No SGP30 Detected. Check connections.");
33+
while (1);
34+
}
3235

3336
//Initialize the humidity sensor and ping it
3437
hSensor.begin();

examples/Example4_ErrorChecking/Example4_ErrorChecking.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example measures CO2 and TVOC and reports any errors.
1414
*/
1515

@@ -25,7 +25,10 @@ void setup() {
2525
//Sensor supports I2C speeds up to 400kHz
2626
Wire.setClock(400000);
2727
//Initialize sensor
28-
mySensor.begin();
28+
if (mySensor.begin() != SUCCESS) {
29+
Serial.println("No SGP30 Detected. Check connections.");
30+
while (1);
31+
}
2932
//Initializes sensor for air quality readings
3033
mySensor.initAirQuality();
3134
}

examples/Example5_DeviceInfo/Example5_DeviceInfo.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example gets the sensor's serial ID and version number.
1414
*/
1515

@@ -24,7 +24,10 @@ void setup() {
2424
//Sensor supports I2C speeds up to 400kHz
2525
Wire.setClock(400000);
2626
//Initialize sensor
27-
mySensor.begin();
27+
if (mySensor.begin() != SUCCESS) {
28+
Serial.println("No SGP30 Detected. Check connections.");
29+
while (1);
30+
}
2831
//Get SGP30's ID
2932
mySensor.getSerialID();
3033
//Get version number

examples/Example6_SchedulerReadings/Example6_SchedulerReadings.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example tasks a scheduler with reading air quality,
1414
freeing the main loop from wasteful delays.
1515
*/
@@ -30,7 +30,10 @@ void setup() {
3030
Serial.begin(9600);
3131
Wire.begin();
3232
Wire.setClock(400000);
33-
mySensor.begin();
33+
if (mySensor.begin() != SUCCESS) {
34+
Serial.println("No SGP30 Detected. Check connections.");
35+
while (1);
36+
}
3437
//initialize scheduler
3538
runner.init();
3639
//add task t1 to the schedule

examples/Example7_Reset/Example7_Reset.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example performs a soft reset on the sensor and restores its baseline.
1414
*/
1515

@@ -26,12 +26,15 @@ void setup() {
2626
Serial.begin(9600);
2727
Wire.begin();
2828
Wire.setClock(400000);
29-
mySensor.begin();
29+
if (mySensor.begin() != SUCCESS) {
30+
Serial.println("No SGP30 Detected. Check connections.");
31+
while (1);
32+
}
3033
mySensor.initAirQuality();
3134
//First fifteen readings will be
3235
//CO2: 400 ppm TVOC: 0 ppb
3336
//We will reset after 30 readings
34-
for (count; count < 30; count++)
37+
for (count; count < 30; count++)
3538
{
3639
delay(1000); //Wait 1 second
3740
mySensor.measureAirQuality();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Library for the Sensirion SGP30 Indoor Air Quality Sensor
3+
By: Ciara Jekel
4+
SparkFun Electronics
5+
Date: June 28th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
9+
10+
Feel like supporting our work? Buy a board from SparkFun!
11+
https://www.sparkfun.com/products/14813
12+
13+
This example reads the sensors calculated CO2 and TVOC values using Software Wire
14+
*/
15+
16+
#include "SparkFun_SGP30_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_SGP30
17+
#include <SoftwareWire.h>
18+
19+
SGP30 mySensor; //create an object of the SGP30 class
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
//Initialize sensor, specifying I2C port
24+
if (mySensor.begin(Wire) != SUCCESS) {
25+
Serial.println("No SGP30 Detected. Check connections.");
26+
while (1);
27+
}
28+
//Initializes sensor for air quality readings
29+
mySensor.initAirQuality();
30+
}
31+
32+
void loop() {
33+
//First fifteen readings will be
34+
//CO2: 400 ppm TVOC: 0 ppb
35+
delay(1000); //Wait 1 second
36+
//measure CO2 and TVOC levels
37+
mySensor.measureAirQuality();
38+
Serial.print("CO2: ");
39+
Serial.print(mySensor.CO2);
40+
Serial.print(" ppm\tTVOC: ");
41+
Serial.print(mySensor.TVOC);
42+
Serial.println(" ppb");
43+
44+
}

examples/Example9_UnitTest/Example9_UnitTest.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
SparkFun Electronics
55
Date: June 28th, 2018
66
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7-
7+
88
SGP30 Datasheet: https://cdn.sparkfun.com/assets/4/7/d/f/b/Sensirion_SGP30_Datasheet.pdf
99
1010
Feel like supporting our work? Buy a board from SparkFun!
1111
https://www.sparkfun.com/products/14813
12-
12+
1313
This example performs the sensor's self test and displays the results.
1414
*/
1515

@@ -22,7 +22,10 @@ void setup() {
2222
Serial.begin(9600);
2323
Wire.begin();
2424
Wire.setClock(400000);
25-
mySensor.begin();
25+
if (mySensor.begin() != SUCCESS) {
26+
Serial.println("No SGP30 Detected. Check connections.");
27+
while (1);
28+
}
2629
error = mySensor.measureTest();
2730
if (error == SUCCESS) {
2831
Serial.println("Success!");

src/SparkFun_SGP30_Library.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ SGP30::SGP30() {
6868
SGP30ERR SGP30::begin(TwoWire &wirePort) {
6969
_i2cPort = &wirePort; //Grab which port the user wants us to use
7070
_i2cPort->begin();
71+
getSerialID();
72+
if (serialID != 0x646762) return NO_SENSOR;
7173
return SUCCESS;
7274
}
7375

@@ -100,15 +102,17 @@ SGP30ERR SGP30::measureAirQuality(void) {
100102
toRead = _i2cPort->requestFrom(_SGP30Address, (uint8_t)6);
101103
}
102104
if (counter == 12) return ERR_I2C_TIMEOUT; //Error out
103-
CO2 = _i2cPort->read() << 8; //store MSB in CO2
104-
CO2 |= _i2cPort->read(); //store LSB in CO2
105+
_CO2 = _i2cPort->read() << 8; //store MSB in CO2
106+
_CO2 |= _i2cPort->read(); //store LSB in CO2
105107
uint8_t checkSum = _i2cPort->read(); //verify checksum
106-
if (checkSum != _CRC8(CO2)) return ERR_BAD_CRC; //checksum failed
107-
TVOC = _i2cPort->read() << 8; //store MSB in TVOC
108-
TVOC |= _i2cPort->read(); //store LSB in TVOC
108+
if (checkSum != _CRC8(_CO2)) return ERR_BAD_CRC; //checksum failed
109+
_TVOC = _i2cPort->read() << 8; //store MSB in TVOC
110+
_TVOC |= _i2cPort->read(); //store LSB in TVOC
109111
checkSum = _i2cPort->read(); //verify checksum
110-
if (checkSum != _CRC8(TVOC)) return ERR_BAD_CRC; //checksum failed
112+
if (checkSum != _CRC8(_TVOC)) return ERR_BAD_CRC; //checksum failed
111113
_i2cPort->endTransmission();
114+
CO2 = _CO2; //publish valid data
115+
TVOC = _TVOC; //publish valid data
112116
return SUCCESS;
113117
}
114118

@@ -133,15 +137,17 @@ SGP30ERR SGP30::getBaseline(void) {
133137
toRead = _i2cPort->requestFrom(_SGP30Address, (uint8_t)6);
134138
}
135139
if (counter == 12) return ERR_I2C_TIMEOUT; //Error out
136-
baselineCO2 = _i2cPort->read() << 8; //store MSB in baselineCO2
137-
baselineCO2 |= _i2cPort->read(); //store LSB in baselineCO2
140+
_baselineCO2 = _i2cPort->read() << 8; //store MSB in _baselineCO2
141+
_baselineCO2 |= _i2cPort->read(); //store LSB in _baselineCO2
138142
uint8_t checkSum = _i2cPort->read(); //verify checksum
139-
if (checkSum != _CRC8(baselineCO2)) return ERR_BAD_CRC; //checksum failed
140-
baselineTVOC = _i2cPort->read() << 8; //store MSB in baselineTVOC
141-
baselineTVOC |= _i2cPort->read(); //store LSB in baselineTVOC
143+
if (checkSum != _CRC8(_baselineCO2)) return ERR_BAD_CRC; //checksum failed
144+
_baselineTVOC = _i2cPort->read() << 8; //store MSB in _baselineTVOC
145+
_baselineTVOC |= _i2cPort->read(); //store LSB in _baselineTVOC
142146
checkSum = _i2cPort->read(); //verify checksum
143-
if (checkSum != _CRC8(baselineTVOC)) return ERR_BAD_CRC; //checksum failed
147+
if (checkSum != _CRC8(_baselineTVOC)) return ERR_BAD_CRC; //checksum failed
144148
_i2cPort->endTransmission();
149+
baselineCO2 = _baselineCO2; //publish valid data
150+
baselineTVOC = _baselineTVOC; //publish valid data
145151
return SUCCESS;
146152
}
147153

@@ -193,11 +199,12 @@ SGP30ERR SGP30::getFeatureSetVersion(void) {
193199
toRead = _i2cPort->requestFrom(_SGP30Address, (uint8_t)3);
194200
}
195201
if (counter == 3) return ERR_I2C_TIMEOUT; //Error out
196-
featureSetVersion = _i2cPort->read() << 8; //store MSB in featureSetVerison
197-
featureSetVersion |= _i2cPort->read(); //store LSB in featureSetVersion
202+
_featureSetVersion = _i2cPort->read() << 8; //store MSB in featureSetVerison
203+
_featureSetVersion |= _i2cPort->read(); //store LSB in featureSetVersion
198204
uint8_t checkSum = _i2cPort->read(); //verify checksum
199-
if (checkSum != _CRC8(featureSetVersion)) return ERR_BAD_CRC; //checksum failed
205+
if (checkSum != _CRC8(_featureSetVersion)) return ERR_BAD_CRC; //checksum failed
200206
_i2cPort->endTransmission();
207+
featureSetVersion = _featureSetVersion; //publish valid data
201208
return SUCCESS;
202209
}
203210

@@ -218,15 +225,17 @@ SGP30ERR SGP30::measureRawSignals(void) {
218225
toRead = _i2cPort->requestFrom(_SGP30Address, (uint8_t)6);
219226
}
220227
if (counter == 5) return ERR_I2C_TIMEOUT; //Error out
221-
H2 = _i2cPort->read() << 8; //store MSB in H2
222-
H2 |= _i2cPort->read(); //store LSB in H2
228+
_H2 = _i2cPort->read() << 8; //store MSB in H2
229+
_H2 |= _i2cPort->read(); //store LSB in H2
223230
uint8_t checkSum = _i2cPort->read(); //verify checksum
224-
if (checkSum != _CRC8(H2)) return ERR_BAD_CRC; //checksumfailed
225-
ethanol = _i2cPort->read() << 8; //store MSB in ethanol
226-
ethanol |= _i2cPort->read(); //store LSB in ethanol
231+
if (checkSum != _CRC8(_H2)) return ERR_BAD_CRC; //checksumfailed
232+
_ethanol = _i2cPort->read() << 8; //store MSB in ethanol
233+
_ethanol |= _i2cPort->read(); //store LSB in ethanol
227234
checkSum = _i2cPort->read(); //verify checksum
228-
if (checkSum != _CRC8(ethanol)) return ERR_BAD_CRC; //checksum failed
235+
if (checkSum != _CRC8(_ethanol)) return ERR_BAD_CRC; //checksum failed
229236
_i2cPort->endTransmission();
237+
H2 = _H2; //publish valid data
238+
ethanol = _ethanol; //publish valid data
230239
return SUCCESS;
231240
}
232241

@@ -267,7 +276,7 @@ SGP30ERR SGP30::getSerialID(void) {
267276
if (checkSum1 != _CRC8(_serialID1)) return ERR_BAD_CRC; //checksum failed
268277
if (checkSum2 != _CRC8(_serialID2)) return ERR_BAD_CRC; //checksum failed
269278
if (checkSum3 != _CRC8(_serialID3)) return ERR_BAD_CRC; //checksum failed
270-
serialID = ((uint64_t)_serialID1 << 32) + ((uint64_t)_serialID2 << 16) + ((uint64_t)_serialID3);
279+
serialID = ((uint64_t)_serialID1 << 32) + ((uint64_t)_serialID2 << 16) + ((uint64_t)_serialID3); //publish valid data
271280
_i2cPort->endTransmission();
272281
return SUCCESS;
273282
}

src/SparkFun_SGP30_Library.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <Wire.h>
4545
typedef enum {
4646
SUCCESS = 0,
47+
NO_SENSOR,
4748
ERR_BAD_CRC,
4849
ERR_I2C_TIMEOUT,
4950
SELF_TEST_FAIL
@@ -131,7 +132,14 @@ class SGP30
131132
//SGP30's I2C address
132133
const byte _SGP30Address = 0x58;
133134

134-
//Stores serialID
135+
//private versions of public vars so only valid data is user accessible
136+
uint16_t _CO2;
137+
uint16_t _TVOC;
138+
uint16_t _baselineCO2;
139+
uint16_t _baselineTVOC;
140+
uint16_t _featureSetVersion;
141+
uint16_t _H2;
142+
uint16_t _ethanol;
135143
uint16_t _serialID1;
136144
uint16_t _serialID2;
137145
uint16_t _serialID3;

0 commit comments

Comments
 (0)