Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Gas Detection System with MQ-5 Sensor and Buzzer Alert

View and Edit Project In Cirkit DesignerImage of Arduino UNO Gas Detection System with MQ-5 Sensor and Buzzer Alert

Circuit Documentation

Summary

This circuit is designed to monitor gas levels using an MQ-5 gas sensor and trigger a buzzer and fan when gas levels exceed a certain threshold. The circuit is controlled by an Arduino UNO microcontroller, which reads the gas sensor values and activates the buzzer and fan accordingly. The Arduino also logs the gas values along with a simulated date and time to the serial monitor.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. MQ-5 Gas Sensor

    • Description: Gas sensor module for detecting LPG, natural gas, and coal gas.
    • Pins: VCC, GND, Digi Out, Analog out
  3. Fan

    • Description: DC fan for ventilation.
    • Pins: GND, 5V
  4. Buzzer

    • Description: Piezoelectric buzzer for sound alerts.
    • Pins: +, GND

Wiring Details

Arduino UNO

MQ-5 Gas Sensor

Fan

Buzzer

Code Documentation

Arduino Code

#define smoke_sensor 2
#define buzzer 13
#define mq5_sensor A0 // Define the MQ5 sensor pin (analog pin)

unsigned long previousMillis = 0; // To track elapsed time
unsigned long currentMillis = 0;

int seconds = 0;  // Starting seconds
int minutes = 45; // Adjust this to the current minute
int hours = 14;   // Adjust this to the current hour (2:45 PM = 14)
int days = 23;    // Set this to the current day of the month
int months = 6;   // Set this to the current month (June)
int years = 2024; // Set this to the current year

// Array to hold the number of days in each month
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void setup() {
  // Initialize smoke sensor, buzzer, and serial monitor
  pinMode(smoke_sensor, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600); // Serial communication for gas values

  // Print column headings
  Serial.println("Date/Time\t\tGas Value");
  Serial.println("--------------------------------------");
}

void loop() {
  // Simulate date and time
  currentMillis = millis();
  if (currentMillis - previousMillis >= 1000) { // Increment every second
    previousMillis = currentMillis;
    seconds++;

    if (seconds >= 60) {
      seconds = 0;
      minutes++;
    }
    if (minutes >= 60) {
      minutes = 0;
      hours++;
    }
    if (hours >= 24) {
      hours = 0;
      days++;
    }

    // Check for month rollover
    if (days > daysInMonth[months - 1]) {
      days = 1;
      months++;
    }

    // Check for year rollover
    if (months > 12) {
      months = 1;
      years++;
    }

    // Adjust for leap years
    if (years % 4 == 0 && years % 100 != 0 || years % 400 == 0) {
      daysInMonth[1] = 29; // February has 29 days in a leap year
    } else {
      daysInMonth[1] = 28; // February has 28 days in a non-leap year
    }

    // Read the gas concentration value from MQ5
    int gas_value = analogRead(mq5_sensor);

    // Convert 24-hour format to 12-hour format with AM/PM
    int display_hours = hours % 12;
    if (display_hours == 0) display_hours = 12; // Adjust midnight and noon to 12

    // Print simulated date and time with gas value in 12-hour format
    if (days < 10) Serial.print("0"); Serial.print(days);
    Serial.print("/");
    if (months < 10) Serial.print("0"); Serial.print(months);
    Serial.print("/");
    Serial.print(years);
    Serial.print(" ");

    if (display_hours < 10) Serial.print("0"); Serial.print(display_hours);
    Serial.print(":");
    if (minutes < 10) Serial.print("0"); Serial.print(minutes);
    Serial.print(":");
    if (seconds < 10) Serial.print("0"); Serial.print(seconds);

    // Display AM or PM
    if (hours < 12) {
      Serial.print(" AM");
    } else {
      Serial.print(" PM");
    }

    // Format for columns: Gas Value
    Serial.print("\t");
    Serial.print(" ");
    Serial.println(gas_value);

    // Existing smoke sensor functionality
    if (digitalRead(smoke_sensor) == LOW) {
      digitalWrite(buzzer, HIGH);
      delay(5000);
      digitalWrite(buzzer, LOW);
    }
  }

  delay(100); // Small delay for smoother output
}

This code initializes the smoke sensor, buzzer, and serial communication. It simulates date and time, reads gas concentration values from the MQ-5 sensor, and logs these values along with the date and time to the serial monitor. If the smoke sensor detects gas, it activates the buzzer for 5 seconds.