Merge pull request #8520 from peteakalad/peteakalad-SGP30_Sensor_Fixes

SGP30 Sensor Fixes #8519
This commit is contained in:
Theo Arends 2020-05-24 11:19:57 +02:00 committed by GitHub
commit 43b999600e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 456 additions and 435 deletions

View File

@ -1,46 +0,0 @@
Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:
- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use
the forums at http://forums.adafruit.com to ask questions and troubleshoot why
something isn't working as expected. In many cases the problem is a common issue
that you will more quickly receive help from the forum community. GitHub issues
are meant for known defects in the code. If you don't know if there is a defect
in the code then start with troubleshooting on the forum first.
- **If following a tutorial or guide be sure you didn't miss a step.** Carefully
check all of the steps and commands to run have been followed. Consult the
forum if you're unsure or have questions about steps in a guide/tutorial.
- **For Arduino projects check these very common issues to ensure they don't apply**:
- For uploading sketches or communicating with the board make sure you're using
a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes
very hard to tell the difference between a data and charge cable! Try using the
cable with other devices or swapping to another cable to confirm it is not
the problem.
- **Be sure you are supplying adequate power to the board.** Check the specs of
your board and plug in an external power supply. In many cases just
plugging a board into your computer is not enough to power it and other
peripherals.
- **Double check all soldering joints and connections.** Flakey connections
cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints.
- **Ensure you are using an official Arduino or Adafruit board.** We can't
guarantee a clone board will have the same functionality and work as expected
with this code and don't support them.
If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:
- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE**
- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO
VERSION HERE**
- List the steps to reproduce the problem below (if possible attach a sketch or
copy the sketch code in too): **LIST REPRO STEPS BELOW**

View File

@ -1,26 +0,0 @@
Thank you for creating a pull request to contribute to Adafruit's GitHub code!
Before you open the request please review the following guidelines and tips to
help it be more easily integrated:
- **Describe the scope of your change--i.e. what the change does and what parts
of the code were modified.** This will help us understand any risks of integrating
the code.
- **Describe any known limitations with your change.** For example if the change
doesn't apply to a supported platform of the library please mention it.
- **Please run any tests or examples that can exercise your modified code.** We
strive to not break users of the code and running tests/examples helps with this
process.
Thank you again for contributing! We will try to test and integrate the change
as soon as we can, but be aware we have many GitHub repositories to manage and
can't immediately respond to every request. There is no need to bump or check in
on a pull request (it will clutter the discussion of the request).
Also don't be worried if the request is closed or not integrated--sometimes the
priorities of Adafruit's GitHub code (education, ease of use) might not match the
priorities of the pull request. Don't fret, the open source community thrives on
forks and GitHub makes it easy to keep your changes in a forked repo.
After reviewing the guidelines above you can delete this text from the pull request.

View File

@ -1,4 +0,0 @@
*~
Doxyfile*
doxygen_sqlite3.db
html

View File

