Qwiic_SGP40_Py¶
Python module for the SparkFun Qwiic Air Quality Sensor - SGP40
This python package is a port of the existing SparkFun SGP40 Arduino Library
This package can be used in conjunction with the overall SparkFun qwiic Python Package
New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.
Supported Platforms¶
The Qwiic SGP40 Python package currently supports the following platforms:
Dependencies¶
This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py
Documentation¶
The SparkFun Qwiic Sgp40 module documentation is hosted at ReadTheDocs
Installation¶
PyPi Installation¶
This repository is hosted on PyPi as the sparkfun-qwiic-sgp40 package. On systems that support PyPi installation via pip, this library is installed using the following commands
For all users (note: the user must have sudo privileges):
sudo pip install sparkfun-qwiic-sgp40
For the current user:
pip install sparkfun-qwiic-sgp40
To install, make sure the setuptools package is installed on the system.
Direct installation at the command line:
python setup.py install
To build a package for use with pip:
python setup.py sdist
A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.
cd dist
pip install sparkfun-qwiic-sgp40-<version>.tar.gz
Example Use¶
See the examples directory for more detailed use examples.
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)
Table of Contents¶
API Reference¶
qwiic_sgp40¶
Python module for the SparkFun Air Quality Sensor - SGP40 (Qwiic).
This package is a port of the existing [SparkFun SGP40 Arduino Library](https://github.com/sparkfun/SparkFun_SGP40_Arduino_Library) and is heavily based on the driver written by [DFRobot](https://github.com/DFRobot/DFRobot_SGP40/tree/master/Python/raspberrypi).
This package can be used in conjunction with the overall [SparkFun Qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
New to qwiic? Take a look at the entire [SparkFun Qwiic Ecosystem](https://www.sparkfun.com/qwiic).
- class qwiic_sgp40.QwiicSGP40(address=None, i2c_driver=None)[source]¶
- Parameters
address – The I2C address to use for the device. If not provided, the default address is used.
i2c_driver – An existing i2c driver object. If not provided a a driver object is created.
- Returns
The GPIO device object.
- Return type
Object
- begin(warm_up_time=10)[source]¶
Initialize the operation of the Qwiic SGP40 and wait through warm- up time. Run is_connected() and measure_test().
- Returns
Returns true if the intialization was successful, false otherwise.
- Return type
bool
- get_VOC_index(_QwiicSGP40__relative_humidity=50, _QwiicSGP40__temperature_c=25)[source]¶
Get VOC index
- Parameters
__relative_humidity – float relative humidity between 0 and 100%.
__temperature_c – float temperature in celcius between -45 and 130 degrees.
- Returns
VOC index
- Return type
int
- heater_off()[source]¶
Turns the hotplate off and puts sensor in idle mode.
- Return type
void - returns nothing
- is_connected()[source]¶
Determine if a Qwiic SGP40 device is connected to the system.
- Returns
True if the device is connected, false otherwise.
- Return type
bool
- measure_raw(_QwiicSGP40__relative_humidity=50, _QwiicSGP40__temperature_c=25)[source]¶
Returns the raw data. See the SGP40 datasheet for more info.
- Parameters
SRAW_ticks – variable to assign raw measurement to
__relative_humidity – float relative humidity between 0 and 100%.
__temperature_c – float temperature in celcius between -45 and 130 degrees.
- Returns
0 if CRC checks out, -1 otherwise
- Return type
int
Example One - Get VOC Index¶
1# !/usr/bin/env python
2# ----------------------------------------------------------------------
3# qwiic_sgp40_ex1.py
4#
5# Simple example to get VOC index
6# ----------------------------------------------------------------------
7#
8# Written by Priyanka Makin @ SparkFun Electronics, June 2021
9#
10# This python library supports the SparkFun Electronics qwiic sensor/
11# board ecosystem on a Raspberry Pi (and compatable) single board
12# computers.
13#
14# More information on qwiic is at https://www.sparkfun.com/qwiic
15#
16# Do you like this library? Help support SparkFun by buying a board!
17#
18# ======================================================================
19# Copyright (c) 2021 SparkFun Electronics
20#
21# Permission is hereby granted, free of charge, to any person obtaining
22# a copy of this software and associated documentation files (the
23# "Software"), to deal in the Software without restriction, including
24# without limitation the rights to use, copy, modify, merge, publish,
25# distribute, sublicense, and/or sell copies of the Software, and to
26# permit persons to whom the Software is furnished to do so, subject to
27# the following conditions:
28#
29# The above copyright notice and this permission notice shall be
30# included in all copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
35# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
36# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
37# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
38# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39#=======================================================================
40# Example 1
41
42from __future__ import print_function
43import qwiic_sgp40
44import time
45import sys
46
47def run_example():
48
49 print("\nSparkFun Qwiic Air Quality Sensor - SGP40, Example 1\n")
50 my_sgp40 = qwiic_sgp40.QwiicSGP40()
51
52 if my_sgp40.begin() != 0:
53 print("\nThe Qwiic SGP40 isn't connected to the system. Please check your connection", \
54 file=sys.stderr)
55 return
56
57 print("\nSGP40 ready!")
58
59 while True:
60
61 print("\nVOC Index is: " + str(my_sgp40.get_VOC_index()))
62
63 time.sleep(1)
64
65if __name__ == '__main__':
66 try:
67 run_example()
68 except (KeyboardInterrupt, SystemExit) as exErr:
69 print("\nEnding Example 1")
70 sys.exit(0)