SparkFun Air Quality Sensor - SGP30 (Qwiic) Hookup Guide

Pages
Favorited Favorite 2

SGP30 Arduino Library

Note: This library assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

The SparkFun SGP30 Arduino library can be downloaded with the Arduino library manager by searching 'SparkFun SGP30' or you can grab the zip here from the GitHub repository:

Library Functions

The list below outlines all of the functions of the library with some quick descriptions of what they do. The examples cover most of the functions so take a look at those for demonstrations of how to integrate them in your code.

Before we get into the heart of the functions, we'll first cover a setup type definition that helps with debugging. This enum parses the four integer values returned by the SGP30 to a more "human readable" way.

language:c
typedef enum {
SUCCESS = 0,
ERR_BAD_CRC,
ERR_I2C_TIMEOUT,
SELF_TEST_FAIL
} SGP30ERR;

This enum allows us to simply tell what error code was returned by its description instead of getting a number for an error which you would then have to "decode". For example, instead of returning error == 0 an error check returns error == SUCCESS. Now, on to the rest of the functions.

Device Setup & Settings

  • bool begin(TwoWire &wirePort); - Initializes the sensor on the I2C bus and selects the wire port.
  • void initAirQuality(void); - Initializes air quality measurements on the SGP30. Must be called prior to calling measureAirQuality.
  • void generalCallReset(void); - Performs a soft reset of the SGP30. Not device specific and this call can reset any attached device that support the General Call Mode.
  • getFeatureSetVersion(void); - Returns a readout of the feature set version number. Refer to page 8 of the datasheet for more information.
  • getSerialID(void); - Returns a readout of the serial ID register to identify the chip and verify the sensor is connected.
  • measureTest(void); - Runs a self test on the SGP30. This function is primarily used by the manufacturer for testing.

Measurement Functions

  • measureAirQuality(void); - Returns CO2 values in ppm and TVOC values in ppb. Call in regular 1 second intervals to maintain baseline calculations.
  • setBaseline(void); - Updates the baseline values to a previous baseline. Recommended to only use previously retrieved baselines to maintain accuracy.
  • getBaseline(void); - Returns the current calculated baseline from the SGP30's dynamic baseline calculations.
  • setHumidity(uint16_t humidity); - Set a humidity value for compensation. Default is 15.5g/m3 and if modified, the new humidity value should be taken from a humidity sensor like the SHTC3. Sending 0x0000 resets to default and turns off humidity compensation. Refer to "Example3_Humidity" in our Arduino library for more information on calculating and sending this value.
  • measureRawSignals(void); - Returns raw signals for H2 and ethanol. These values are used as inputs for on-chip calibration and baseline compensation algorithms. Refer to page 8 of the datasheet for more information.

With the core functions of this Arduino library outlined and explained, it's time to start putting them together in some example code.