@ -1,267 +0,0 @@
/*!
* @file Adafruit_SGP30.cpp
*
* @mainpage Adafruit SGP30 gas sensor driver
*
* @section intro_sec Introduction
*
* This is the documentation for Adafruit's SGP30 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit SGP30 breakout: http://www.adafruit.com/products/3709
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
*
* @section author Author
* Written by Ladyada for Adafruit Industries.
*
* @section license License
* BSD license, all text here must be included in any redistribution.
*
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Adafruit_SGP30.h"
//#define I2C_DEBUG
/**************************************************************************/
/*!
@brief Instantiates a new SGP30 class
*/
/**************************************************************************/
Adafruit_SGP30::Adafruit_SGP30() {
}
/**************************************************************************/
/*!
@brief Setups the hardware and detects a valid SGP30. Initializes I2C
then reads the serialnumber and checks that we are talking to an SGP30
@param theWire Optional pointer to I2C interface, otherwise use Wire
@returns True if SGP30 found on I2C, False if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::begin(TwoWire *theWire) {
_i2caddr = SGP30_I2CADDR_DEFAULT;
if (theWire == NULL) {
_i2c = &Wire;
} else {
_i2c = theWire;
}
// assume i2c initialized already to avoid resetting clock stretching
// _i2c->begin();
uint8_t command[2];
command[0] = 0x36;
command[1] = 0x82;
if (! readWordFromCommand(command, 2, 10, serialnumber, 3))
return false;
uint16_t featureset;
command[0] = 0x20;
command[1] = 0x2F;
if (! readWordFromCommand(command, 2, 10, &featureset, 1))
return false;
//Serial.print("Featureset 0x"); Serial.println(featureset, HEX);
if (featureset != SGP30_FEATURESET)
return false;
if (! IAQinit())
return false;
return true;
}
/**************************************************************************/
/*!
@brief Commands the sensor to begin the IAQ algorithm. Must be called after startup.
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::IAQinit(void) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x03;
return readWordFromCommand(command, 2, 10);
}
/**************************************************************************/
/*!
@brief Commands the sensor to take a single eCO2/VOC measurement. Places results in {@link TVOC} and {@link eCO2}
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::IAQmeasure(void) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x08;
uint16_t reply[2];
if (! readWordFromCommand(command, 2, 12, reply, 2))
return false;
TVOC = reply[1];
eCO2 = reply[0];
return true;
}
/**************************************************************************/
/*!
@brief Request baseline calibration values for both CO2 and TVOC IAQ calculations. Places results in parameter memory locaitons.
@param eco2_base A pointer to a uint16_t which we will save the calibration value to
@param tvoc_base A pointer to a uint16_t which we will save the calibration value to
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x15;
uint16_t reply[2];
if (! readWordFromCommand(command, 2, 10, reply, 2))
return false;
*eco2_base = reply[0];
*tvoc_base = reply[1];
return true;
}
/**************************************************************************/
/*!
@brief Assign baseline calibration values for both CO2 and TVOC IAQ calculations.
@param eco2_base A uint16_t which we will save the calibration value from
@param tvoc_base A uint16_t which we will save the calibration value from
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) {
uint8_t command[8];
command[0] = 0x20;
command[1] = 0x1e;
command[2] = tvoc_base >> 8;
command[3] = tvoc_base & 0xFF;
command[4] = generateCRC(command+2, 2);
command[5] = eco2_base >> 8;
command[6] = eco2_base & 0xFF;
command[7] = generateCRC(command+5, 2);
return readWordFromCommand(command, 8, 10);
}
/**************************************************************************/
/*!
@brief Set the absolute humidity value [mg/m^3] for compensation to increase precision of TVOC and eCO2.
@param absolute_humidity A uint32_t [mg/m^3] which we will be used for compensation. If the absolute humidity is set to zero, humidity compensation will be disabled.
@returns True if command completed successfully, false if something went wrong!
*/
/**************************************************************************/
boolean Adafruit_SGP30::setHumidity(uint32_t absolute_humidity) {
if (absolute_humidity > 256000) {
return false;
}
uint16_t ah_scaled = (uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
uint8_t command[5];
command[0] = 0x20;
command[1] = 0x61;
command[2] = ah_scaled >> 8;
command[3] = ah_scaled & 0xFF;
command[4] = generateCRC(command+2, 2);
return readWordFromCommand(command, 5, 10);
}
/**************************************************************************/
/*!
@brief I2C low level interfacing
*/
/**************************************************************************/
boolean Adafruit_SGP30::readWordFromCommand(uint8_t command[], uint8_t commandLength, uint16_t delayms, uint16_t *readdata, uint8_t readlen)
{
uint8_t data;
_i2c->beginTransmission(_i2caddr);
#ifdef I2C_DEBUG
Serial.print("\t\t-> ");
#endif
for (uint8_t i=0; i<commandLength; i++) {
_i2c->write(command[i]);
#ifdef I2C_DEBUG
Serial.print("0x"); Serial.print(command[i], HEX); Serial.print(", ");
#endif
}
#ifdef I2C_DEBUG
Serial.println();
#endif
_i2c->endTransmission();
delay(delayms);
if (readlen == 0)
return true;
uint8_t replylen = readlen * (SGP30_WORD_LEN +1);
if (_i2c->requestFrom(_i2caddr, replylen) != replylen)
return false;
uint8_t replybuffer[replylen];
#ifdef I2C_DEBUG
Serial.print("\t\t<- ");
#endif
for (uint8_t i=0; i<replylen; i++) {
replybuffer[i] = _i2c->read();
#ifdef I2C_DEBUG
Serial.print("0x"); Serial.print(replybuffer[i], HEX); Serial.print(", ");
#endif
}
#ifdef I2C_DEBUG
Serial.println();
#endif
for (uint8_t i=0; i<readlen; i++) {
uint8_t crc = generateCRC(replybuffer+i*3, 2);
#ifdef I2C_DEBUG
Serial.print("\t\tCRC calced: 0x"); Serial.print(crc, HEX);
Serial.print(" vs. 0x"); Serial.println(replybuffer[i * 3 + 2], HEX);
#endif
if (crc != replybuffer[i * 3 + 2])
return false;
// success! store it
readdata[i] = replybuffer[i*3];
readdata[i] <<= 8;
readdata[i] |= replybuffer[i*3 + 1];
#ifdef I2C_DEBUG
Serial.print("\t\tRead: 0x"); Serial.println(readdata[i], HEX);
#endif
}
return true;
}
uint8_t Adafruit_SGP30::generateCRC(uint8_t *data, uint8_t datalen) {
// calculates 8-Bit checksum with given polynomial
uint8_t crc = SGP30_CRC8_INIT;
for (uint8_t i=0; i<datalen; i++) {
crc ^= data[i];
for (uint8_t b=0; b<8; b++) {
if (crc & 0x80)
crc = (crc << 1) ^ SGP30_CRC8_POLYNOMIAL;
else
crc <<= 1;
}
}
return crc;
}

View File

@ -1,69 +0,0 @@
/*!
* @file Adafruit_SGP30.h
*
* This is the documentation for Adafruit's SGP30 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit SGP30 breakout: http://www.adafruit.com/products/3709
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Ladyada for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Arduino.h"
#include <Wire.h>
// the i2c address
#define SGP30_I2CADDR_DEFAULT 0x58 ///< SGP30 has only one I2C address
// commands and constants
#define SGP30_FEATURESET 0x0020 ///< The required set for this library
#define SGP30_CRC8_POLYNOMIAL 0x31 ///< Seed for SGP30's CRC polynomial
#define SGP30_CRC8_INIT 0xFF ///< Init value for CRC
#define SGP30_WORD_LEN 2 ///< 2 bytes per word
/**************************************************************************/
/*! Class that stores state and functions for interacting with SGP30 Gas Sensor */
/**************************************************************************/
class Adafruit_SGP30 {
public:
Adafruit_SGP30();
boolean begin(TwoWire *theWire = NULL);
boolean IAQinit(void);
boolean IAQmeasure(void);
boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base);
boolean setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base);
boolean setHumidity(uint32_t absolute_humidity);
/**
* The last measurement of the IAQ-calculated Total Volatile Organic Compounds in ppb. This value is set when you call {@link IAQmeasure()}
*/
uint16_t TVOC;
/**
* The last measurement of the IAQ-calculated equivalent CO2 in ppm. This value is set when you call {@link IAQmeasure()}
*/
uint16_t eCO2;
/**
* The 48-bit serial number, this value is set when you call {@link begin()}
*/
uint16_t serialnumber[3];
private:
TwoWire *_i2c;
uint8_t _i2caddr;
void write(uint8_t address, uint8_t *data, uint8_t n);
void read(uint8_t address, uint8_t *data, uint8_t n);
boolean readWordFromCommand(uint8_t command[], uint8_t commandLength, uint16_t delay, uint16_t *readdata = NULL, uint8_t readlen = 0);
uint8_t generateCRC(uint8_t data[], uint8_t datalen);
};

