Air Quality Sensor - SGP40 (Qwiic) Hookup Guide

Pages
Contributors: El Duderino, MAKIN-STUFF
Favorited Favorite 0

SGP40 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.

We've written a simple Arduino library to quickly get started reading data from the SGP40. Install the library through the Arduino Library Manager tool by searching for "SparkFun SGP40". Users who prefer to manually install it can get the library from the GitHub Repository or download the ZIP by clicking the button below:

Library Functions

The list below outlines all of the functions of the library with some quick descriptions of what they do.

Class

Construct the SGP40 object in the global scope. mySensor is used as the SGP40 object in the example.

  • SGP40 mySensor;

Device Setup and Settings

  • bool SGP40::begin(TwoWire &wirePort); - Initialize I2C communication with the SGP40 on the defined port. If no port is specified, Wire is used. This function also initializes the VOC index parameters.
  • void enableDebugging(Stream &debugPort = Serial); - Turn debug printouts and select the Serial port used for prints. If no port is defined, Serial is used.
  • SGP40ERR measureTest(void); - Run a self test of the SGP40 chip. Returns SUCCESS (0) on pass or specific error code on fail when debugging prints are enabled. Primarily used during manufacturing but can be helpful for debugging.
  • SGP40ERR softReset(void); - Trigger a soft reset of the SGP40. Returns SUCCESS (0) if reset is successful with debugging enabled.
  • SGP40ERR heaterOff(void); - Turn the heater off. Returns SUCCESS (0) if heater is successfully disabled with debugging enabled.

SGP40 VOC Data

Both of these data outputs have default values set for relative humidity percentage and temperature (in °C) that can be adjusted to new static values or fed a live humidity and temperature data from an external sensor.

  • SGP40ERR measureRaw(uint16_t *SRAW_ticks, float RH = 50, float T = 25); - Returns the raw signal from the SGP40 in SRAW_ticks. If debugging statements are on, returns SUCCESS (0) on pass.
  • int32_t getVOCindex(float RH = 50, float T = 25); - Return the processed VOC index data from the SGP40. Returns -100 on error. Refer to the VOC Index app note document for more information on the Sensirion VOC Index.