CircuitPython Wiring & Test

Installing Library

To use the SGP30 you'll need to install the Adafruit CircuitPython SGP30 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_sgp30.mpy
  • adafruit_bus_device

You can also download the adafruit_sgp.mpy from its releases page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_sgp30.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.


To demonstrate the usage of the sensor we'll initialize it and read the eCO2 and TVOC data and print it to the REPL

Save this example sketch as on your CircuitPython board:

""" Example for using the SGP30 with CircuitPython and the Adafruit library"""

import time
import board
import busio
import adafruit_sgp30

i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)

# Create library object on our I2C port
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)

print("SGP30 serial #", [hex(i) for i in sgp30.serial])

sgp30.set_iaq_baseline(0x8973, 0x8aae)

elapsed_sec = 0

while True:
    co2eq, tvoc = sgp30.iaq_measure()
    print("CO2eq = %d ppm \t TVOC = %d ppb" % (co2eq, tvoc))
    elapsed_sec += 1
    if elapsed_sec > 10:
        elapsed_sec = 0
        co2eq_base, tvoc_base = sgp30.get_iaq_baseline()
        print("**** Baseline values: CO2eq = 0x%x, TVOC = 0x%x" % (co2eq_base, tvoc_base))

In the REPL you'll see the serial number printed out: [0x0, 0x64, 0xb878] in this case. This is a unique 48-bit number burned into each chip. Since you may want to do per-chip calibration, this can help keep your calibration detail separate

The first 10-20 readings will always be eCO2 400 ppm  TVOC 0 ppb . That's because the sensor is warming up, so it will have 'null' readings.

After a few seconds, you will see the TVOC and eCO2 readings fluctuate

Every minute or so you'll also get a Baseline value printed out. More about that later!

You can take a bit of alcohol on a swap and swipe it nearby to get the readings to spike

That's it! The sensor pretty much only does that - all the calculations for the TVOC and eCO2 are done within the sensor itself, no other data is exposed beyond the 'baseline' values

Baseline Set & Get

All VOC/gas sensors use the same underlying technology: a tin oxide element that, when exposed to organic compounds, changes resistance. The 'problem' with these sensors is that the baseline changes, often with humidity, temperature, and other non-gas-related-events. To keep the values coming out reasonable, you'll need to calibrate the sensor. To make that easy, SGP lets you query the 'baseline calibration readings' from the sensor with code like this:

co2eq_base, tvoc_base = sgp30.get_iaq_baseline()

This will grab the two 16-bit sensor calibration words and place them in the variables so-named.

You should store these in EEPROM, FLASH or hard-coded. Then, next time you start up the sensor, you can pre-fill the calibration words with sgp30.set_iaq_baseline(co2eq_base, tvoc_base)

Last updated on 2018-01-12 at 01.47.44 PM Published on 2018-01-17 at 05.30.16 PM