update adafruit sgp30 lib

This commit is contained in:
gemu2015 2019-05-18 14:16:29 +02:00
parent d40a9b96d7
commit 22a4410280
10 changed files with 59 additions and 18 deletions

View File

View File

@ -60,7 +60,8 @@ boolean Adafruit_SGP30::begin(TwoWire *theWire) {
_i2c = theWire; _i2c = theWire;
} }
_i2c->begin(); // assume i2c initialized already to avoid resetting clock stretching
// _i2c->begin();
uint8_t command[2]; uint8_t command[2];
@ -156,6 +157,29 @@ boolean Adafruit_SGP30::setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) {
return readWordFromCommand(command, 8, 10); 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 @brief I2C low level interfacing

View File

@ -42,6 +42,7 @@ class Adafruit_SGP30 {
boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base); boolean getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base);
boolean setIAQBaseline(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()} * The last measurement of the IAQ-calculated Total Volatile Organic Compounds in ppb. This value is set when you call {@link IAQmeasure()}

View File

@ -3,6 +3,17 @@
Adafruit_SGP30 sgp; Adafruit_SGP30 sgp;
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("SGP30 test"); Serial.println("SGP30 test");
@ -22,6 +33,11 @@ void setup() {
int counter = 0; int counter = 0;
void loop() { void loop() {
// If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
//float temperature = 22.1; // [°C]
//float humidity = 45.2; // [%RH]
//sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
if (! sgp.IAQmeasure()) { if (! sgp.IAQmeasure()) {
Serial.println("Measurement failed"); Serial.println("Measurement failed");
return; return;

View File

@ -1,5 +1,5 @@
name=Adafruit SGP30 Sensor name=Adafruit SGP30 Sensor
version=1.0.2 version=1.0.3
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor