#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("ESP-32")
#define Pin (4) // Analog input pin for MQ-2 sensor
#define LED_PIN (13) // GPIO pin for the LED
#define BUZZER_PIN (12) // GPIO pin for the buzzer
/***********************Software Related Macros************************************/
#define Type ("MQ-2") // MQ2
#define Voltage_Resolution (3.3)
#define ADC_Bit_Resolution (12) // For ESP32
#define RatioMQ2CleanAir (9.83) // RS / R0 = 9.83 ppm
/*****************************Globals***********************************************/
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int valor_gas = 0;
bool ledState = LOW; // Variable to store the current state of the LED
void setup() {
// Initialize the LCD
lcd.init();
lcd.begin(20, 4);
lcd.clear();
lcd.backlight();
lcd.print("PRACTICA");
delay(1000);
lcd.clear();
lcd.print("SENSADO LM35");
lcd.setCursor(0,1);
lcd.print("CURSO 1 ");
delay(1000);
lcd.clear();
// Initialize the serial port for debugging
Serial.begin(9600);
// Set up the MQ-2 sensor
MQ2.setRegressionMethod(1); // PPM = a * ratio^b
MQ2.setA(574.25); MQ2.setB(-2.222); // Configure the equation for LPG concentration
MQ2.init();
Serial.print("Calibrating please wait.");
float calcR0 = 0;
for(int i = 1; i <= 10; i++) {
MQ2.update(); // Update data
calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
Serial.print(".");
}
MQ2.setR0(calcR0 / 10);
Serial.println(calcR0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MQ2 = ");
lcd.print(calcR0);
delay(500); // Sampling frequency
if(isinf(calcR0)) { Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected)"); while(1); }
if(calcR0 == 0) { Serial.println("Warning: Connection issue, R0 is zero (Analog pin shorts to ground)"); while(1); }
// Set up the LED and buzzer pins as outputs
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Ensure the LED is initially off
digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is initially off
}
void loop() {
MQ2.update(); // Update data
MQ2.readSensor(); // Read PPM concentration
MQ2.serialDebug(); // Print the table on the serial port
valor_gas = analogRead(Pin);
valor_gas = map(valor_gas, 0, 4095, 0, 100);
Serial.println(valor_gas);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MQ2 = ");
lcd.print(valor_gas);
lcd.setCursor(10,0);
lcd.print("PPM");
// Check gas concentration levels
if (valor_gas >= 90) {
// Display "URGER" message
lcd.setCursor(0,1);
lcd.print("URGENT !");
lcd.setCursor(0,2);
lcd.print("Fire Nearby");
lcd.setCursor(0,3);
lcd.print("Please Check Area");
// Flip-flop the LED
ledState = !ledState; // Toggle the LED state
digitalWrite(LED_PIN, ledState); // Update the LED
// Activate the buzzer continuously
digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
} else if (valor_gas > 70) {
// Display "Smoke Detected" message
lcd.setCursor(0,1);
lcd.print("Smoke Detected");
lcd.setCursor(0,2);
lcd.print("Please Check Area");
// Flip-flop the LED
ledState = !ledState; // Toggle the LED state
digitalWrite(LED_PIN, ledState); // Update the LED
// Turn off the buzzer
digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off
} else {
// Display "Air Quality OK" message
lcd.setCursor(0,1);
lcd.print("Air Quality OK");
digitalWrite(LED_PIN, LOW); // Turn off the LED
digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off
}
delay(500); // Sampling frequency
}