The SGP30 VOC gas sensor paired with ESP32 is an excellent combination for monitoring indoor air quality in Indian homes, offices, and schools. The SGP30 from Sensirion measures Total Volatile Organic Compounds (TVOC) and equivalent CO2 (eCO2), providing actionable data about air pollution sources like paints, cleaning products, adhesives, and cooking fumes that are common in Indian households. This tutorial covers everything you need to set up SGP30 VOC gas sensor air quality monitoring with an ESP32.
Table of Contents
- What is the SGP30 Sensor?
- Air Quality Concerns in Indian Homes
- Wiring SGP30 to ESP32
- Installing Required Libraries
- Complete Arduino Code
- Baseline Calibration
- Adding an OLED Display
- Frequently Asked Questions
What is the SGP30 Sensor?
The SGP30 is a fully calibrated, multi-pixel gas sensor platform manufactured by Sensirion AG. It uses a metal oxide sensing element to detect a broad range of VOCs and outputs eCO2 (equivalent CO2 concentration in ppm) and TVOC (total volatile organic compounds in ppb) readings. The sensor communicates over I2C, making it easy to interface with any microcontroller including the ESP32.
Key specifications of the SGP30:
- Operating voltage: 1.8V (I2C logic level)
- Supply current: 48mA (max)
- TVOC output range: 0–60,000 ppb
- eCO2 output range: 400–60,000 ppm
- Response time: <1 second
- I2C address: 0x58 (fixed)
- Operating temperature: -40°C to +85°C
The SGP30 module boards available in India typically include a 3.3V regulator and I2C level shifter, allowing direct connection to both 3.3V ESP32 and 5V Arduino boards.
Air Quality Concerns in Indian Homes
Indoor air quality is a significant concern in India. Several factors make Indian homes particularly susceptible to elevated VOC levels:
- Cooking with biomass or LPG: Traditional cooking releases acrolein, benzene, and formaldehyde
- Incense and dhoop burning: Common in Indian households; releases significant TVOC
- New furniture and paints: Formaldehyde off-gassing from MDF and water-based paints
- Cleaning products: Phenol-based disinfectants widely used post-COVID
- Seasonal factors: Summer heat accelerates off-gassing from building materials
WHO guidelines suggest TVOC levels below 300 ppb are acceptable, 300–500 ppb is marginal, and above 500 ppb requires action. An SGP30-based monitor helps you identify when and where VOC levels spike in your home.
Wiring SGP30 to ESP32
The SGP30 uses I2C communication. Here is the wiring for an ESP32 development board:
| SGP30 Pin | ESP32 Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GPIO 21 |
| SCL | GPIO 22 |
Note: The SGP30’s I2C address is hardcoded at 0x58 and cannot be changed. Ensure no other I2C device on your bus uses this address. The BME280 (0x76 or 0x77) and SSD1306 OLED (0x3C) are safe to use alongside the SGP30.
Installing Required Libraries
In Arduino IDE, go to Sketch → Include Library → Manage Libraries and install:
- Adafruit SGP30 Sensor — by Adafruit Industries
- Adafruit Unified Sensor — dependency for Adafruit libraries
For PlatformIO users, add to platformio.ini:
lib_deps =
adafruit/Adafruit SGP30 Sensor@^2.0.1
adafruit/Adafruit Unified Sensor@^1.1.9
Complete Arduino Code
#include <Wire.h>
#include "Adafruit_SGP30.h"
Adafruit_SGP30 sgp;
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// Use this with SGP30's setHumidity() for better accuracy
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f *
exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature));
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity);
return absoluteHumidityScaled;
}
void setup() {
Serial.begin(115200);
Wire.begin();
if (!sgp.begin()) {
Serial.println("SGP30 not found!");
while (1);
}
Serial.print("SGP30 serial #");
Serial.print(sgp.serialnumber[0], HEX);
Serial.print(",");
Serial.print(sgp.serialnumber[1], HEX);
Serial.print(",");
Serial.println(sgp.serialnumber[2], HEX);
}
void loop() {
if (!sgp.IAQmeasure()) {
Serial.println("Measurement failed");
return;
}
Serial.print("TVOC: "); Serial.print(sgp.TVOC); Serial.print(" ppbt");
Serial.print("eCO2: "); Serial.print(sgp.eCO2); Serial.println(" ppm");
// Air quality classification
String quality;
if (sgp.TVOC < 300) quality = "Good";
else if (sgp.TVOC < 500) quality = "Moderate";
else if (sgp.TVOC < 1000) quality = "Unhealthy for Sensitive Groups";
else quality = "Unhealthy";
Serial.print("Air Quality: "); Serial.println(quality);
delay(1000);
}
Baseline Calibration
The SGP30 uses an on-chip baseline algorithm that automatically adjusts to ambient conditions. However, for best accuracy, save and restore the baseline values periodically. This prevents the sensor from “drifting” after a power cycle:
// Save baseline (run periodically, e.g., every hour)
uint16_t eco2_base, tvoc_base;
if (sgp.getIAQBaseline(&eco2_base, &tvoc_base)) {
// Save to EEPROM or SPIFFS
preferences.putUShort("eco2_base", eco2_base);
preferences.putUShort("tvoc_base", tvoc_base);
}
// Restore baseline on startup (after 12+ hours of initial learning)
eco2_base = preferences.getUShort("eco2_base", 0);
tvoc_base = preferences.getUShort("tvoc_base", 0);
if (eco2_base && tvoc_base) {
sgp.setIAQBaseline(eco2_base, tvoc_base);
}
Allow at least 12 hours of continuous operation before relying on baseline values. The SGP30’s algorithm improves continuously with exposure to your environment’s typical air composition.
Adding an OLED Display
For a standalone air quality monitor, add an SSD1306 OLED display. Install the Adafruit SSD1306 and Adafruit GFX libraries, then connect the OLED SDA/SCL to GPIO 21/22 (shared I2C bus):
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// In setup():
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// In loop(), after SGP30 reading:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Air Quality Monitor");
display.print("TVOC: "); display.print(sgp.TVOC); display.println(" ppb");
display.print("eCO2: "); display.print(sgp.eCO2); display.println(" ppm");
display.display();
Frequently Asked Questions
Why does the SGP30 always show 400 ppm eCO2 and 0 ppb TVOC initially?
These are the default startup values while the sensor’s algorithm initialises. The SGP30 requires approximately 15 seconds of warm-up after power-on before reporting meaningful values. Additionally, the baseline algorithm needs 12–24 hours of continuous operation to fully calibrate to your environment. The initial 400 ppm eCO2 represents clean outdoor air baseline, which is correct.
How accurate is the SGP30 for detecting cooking fumes?
The SGP30 is highly sensitive to alcohols, aldehydes, and ketones produced during cooking. It will detect elevated TVOC levels within seconds of starting to cook. However, it is a broad-spectrum sensor and cannot identify specific VOCs like formaldehyde individually. For specific gas identification, you would need electrochemical or photoionisation sensors.
Can I use SGP30 for monitoring air quality near traffic in Indian cities?
The SGP30 detects combustion-related VOCs from vehicle exhaust, making it useful for outdoor monitoring near busy roads in cities like Delhi, Mumbai, and Bangalore. However, for outdoor use, protect the sensor from direct rain and dust. The BME280’s humidity reading is essential here to apply humidity compensation and improve accuracy.
What is the difference between SGP30 and MQ135 for air quality?
The SGP30 is far superior to the MQ135. The SGP30 provides calibrated digital readings in standard units (ppb/ppm), has integrated baseline compensation, and uses I2C for noise-free communication. The MQ135 provides only an uncalibrated analog voltage that varies with temperature and humidity, requiring extensive external calibration. The SGP30 costs slightly more (₹500–₹800 vs ₹50–₹100) but delivers professional-grade results.
How do I integrate SGP30 readings with a home automation system in India?
The ESP32’s Wi-Fi allows direct integration with Home Assistant via MQTT, ESPHome, or the HTTPClient library. You can set up automation rules that turn on an exhaust fan when TVOC exceeds 500 ppb, or send WhatsApp notifications via Twilio API when air quality degrades. Home Assistant is free and runs on a Raspberry Pi—a popular choice for smart home enthusiasts in India.
Add comment