SparkFun Air Quality Sensor - SGP30 (Qwiic) Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus
Favorited Favorite 1

Arduino Examples

The SGP30 Arduino Library includes nine examples to get you started. For the scope of this tutorial, we'll just cover the first three as they demonstrate the basics of setting up and reading from the SGP30.

Example 1 - Basic Readings

A basic example to get you started reading TVOC and CO2 readings from the SGP30. To open this example, navigate to File > Examples > SparkFun SGP30 Arduino Library > Example1_BasicReadings. Next, open the Tools menu and select your board (in our case, Arduino Uno) and the correct Port your board enumerated on.

Upload the code, open the Arduino Serial Monitor and set your baud rate to 9600. If the sensor initialization returns false, the code will print out "No SGP30 Detected. Check connections." via serial. That error will almost certainly be the result of a bad connection to the SGP30. After successful initialization, it will start to print out TVOC and CO2 readings. Take note that for the first 15 seconds after sensor power up or reset, the readings will be: CO2 : 400 ppm and TVOC : 0 ppb so if you are logging the data using, say, the OpenLog Artemis, you will want to ignore these first 15 readings.

After the first 15 readings have passed, try breathing lightly near the sensor and you should see the CO2 readings jump significantly from the 400 ppm baseline. These basic readings are good to get started and making sure your sensor is connected and functioning, but for more accurate readings, you'll probably want to include a compensation value from a humidity sensor like we demonstrate in Example 3 - Humidity.

Example 2 - Raw Signals

This example includes both the TVOC and CO2 readings from the SGP30 along with raw readings for ethanol and H2. The Ethanol and H2 values are primarily for internal processing on the SGP30 but you can use them for rough measurements of both gases.

Open the example from the Examples menu as we did above and select Example2_RawSignals. Select your Board and Port and upload the code. Open the Arduino Serial Monitor again and set your baud to 9600. You should see readings for CO2, TVOC, Raw H2 and Raw Ethanol every second.

Example 3 - Humidity

This example demonstrates how to pass humidity data from an external sensor (in this case, the SparkFun Humidity Sensor Breakout - SHTC3 (Qwiic)) to modify the SGP30's humidity compensation with an actual absolute humidity value. Passing an actual humidity value to the SGP30's humidity compensation will help increase the accuracy and reliability of the sensor.

In order to use this example as is you will need to also have the SparkFun SHTC3 Arduino Library installed. With both libraries installed, simply connect an SHTC3 Breakout to your Qwiic chain and upload the example to add a humidity compensation value to your SGP30's readings!

SGP30 and SHTC3 Connected to RedBoard Qwiic

This example initializes both sensors and, in the setup, takes a temperature and humidity reading from the SHTC3. The code then converts those readings to absolute humidity, parses that into a fixed 8.8 bit value the SGP30 can accept for humidity compensation and then sends that data to the SGP30 using the setHumidity function. After this value is set, the code runs through the primary routine of reading CO2 and TVOC values and printing them over serial.

The value stored for the on-chip humidity compensation will remain static until a new value is sent or the SGP30 is reset. We have included an option to send the letter "H" (upper or lower case) through your serial monitor to adjust the humidity compensation value to a new reading from the SHTC3 to avoid having to reset the circuit.

To update the compensation value, open the Serial Monitor at any time after initialization and set the baud to 9600. Type in "H", hit enter and the code will reset the humidity compensation value using an updated reading from the SHTC3. The code will also print out the new value in g/m3 and then resume taking CO2 and TVOC readings from the SGP30. The code below shows that if statement and how the humidity value is calculated from the SHTC3's temperature and humidity data:

language:c
if (Serial.available())
{
char ch = Serial.read();
if (ch == 'H' || ch == 'h')
{
  SHTC3_Status_TypeDef result = hSensor.update();
  delay(190);  
  // Measure Relative Humidity from the Si7021
  float humidity = hSensor.toPercent();

  //Measure temperature (in C) from the Si7021
  float temperature = hSensor.toDegC();

  //Convert relative humidity to absolute humidity
  double absHumidity = RHtoAbsolute(humidity, temperature);

  //Convert the double type humidity to a fixed point 8.8bit number
  uint16_t sensHumidity = doubleToFixedPoint(absHumidity);

  //Set the humidity compensation on the SGP30 to the measured value
  //If no humidity sensor attached, sensHumidity should be 0 and sensor will use default
  mySensor.setHumidity(sensHumidity);
  Serial.print("Absolute Humidity compensation value set to: ");
  Serial.print(absHumidity);
  Serial.println("g/m^3 ");
  delay(100);