Skip to content
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

Setting baseline stalls sensor #22

Open
bboynton97 opened this issue Oct 15, 2020 · 4 comments
Open

Setting baseline stalls sensor #22

bboynton97 opened this issue Oct 15, 2020 · 4 comments

Comments

@bboynton97
Copy link

SGP30 Unable to Accept Setting Baseline

  • Arduino board: ESP32
  • Using Platformio 5.0.1 in Atom

Description
Before my board powers down, I want to record the latest baseline in order to restart the sensor with that baseline later and not have to wait for it to recalibrate. I can pull the baseline no problem, but attempting to set the baseline causes the sensor to output the default values of TVOC 0ppb and eCO2 400ppm. These values never change. When I restart the board without setting the baseline values, it works no problem and starts establishing a new baseline like normal.

I would like to believe this is a code issue because I have troubleshooted every possible alternative issue I could think of over the past 6 hours. I also posted this on the forum to no avail.

Repro

#include <Arduino.h>

#include <Wire.h>        // I2C library
#include "Adafruit_SGP30.h"
#include "DHT.h"
#include <EEPROM.h>

// Flash Memory addresses
#define SGP_CO2_BASELINE    0
#define SGP_TVOC_BASELINE    1

#define DHT_TYPE DHT22

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;
}

// Create sensors
Adafruit_SGP30 sgp;
DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);

  // Enable EEPROM
  EEPROM.begin(64); // 64 bytes of memory

  // Set up DHT22
  dht.begin();

  // Set up SGP30
  if (! sgp.begin()){
    Serial.println("Sensor not found :(");
    while (1);
  }

  // Start at baseline if it exists
  uint16_t co2_baseline = EEPROM.read(SGP_CO2_BASELINE);
  uint16_t tvoc_baseline = EEPROM.read(SGP_TVOC_BASELINE);

  // *** I even tried setting the values manually here with no luck ***   <-----
  // co2_baseline = 0xEE; //238;
  // tvoc_baseline = 0xAF; //175;

  Serial.println("Retrieved baselines from memory:");
  Serial.print(" eCO2: 0x"); Serial.print(co2_baseline, HEX);
  Serial.print(" & TVOC: 0x"); Serial.println(tvoc_baseline, HEX);
  if (co2_baseline != NULL && tvoc_baseline != NULL) {
      Serial.println("Setting baselines");
      sgp.setIAQBaseline(co2_baseline, tvoc_baseline);  // <--- If you remove this line, it runs like normal. With it in, it outputs defaults
  }

  Wire.begin();
  delay(3000);
  Serial.println("setup: done");
}

int counter = 0;
void loop() {
  float rh = dht.readHumidity();
  double  temp = dht.readTemperature();

  sgp.setHumidity(getAbsoluteHumidity(temp, rh));

  if (! sgp.IAQmeasure()) {
      Serial.println("Measurement failed");
      return;
  }

  Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
  Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");

  delay(500);

  counter++;
  if (counter == 30) {
    counter = 0;

    uint16_t TVOC_base, eCO2_base;
    if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      Serial.println("Failed to get baseline readings");
      return;
    }
    Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
    Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);

    EEPROM.write(SGP_CO2_BASELINE, eCO2_base);
    EEPROM.write(SGP_TVOC_BASELINE, TVOC_base);
    EEPROM.commit();
  }
}
@gorghino
Copy link

Any news?

@Chesteron
Copy link

According to "SGP30 Data Sheet" you need to call first Init command.
Have you tried that?

"After a power-up or soft reset, the baseline of the baseline compensation algorithm can be restored by sending first an “sgp30_iaq_init” command followed by a “sgp30_set_iaq_baseline” command with the two baseline values as parameters in the order as (TVOC, CO2eq)."

@caternuson
Copy link
Contributor

@gorghino Did you try @Chesteron suggestion? Your sketch is not calling IAQinit().

image

@gorghino
Copy link

Hi @caternuson , I guess you need to ask @bboynton97 .
I haven't the SGP30 with me anymore so I cannot test @Chesteron 's suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants