#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define MQ137_PIN 34 // Analog pin for MQ137 sensor
#define BUZZER_PIN 25 // Digital pin for buzzer
#define PPM_THRESHOLD 100 // PPM threshold for triggering the buzzer
void setup() {
// Initialize serial communication at 115200 baud rate
Serial.begin(115200);
// Initialize the SSD1306 display with I2C address 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Display static text
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("MQ137 PPM and Concentration"));
display.display();
// Initialize the buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off initially
}
void loop() {
// Read from MQ137 sensor
int adcValue = analogRead(MQ137_PIN);
float voltage = adcValue * (3.3 / 4095.0); // Convert to voltage (ESP32 ADC range is 0-4095)
// Convert voltage to PPM (this is a simplified example, proper calibration is required)
float ppm = (voltage - 0.1) * 100;
// Calculate concentration from PPM (example calculation, needs calibration)
float concentration = ppm * 0.1; // Example conversion factor
// Display values on Serial Monitor
Serial.print("ADC Value: "); Serial.print(adcValue);
Serial.print(", Voltage: "); Serial.print(voltage);
Serial.print(", PPM: "); Serial.print(ppm);
Serial.print(", Concentration: "); Serial.println(concentration);
// Display values on OLED
display.clearDisplay();
display.setCursor(0, 10); // Start a bit lower to avoid overlap
display.print("PPM: ");
display.print(ppm);
display.setCursor(0, 30); // Move cursor down for concentration
display.print("Concentration: ");
display.print(concentration);
display.display();
// Check if PPM exceeds the threshold
if(ppm > PPM_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
}
delay(1000); // Update every second
}