Good day everyone, I tried making an Automated Toxic Fume Recirculator, but there is a problem with it; when i set the toxic fume level to 500 it supposedly automaticaly turn on and vacuum the fumes, but it turns out the opposite way, it automatically start at 500. When i try to blow air on the MQ2 the toxic fume level decreases, here is my sketch that ive made. Can you guys help pls :< SOS
Sketch:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// I2C pins declaration
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the I2C address, 16 columns, and 2 rows
// Relay pin declaration
const int relayPin = 7; // Set the pin number for the relay (change this if necessary)
// MQ-2 sensor pin declaration (Analog input)
const int mq2Pin = A0; // MQ-2 analog output connected to A0
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2); // Define LCD size
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("FUME TOXIN");
lcd.setCursor(0, 1);
lcd.print("Please wait. . . . ");
delay(5000);
lcd.clear(); // Clear the LCD screen
pinMode(relayPin, OUTPUT); // Set relay pin as an output
digitalWrite(relayPin, HIGH); // Make sure the relay is off initially
// Optionally, you can add initialization for the MQ-2 sensor if needed
}
void loop()
{
int mq2Value = analogRead(mq2Pin); // Read the analog value from MQ-2 sensor (0-1023)
// Convert the raw value to a meaningful range if needed (e.g., 0-100 or other)
float voltage = mq2Value * (5.0 / 1023.0); // Convert the analog value to voltage (0 to 5V)
// Output the sensor value and voltage to the serial monitor (for debugging)
Serial.print("MQ2 Raw Value: ");
Serial.print(mq2Value);
Serial.print("\t Voltage: ");
Serial.println(voltage, 3);
// Display the reading on the LCD screen
lcd.setCursor(0, 0);
lcd.print("Fume Level: ");
lcd.print(mq2Value); // Display raw value of MQ-2 sensor
// If the fume level exceeds a certain threshold, turn on the relay (e.g., for an alarm)
int threshold = 500; // Adjust the threshold value based on your testing
if (mq2Value > threshold) {
digitalWrite(relayPin, LOW); // Trigger relay (turn on alarm or fan, etc.)
lcd.setCursor(0, 1);
lcd.print("lcd.print(mq2Value); // Display raw value of MQ-2 sensorF``umes detected");
} else {
digitalWrite(relayPin, HIGH); // Turn off relay if no fumes are detected
}
delay(200); // Delay to prevent flickering
}
Schematic Diagram:

