Skip to content

VOC Index #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Usage Example
print("")
sleep(1)

For humidity compensated raw gas readings, we'll need a secondary sensor such as the bme280
For humidity compensated raw gas and voc index readings, we'll need a secondary sensor such as the bme280

.. code-block:: python3

Expand All @@ -90,12 +90,20 @@ For humidity compensated raw gas readings, we'll need a secondary sensor such as
while True:
temperature = bme280.temperature
humidity = bme280.relative_humidity
compensated_raw_gas = sgp.measure_raw(temperature = temperature, relative_humidity = humidity)

# For compensated raw gas readings
compensated_raw_gas = sgp.raw(temperature = temperature, relative_humidity = humidity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgp.raw doesn't take in any inputs, you'll need measure_raw in it's place


# For Compensated voc index readings
Copy link
Contributor

@KeithTheEE KeithTheEE Aug 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might also need to be a time.sleep(1) here, or we can comment out one of the examples (in this case compensated_raw_gas would be better to comment out) because I think the sensor needs full second between readings, and both the measure_raw and measure_index make a call to the sensor over i2c.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, datasheet says 1s typical, .5s min

voc_index = sgp.measure_index(temperature = temperature, relative_humidity = humidity)

print(compensated_raw_gas)
print(voc_index)
print("")
time.sleep(1)


It may take several minutes for the VOC index to start changing as it calibrates the baseline readings.

Contributing
============
Expand Down
35 changes: 31 additions & 4 deletions adafruit_sgp40.py → adafruit_sgp40/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"""
from time import sleep
from struct import unpack_from
import adafruit_bus_device.i2c_device as i2c_device
from adafruit_bus_device import i2c_device

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
Expand Down Expand Up @@ -111,6 +111,7 @@ def __init__(self, i2c, address=0x59):
self.i2c_device = i2c_device.I2CDevice(i2c, address)
self._command_buffer = bytearray(2)
self._measure_command = _READ_CMD
self._voc_algorithm = None

self.initialize()

Expand All @@ -133,8 +134,6 @@ def initialize(self):

raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))

# VocAlgorithm_init(&voc_algorithm_params)

# Self Test
self._command_buffer[0] = 0x28
self._command_buffer[1] = 0x0E
Expand Down Expand Up @@ -226,6 +225,34 @@ def measure_raw(self, temperature=25, relative_humidity=50):
self._measure_command = bytearray(_cmd)
return self.raw

def measure_index(self, temperature=25, relative_humidity=50):
"""Measure VOC index after humidity compensation
:param float temperature: The temperature in degrees Celsius, defaults to :const:`25`
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
:note VOC index can indicate the quality of the air directly.
The larger the value, the worse the air quality.
:note 0-100, no need to ventilate, purify
:note 100-200, no need to ventilate, purify
:note 200-400, ventilate, purify
:note 00-500, ventilate, purify intensely
:return int The VOC index measured, ranged from 0 to 500
"""
# import/setup algorithm only on use of index
# pylint: disable=import-outside-toplevel
from adafruit_sgp40.voc_algorithm import (
VOCAlgorithm,
)

if self._voc_algorithm is None:
self._voc_algorithm = VOCAlgorithm()
self._voc_algorithm.vocalgorithm_init()

raw = self.measure_raw(temperature, relative_humidity)
if raw < 0:
return -1
voc_index = self._voc_algorithm.vocalgorithm_process(raw)
return voc_index

def _read_word_from_command(
self,
delay_ms=10,
Expand Down Expand Up @@ -288,7 +315,7 @@ def _generate_crc(crc_buffer):
if crc & 0x80:
crc = (
crc << 1
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
else:
crc = crc << 1
return crc & 0xFF # Returns only bottom 8 bits
Loading