// Include necessary libraries
#include <Wire.h>
#include "Adafruit_SGP30.h"
// Define pin numbers
#define BUZZER_PIN 13 // Change this to the pin number connected to the buzzer
#define I2C_SDA_PIN 21 // Change this to the pin number connected to the SDA of your sensor
#define I2C_SCL_PIN 22 // Change this to the pin number connected to the SCL of your sensor
// Create an instance of the SGP30 sensor
Adafruit_SGP30 sgp;
void setup() {
// Start Serial communication
Serial.begin(115200);
// Initialize I2C communication
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
// Initialize the gas sensor
if (!sgp.begin()) {
Serial.println("Error initializing SGP30 sensor!");
while (1);
}
// Set up baseline measurements
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
Serial.println("Error setting baseline values!");
}
// Initialize buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// Measure eCO2 and TVOC
if (!sgp.IAQmeasure()) {
Serial.println("Error reading CO2 and TVOC values");
return;
}
// Retrieve the measured values
uint16_t eCO2 = sgp.eCO2;
uint16_t TVOC = sgp.TVOC;
// Print the values to Serial monitor
Serial.print("eCO2: ");
Serial.print(eCO2);
Serial.print(" ppm\tTVOC: ");
Serial.print(TVOC);
Serial.println(" ppb");
// Check if gas levels are above a certain threshold
if (eCO2 > 1000 || TVOC > 100) {
// Trigger the buzzer
digitalWrite(BUZZER_PIN, HIGH);
delay(500); // Buzzer on for 0.5 seconds
digitalWrite(BUZZER_PIN, LOW);
}
// Wait for some time before the next measurement
delay(2000); // Adjust this delay based on your application
}