Air Quality Sensor - SGP40 (Qwiic) Hookup Guide

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

Python Example

The Qwiic SGP40 Python package includes one example to demonstrate how to get VOC Index data from the sensor. Open the example from the Qwiic SGP40 Py location or copy the code into your preferred Python interpreter.

Example 1 - VOC Index

This simple example initializes the SGP40 on the I2C bus and uses the get_VOC_index() function to, well, get the VOC index from the sensor. Take note, polling the VOC index should occur at 1Hz (once per second) for accurate results.

language:python
from __future__ import print_function
import qwiic_sgp40
import time
import sys

def run_example():

    print("\nSparkFun Qwiic Air Quality Sensor - SGP40, Example 1\n")
    my_sgp40 = qwiic_sgp40.QwiicSGP40()

    if my_sgp40.begin() != 0:
        print("\nThe Qwiic SGP40 isn't connected to the system. Please check your connection", \
            file=sys.stderr)
        return

    print("\nSGP40 ready!")

    while True:

        print("\nVOC Index is: " + str(my_sgp40.get_VOC_index()))

        time.sleep(1)

if __name__ == '__main__':
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

Run the example and let the sensor warm up for ~60 seconds and the readings should stabilize to around 100. Try creating VOC events by putting something like isopropyl alcohol or a permanent marker near the sensor and then pull it away. You should see the VOC index rise significantly and then return back to the "norm" of ~100 after a minute or so. For the best results, let the SGP40 run for 24hrs to generate a "history" of the average VOC gas concentration in the room.