Adafruit_CircuitPython_SGP30 library question
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit_CircuitPython_SGP30 library question

by ductsoup on Thu Oct 11, 2018 7:28 am

This is a really interesting little sensor. Question is, will Adafruit be implementing humidity compensation any time soon? I see somebody did a pull request to add that feature to the Arduino version of the library. If it's in the works I won't bother doing the same for the Python version.

IMG_3860.JPG (87.05 KiB) Viewed 39 times

Posts: 196
Joined: Sun Oct 12, 2014 7:26 am

Re: Adafruit_CircuitPython_SGP30 library question

by jerryn on Thu Oct 11, 2018 7:44 am

The github repo for the sgp30 is here https://github.com/adafruit/Adafruit_CircuitPython_SGP30 I don't see any mention of adding humidity compensation so I would suggest adding an issue then submitting a PR with the implementation. Also you may want to check out the Discord channel for #circuitpython at http://adafru.it/discord
You'll find folks there who'll be happy to help if you have any questions.

Posts: 752
Joined: Sat Sep 14, 2013 9:05 am

Re: Adafruit_CircuitPython_SGP30 library question

by ductsoup on Sun Oct 14, 2018 10:08 am

Can someone sanity check me on what appears to be a typo on the datasheet? Unless I'm not getting something according to the description I'm thinking 16.50 = 0x1080.
0x0B92 = 11.57 g/m3
0x0F80 = 16.50 g/m3

Extending the base class is also a way to go. Here's a modified version of the SGP30 example that seems to work well.

Code: Select all | TOGGLE FULL SIZE
""" Example SGP30 humidity compensation using a BME680 on a Raspberry Pi """

import time
import board
import busio
import bme680
import adafruit_sgp30

from math import *
class sgp30hc(adafruit_sgp30.Adafruit_SGP30):
    def set_humidity(self, t, RH):
        """Set the humitity compensation using temperature (C) and RH (0-100)"""
        AH = 216.7 * (((RH / 100) * 6.112 * exp((17.62 * t) / (243.12 + t))) / (273.15 + t))   
        if AH > 256:
            raise RuntimeError('Invalid humidity compensation')
        buffer = []
        arr = [int(AH), floor(256 * (AH - int(AH)))]
        buffer += arr
        print("**** Temperature = %3.2f (C), RH = %3.2f (%%), AH = %3.2f (g/m^3), HC = 0x%02x%02x" % (t, RH, AH, arr[0], arr[1]))
        return self._run_profile(["set_humidity", [0x20, 0x61] + buffer, 0, 0.01])

    def disable_humidity(self):
        buffer = []
        arr = [0x00, 0x00]
        buffer += arr
        return self._run_profile(["disable_humidity", [0x20, 0x61] + buffer, 0, 0.01])

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

""" Initialize the BME680 """
s1 = bme680.BME680(i2c_addr=0x77)

""" Initialize the SGP30 """
s2 = sgp30hc(i2c)
print("SGP30 serial #", [hex(i) for i in s2.serial])
s2.set_iaq_baseline(0x88d7, 0x8a63)

elapsed_sec = 0

while True:
    print("co2eq = %d ppm \t tvoc = %d ppb" % (s2.co2eq, s2.tvoc))
    elapsed_sec += 1
    if elapsed_sec > 10:
        elapsed_sec = 0
        print("**** Baseline values: co2eq = 0x%x, tvoc = 0x%x"
              % (s2.baseline_co2eq, s2.baseline_tvoc))
        if s1.get_sensor_data():
            s2.set_humidity(s1.data.temperature, s1.data.humidity)

Posts: 196
Joined: Sun Oct 12, 2014 7:26 am

Please be positive and constructive with your questions and comments.