Arduino library · Sensors

Sensirion I2C SGP30: an example that runs

Yes. You can use the Sensirion I2C SGP30 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Air quality sensor, SGP30). The example below, "SGP30 the Sensirion way: serial number, self test and air quality", runs on the Arduino Uno R3.

Built in on Mokxi: #include <SensirionI2CSgp30.h> and it builds, with nothing to install. Free, and no account needed.

What Sensirion I2C SGP30 does

SensirionI2CSgp30 sgp30: begin(Wire), initAirQuality(), measureAirQuality(tvoc, co2eq), the baseline, setAbsoluteHumidity(), measureTest(), the raw H2 and ethanol signals and getSerialId(), each returning an error code. That is what Sensirion I2C SGP30 covers. The sketch below uses it on the Arduino Uno: “SGP30 the Sensirion way: serial number, self test and air quality”.

Sensirion's own SGP30 library (Sensirion I2C SGP30), the SensirionI2CSgp30 class for the SGP30 indoor air quality sensor.

Each sends the datasheet's sixteen-bit command, any argument words with their CRC, waits the command's maximum time and reads the answer's words, CRC checked. The part answers 0x58: for the first 15 s after initAirQuality() it reports the datasheet's start values, 400 ppm and 0 ppb, then the air set on it.

The calls follow release 0.1.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameSensirion I2C SGP30
AuthorSensirion
Include#include <SensirionI2CSgp30.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAir quality sensor, SGP30

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <SensirionI2CSgp30.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Sensirion I2C SGP30" by Sensirion from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Sensirion I2C SGP30 to lib_deps in PlatformIO. Mokxi follows the calls of release 0.1.0, so a sketch written here builds there too.

What to know in the simulator: The part holds a baseline and a humidity setting without running the baseline algorithm. Needs Sensirion Core (SensirionCore.h), which Mokxi ships too.

The calls you will use

In the order a sketch meets them.

SensirionI2CSgp30 sgp30

Makes sgp30, the SensirionI2CSgp30 object the calls below are made on.

sgp30.begin(Wire)

Starts it up. Call it once in setup().

sgp30.getSerialId(serialNumber, serialNumberSize)

Returns the serial id as a whole number. Arguments: serial id, serial id size.

sgp30.measureTest(testResult)

Returns the test as a whole number. Arguments: test result.

sgp30.measureAirQuality(tvoc, co2eq)

Returns the air quality as a whole number.

Example: SGP30 the Sensirion way: serial number, self test and air quality

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Arduino.h>
#include <SensirionI2CSgp30.h>
#include <Wire.h>

SensirionI2CSgp30 sgp30;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }

  Wire.begin();

  uint16_t error;
  char errorMessage[256];

  sgp30.begin(Wire);

  uint16_t serialNumber[3];
  uint8_t serialNumberSize = 3;

  error = sgp30.getSerialId(serialNumber, serialNumberSize);
  if (error) {
    Serial.print("Error trying to execute getSerialId(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
  } else {
    Serial.print("SerialNumber:");
    Serial.print("0x");
    for (size_t i = 0; i < serialNumberSize; i++) {
      uint16_t value = serialNumber[i];
      Serial.print(value < 4096 ? "0" : "");
      Serial.print(value < 256 ? "0" : "");
      Serial.print(value < 16 ? "0" : "");
      Serial.print(value, HEX);
    }
    Serial.println();
  }

  uint16_t testResult;
  error = sgp30.measureTest(testResult);
  if (error) {
    Serial.print("Error trying to execute measureTest(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
  } else if (testResult != 0xD400) {
    Serial.print("measureTest failed with error: ");
    Serial.println(testResult);
  } else {
    Serial.println("measureTest passed");
  }

  error = sgp30.initAirQuality();
  if (error) {
    Serial.print("Error trying to execute initAirQuality(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
  }
}

void loop() {
  uint16_t error;
  char errorMessage[256];

  delay(1000);

  uint16_t tvoc = 0;
  uint16_t co2eq = 0;

  error = sgp30.measureAirQuality(tvoc, co2eq);
  if (error) {
    Serial.print("Error trying to execute measureAirQuality(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
  } else {
    Serial.print("tvoc:");
    Serial.print(tvoc);
    Serial.print("\t");
    Serial.print("co2eq:");
    Serial.println(co2eq);
  }
}

Wiring

  • Arduino Uno R3 A4 to Air quality sensor, SGP30 SDA
  • Arduino Uno R3 A5 to Air quality sensor, SGP30 SCL
  • Arduino Uno R3 5V to Air quality sensor, SGP30 VIN
  • Arduino Uno R3 GND to Air quality sensor, SGP30 GND

Open it in the editor, wired and ready to run

Try Sensirion I2C SGP30 without installing anything

Open the example, change the sketch, and keep your version in a free account.