Skip to content

Commit 7b80869

Browse files
committed
added index algorithm
1 parent c627e60 commit 7b80869

File tree

4 files changed

+574
-37
lines changed

4 files changed

+574
-37
lines changed

README.rst

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
Introduction
22
============
33

4-
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-sgp40/badge/?version=latest
5-
:target: https://circuitpython.readthedocs.io/projects/sgp40/en/latest/
6-
:alt: Documentation Status
4+
CircuitPython library for the Adafruit SGP40 Air Quality Sensor / VOC Index Sensor Breakouts.
5+
This fork adds DFRobots VOC Index algorithm to adafruits CircuitPython library.
76

8-
.. image:: https://img.shields.io/discord/327254708534116352.svg
9-
:target: https://adafru.it/discord
10-
:alt: Discord
11-
12-
.. image:: https://github.com/adafruit/Adafruit_CircuitPython_SGP40/workflows/Build%20CI/badge.svg
13-
:target: https://github.com/adafruit/Adafruit_CircuitPython_SGP40/actions
14-
:alt: Build Status
15-
16-
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
17-
:target: https://github.com/psf/black
18-
:alt: Code Style: Black
19-
20-
CircuitPython library for the Adafruit SGP40 Air Quality Sensor / VOC Index Sensor Breakouts
7+
`Original Library <https://github.com/adafruit/Adafruit_CircuitPython_SGP40>`_
8+
`DFRobot Algorithm <https://github.com/DFRobot/DFRobot_SGP40>`_
219

2210

2311
Dependencies
@@ -60,21 +48,7 @@ To install in a virtual environment in your current project:
6048
Usage Example
6149
=============
6250

63-
.. code-block:: python3
64-
65-
import time
66-
import board
67-
import adafruit_sgp40
68-
69-
i2c = board.I2C() # uses board.SCL and board.SDA
70-
sgp = adafruit_sgp40(i2c)
71-
72-
while True:
73-
print("Measurement: ", sgp.raw)
74-
print("")
75-
sleep(1)
76-
77-
For humidity compensated raw gas readings, we'll need a secondary sensor such as the bme280
51+
For humidity compensated raw gas and voc index readings, we'll need a secondary sensor such as the bme280
7852

7953
.. code-block:: python3
8054
@@ -90,12 +64,20 @@ For humidity compensated raw gas readings, we'll need a secondary sensor such as
9064
while True:
9165
temperature = bme280.temperature
9266
humidity = bme280.relative_humidity
93-
compensated_raw_gas = sgp.measure_raw(temperature = temperature, relative_humidity = humidity)
67+
68+
# Compensated raw gas reading
69+
compensated_raw_gas = sgp.measure_index(temperature = temperature, relative_humidity = humidity)
70+
71+
# Compensated voc index reading
72+
voc_index = sgp.measure_raw(temperature = temperature, relative_humidity = humidity)
73+
9474
print(compensated_raw_gas)
75+
print(voc_index)
9576
print("")
9677
time.sleep(1)
9778
9879
80+
It may take several minutes for the VOC index to start changing as it calibrates the baseline readings.
9981

10082
Contributing
10183
============

adafruit_sgp40.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from time import sleep
3333
from struct import unpack_from
3434
import adafruit_bus_device.i2c_device as i2c_device
35+
from voc_index_algorithm import DFRobot_VOCAlgorithm
3536

3637
__version__ = "0.0.0-auto.0"
3738
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
@@ -118,6 +119,7 @@ def __init__(self, i2c, address=0x59):
118119
self.i2c_device = i2c_device.I2CDevice(i2c, address)
119120
self._command_buffer = bytearray(2)
120121
self._measure_command = _READ_CMD
122+
self._voc_algorithm = DFRobot_VOCAlgorithm()
121123

122124
self.initialize()
123125

@@ -138,9 +140,10 @@ def initialize(self):
138140
featureset = self._read_word_from_command()
139141
if featureset[0] != 0x3220:
140142

141-
raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))
143+
raise RuntimeError("Feature set does not match: %s" %
144+
hex(featureset[0]))
142145

143-
# VocAlgorithm_init(&voc_algorithm_params)
146+
self._voc_algorithm.vocalgorithm_init()
144147

145148
# Self Test
146149
self._command_buffer[0] = 0x28
@@ -232,6 +235,26 @@ def measure_raw(self, temperature=25, relative_humidity=50):
232235
self._measure_command = bytearray(_cmd)
233236
return self.raw
234237

238+
def measure_index(self, temperature=25, relative_humidity=50):
239+
""" Measure VOC index after humidity compensation
240+
:param float temperature: The temperature in degrees Celsius, defaults
241+
to :const:`25`
242+
:param float relative_humidity: The relative humidity in percentage, defaults
243+
to :const:`50`
244+
:note VOC index can indicate the quality of the air directly. The larger the value, the worse the air quality.
245+
:note 0-100,no need to ventilate, purify
246+
:note 100-200,no need to ventilate, purify
247+
:note 200-400,ventilate, purify
248+
:note 00-500,ventilate, purify intensely
249+
:return int The VOC index measured, ranged from 0 to 500
250+
"""
251+
raw = self.measure_raw(temperature, relative_humidity)
252+
if raw < 0:
253+
return -1
254+
else:
255+
vocIndex = self._voc_algorithm.vocalgorithm_process(raw)
256+
return vocIndex
257+
235258
def _read_word_from_command(
236259
self,
237260
delay_ms=10,
@@ -264,9 +287,9 @@ def _read_word_from_command(
264287
i2c.readinto(replybuffer, end=replylen)
265288

266289
for i in range(0, replylen, 3):
267-
if not self._check_crc8(replybuffer[i : i + 2], replybuffer[i + 2]):
290+
if not self._check_crc8(replybuffer[i: i + 2], replybuffer[i + 2]):
268291
raise RuntimeError("CRC check failed while reading data")
269-
readdata_buffer.append(unpack_from(">H", replybuffer[i : i + 2])[0])
292+
readdata_buffer.append(unpack_from(">H", replybuffer[i: i + 2])[0])
270293

271294
return readdata_buffer
272295

@@ -294,7 +317,7 @@ def _generate_crc(crc_buffer):
294317
if crc & 0x80:
295318
crc = (
296319
crc << 1
297-
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
320+
) ^ 0x31 # 0x31 is the Seed for SGP40's CRC polynomial
298321
else:
299322
crc = crc << 1
300323
return crc & 0xFF # Returns only bottom 8 bits

examples/sgp40_indextest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import time
2+
import board
3+
import adafruit_sgp40
4+
import adafruit_bme280
5+
6+
# Boards i2c bus
7+
i2c = board.I2C() # uses board.SCL and board.SDA
8+
sgp = adafruit_sgp40.SGP40(i2c)
9+
10+
# Humidity sensor for compensated Readings
11+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
12+
13+
while True:
14+
temperature = bme280.temperature
15+
humidity = bme280.relative_humidity
16+
17+
# Compensated voc index reading
18+
voc_index = sgp.measure_raw(
19+
temperature=temperature, relative_humidity=humidity)
20+
21+
print(voc_index)
22+
print("")
23+
time.sleep(1)

0 commit comments

Comments
 (0)