View File

@ -1,18 +0,0 @@
Adafruit_SGP30
================
This is the Adafruit SGP30 Gas / Air Quality I2C sensor library
Tested and works great with the Aadafruit SGP30 Breakout Board
* http://www.adafruit.com/products/3709
This chip uses I2C to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution

View File

@ -0,0 +1,306 @@
/*!
* @file Adafruit_SGP30.cpp
*
* @mainpage Adafruit SGP30 gas sensor driver
*
* @section intro_sec Introduction
*
* This is the documentation for Adafruit's SGP30 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit SGP30 breakout: http://www.adafruit.com/products/3709
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
*
* @section author Author
* Written by Ladyada for Adafruit Industries.
*
* @section license License
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Arduino.h"
#include "Adafruit_SGP30.h"
//#define I2C_DEBUG
/*!
* @brief Instantiates a new SGP30 class
*/
Adafruit_SGP30::Adafruit_SGP30() {}
/*!
* @brief Setups the hardware and detects a valid SGP30. Initializes I2C
* then reads the serialnumber and checks that we are talking to an
* SGP30
* @param theWire
* Optional pointer to I2C interface, otherwise use Wire
* @param initSensor
* Optional pointer to prevent IAQinit to be called. Used for Deep
* Sleep.
* @return True if SGP30 found on I2C, False if something went wrong!
*/
boolean Adafruit_SGP30::begin(TwoWire *theWire, boolean initSensor) {
_i2caddr = SGP30_I2CADDR_DEFAULT;
_i2c = theWire;
_i2c->begin();
uint8_t command[2];
command[0] = 0x36;
command[1] = 0x82;
if (!readWordFromCommand(command, 2, 10, serialnumber, 3))
return false;
uint16_t featureset;
command[0] = 0x20;
command[1] = 0x2F;
if (!readWordFromCommand(command, 2, 10, &featureset, 1))
return false;
// Serial.print("Featureset 0x"); Serial.println(featureset, HEX);
if ((featureset & 0xF0) != SGP30_FEATURESET)
return false;
if (initSensor) {
if (!IAQinit())
return false;
}
return true;
}
/*!
* @brief Commands the sensor to perform a soft reset using the "General
* Call" mode. Take note that this is not sensor specific and all devices that
* support the General Call mode on the on the same I2C bus will perform this.
*
* @return True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::softReset(void) {
uint8_t command[2];
command[0] = 0x00;
command[1] = 0x06;
return readWordFromCommand(command, 2, 10);
}
/*!
* @brief Commands the sensor to begin the IAQ algorithm. Must be called
* after startup.
* @returns True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::IAQinit(void) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x03;
return readWordFromCommand(command, 2, 10);
}
/*!
* @brief Commands the sensor to take a single eCO2/VOC measurement. Places
* results in {@link TVOC} and {@link eCO2}
* @return True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::IAQmeasure(void) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x08;
uint16_t reply[2];
if (!readWordFromCommand(command, 2, 12, reply, 2))
return false;
TVOC = reply[1];
eCO2 = reply[0];
return true;
}
/*!
* @brief Commands the sensor to take a single H2/ethanol raw measurement.
* Places results in {@link rawH2} and {@link rawEthanol}
* @returns True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::IAQmeasureRaw(void) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x50;
uint16_t reply[2];
if (!readWordFromCommand(command, 2, 25, reply, 2))
return false;
rawEthanol = reply[1];
rawH2 = reply[0];
return true;
}
/*!
* @brief Request baseline calibration values for both CO2 and TVOC IAQ
* calculations. Places results in parameter memory locaitons.
* @param eco2_base
* A pointer to a uint16_t which we will save the calibration
* value to
* @param tvoc_base
* A pointer to a uint16_t which we will save the calibration value to
* @return True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::getIAQBaseline(uint16_t *eco2_base,
uint16_t *tvoc_base) {
uint8_t command[2];
command[0] = 0x20;
command[1] = 0x15;
uint16_t reply[2];
if (!readWordFromCommand(command, 2, 10, reply, 2))
return false;
*eco2_base = reply[0];
*tvoc_base = reply[1];
return true;
}
/*!
* @brief Assign baseline calibration values for both CO2 and TVOC IAQ
* calculations.
* @param eco2_base
* A uint16_t which we will save the calibration value from
* @param tvoc_base
* A uint16_t which we will save the calibration value from
* @return True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) {
uint8_t command[8];
command[0] = 0x20;
command[1] = 0x1e;
command[2] = tvoc_base >> 8;
command[3] = tvoc_base & 0xFF;
command[4] = generateCRC(command + 2, 2);
command[5] = eco2_base >> 8;
command[6] = eco2_base & 0xFF;
command[7] = generateCRC(command + 5, 2);
return readWordFromCommand(command, 8, 10);
}
/*!
* @brief Set the absolute humidity value [mg/m^3] for compensation to
* increase precision of TVOC and eCO2.
* @param absolute_humidity
* A uint32_t [mg/m^3] which we will be used for compensation.
* If the absolute humidity is set to zero, humidity compensation
* will be disabled.
* @return True if command completed successfully, false if something went
* wrong!
*/
boolean Adafruit_SGP30::setHumidity(uint32_t absolute_humidity) {
if (absolute_humidity > 256000) {
return false;
}
uint16_t ah_scaled =
(uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
uint8_t command[5];
command[0] = 0x20;
command[1] = 0x61;
command[2] = ah_scaled >> 8;
command[3] = ah_scaled & 0xFF;
command[4] = generateCRC(command + 2, 2);
return readWordFromCommand(command, 5, 10);
}
/*!
* @brief I2C low level interfacing
*/
boolean Adafruit_SGP30::readWordFromCommand(uint8_t command[],
uint8_t commandLength,
uint16_t delayms,
uint16_t *readdata,
uint8_t readlen) {
_i2c->beginTransmission(_i2caddr);
#ifdef I2C_DEBUG
Serial.print("\t\t-> ");
#endif
for (uint8_t i = 0; i < commandLength; i++) {
_i2c->write(command[i]);
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(command[i], HEX);
Serial.print(", ");
#endif
}
#ifdef I2C_DEBUG
Serial.println();
#endif
_i2c->endTransmission();
delay(delayms);
if (readlen == 0)
return true;
uint8_t replylen = readlen * (SGP30_WORD_LEN + 1);
if (_i2c->requestFrom(_i2caddr, replylen) != replylen)
return false;
uint8_t replybuffer[replylen];
#ifdef I2C_DEBUG
Serial.print("\t\t<- ");
#endif
for (uint8_t i = 0; i < replylen; i++) {
replybuffer[i] = _i2c->read();
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(replybuffer[i], HEX);
Serial.print(", ");
#endif
}
#ifdef I2C_DEBUG
Serial.println();
#endif
for (uint8_t i = 0; i < readlen; i++) {
uint8_t crc = generateCRC(replybuffer + i * 3, 2);
#ifdef I2C_DEBUG
Serial.print("\t\tCRC calced: 0x");
Serial.print(crc, HEX);
Serial.print(" vs. 0x");
Serial.println(replybuffer[i * 3 + 2], HEX);
#endif
if (crc != replybuffer[i * 3 + 2])
return false;
// success! store it
readdata[i] = replybuffer[i * 3];
readdata[i] <<= 8;
readdata[i] |= replybuffer[i * 3 + 1];
#ifdef I2C_DEBUG
Serial.print("\t\tRead: 0x");
Serial.println(readdata[i], HEX);
#endif
}
return true;
}
uint8_t Adafruit_SGP30::generateCRC(uint8_t *data, uint8_t datalen) {
// calculates 8-Bit checksum with given polynomial
uint8_t crc = SGP30_CRC8_INIT;
for (uint8_t i = 0; i < datalen; i++) {
crc ^= data[i];
for (uint8_t b = 0; b < 8; b++) {
if (crc & 0x80)
crc = (crc << 1) ^ SGP30_CRC8_POLYNOMIAL;
else
crc <<= 1;
}
}
return crc;
}

View File

@ -0,0 +1,80 @@
/*!
* @file Adafruit_SGP30.h
*
* This is the documentation for Adafruit's SGP30 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit SGP30 breakout: http://www.adafruit.com/products/3709
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Ladyada for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Arduino.h"
#include <Wire.h>
// the i2c address
#define SGP30_I2CADDR_DEFAULT 0x58 ///< SGP30 has only one I2C address
// commands and constants
#define SGP30_FEATURESET 0x0020 ///< The required set for this library
#define SGP30_CRC8_POLYNOMIAL 0x31 ///< Seed for SGP30's CRC polynomial
#define SGP30_CRC8_INIT 0xFF ///< Init value for CRC
#define SGP30_WORD_LEN 2 ///< 2 bytes per word
/*!
* @brief Class that stores state and functions for interacting with
* SGP30 Gas Sensor
*/
class Adafruit_SGP30 {
public:
Adafruit_SGP30();
boolean begin(TwoWire *theWire = &Wire, boolean initSensor = true);
boolean softReset();
boolean IAQinit();
boolean IAQmeasure();
boolean IAQmeasureRaw();
boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base);
boolean setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base);
boolean setHumidity(uint32_t absolute_humidity);
/** The last measurement of the IAQ-calculated Total Volatile Organic
* Compounds in ppb. This value is set when you call {@link IAQmeasure()} **/
uint16_t TVOC;
/** The last measurement of the IAQ-calculated equivalent CO2 in ppm. This
* value is set when you call {@link IAQmeasure()} **/
uint16_t eCO2;
/** The last measurement of the IAQ-calculated equivalent CO2 in ppm. This
* value is set when you call {@link IAQmeasureRaw()} **/
uint16_t rawH2;
/** The last measurement of the IAQ-calculated equivalent CO2 in ppm. This
* value is set when you call {@link IAQmeasureRaw()} **/
uint16_t rawEthanol;
/** The 48-bit serial number, this value is set when you call {@link begin()}
* **/
uint16_t serialnumber[3];
private:
TwoWire *_i2c;
uint8_t _i2caddr;
void write(uint8_t address, uint8_t *data, uint8_t n);
void read(uint8_t address, uint8_t *data, uint8_t n);
boolean readWordFromCommand(uint8_t command[], uint8_t commandLength,
uint16_t delay, uint16_t *readdata = NULL,
uint8_t readlen = 0);
uint8_t generateCRC(uint8_t data[], uint8_t datalen);
};

View File

@ -0,0 +1,54 @@
# Adafruit SGP30 Gas / Air Quality I2C sensor [[![Build Status](https://github.com/adafruit/Adafruit_SGP30/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_SGP30/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_SGP30/html/index.html)
<a href="https://www.adafruit.com/product/3709"><img src="assets/board.jpg?raw=true" width="500px"></a>
This is the Adafruit SGP30 Gas / Air Quality I2C sensor library
Tested and works great with the Aadafruit SGP30 Breakout Board
* http://www.adafruit.com/products/3709
This chip uses I2C to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
# Installation
To install, use the Arduino Library Manager and search for "Adafruit SGP30" and install the library.
## Dependencies
* [Adafruit ILI9341](https://github.com/adafruit/Adafruit_ILI9341)
* [Adafruit GFX Library](https://github.com/adafruit/Adafruit-GFX-Library)
# Contributing
Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/Adafruit_SGP30/blob/master/CODE_OF_CONDUCT.md>)
before contributing to help this project stay welcoming.
## Documentation and doxygen
Documentation is produced by doxygen. Contributions should include documentation for any new code added.
Some examples of how to use doxygen can be found in these guide pages:
https://learn.adafruit.com/the-well-automated-arduino-library/doxygen
https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips
## Formatting and clang-format
This library uses [`clang-format`](https://releases.llvm.org/download.html) to standardize the formatting of `.cpp` and `.h` files.
Contributions should be formatted using `clang-format`:
The `-i` flag will make the changes to the file.
```bash
clang-format -i *.cpp *.h
```
If you prefer to make the changes yourself, running `clang-format` without the `-i` flag will print out a formatted version of the file. You can save this to a file and diff it against the original to see the changes.
Note that the formatting output by `clang-format` is what the automated formatting checker will expect. Any diffs from this formatting will result in a failed build until they are addressed. Using the `-i` flag is highly recommended.
### clang-format resources
* [Binary builds and source available on the LLVM downloads page](https://releases.llvm.org/download.html)
* [Documentation and IDE integration](https://clang.llvm.org/docs/ClangFormat.html)
## About this Driver
Written by Limor Fried for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution

View File

@ -44,6 +44,14 @@ void loop() {
}
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
if (! sgp.IAQmeasureRaw()) {
Serial.println("Raw Measurement failed");
return;
}
Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t");
Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("");
delay(1000);
counter++;
@ -58,4 +66,4 @@ void loop() {
Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
}
}
}

View File

@ -1,5 +1,5 @@
name=Adafruit SGP30 Sensor
version=1.0.3
version=1.2.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor
@ -7,3 +7,4 @@ paragraph=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Se
category=Sensors
url=https://github.com/adafruit/Adafruit_SGP30
architectures=*
depends=Adafruit ILI9341, Adafruit GFX Library

View File

@ -118,12 +118,14 @@ void Sgp30Show(bool json)
{
if (sgp30_ready) {
char abs_hum[33];
if (global_update && global_humidity>0 && global_temperature!=9999) {
// has humidity + temperature
dtostrfd(sgp30_abshum,4,abs_hum);
}
if (json) {
ResponseAppend_P(PSTR(",\"SGP30\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d"), sgp.eCO2, sgp.TVOC);
if (global_update && global_humidity>0 && global_temperature!=9999) {
// has humidity + temperature
dtostrfd(sgp30_abshum,4,abs_hum);
ResponseAppend_P(PSTR(",\"" D_JSON_AHUM "\":%s"),abs_hum);
}
ResponseJsonEnd();