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

Arduino UNO Based Gas Detection and Temperature Monitoring System with SMS Alerts

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Gas Detection and Temperature Monitoring System with SMS Alerts

Circuit Documentation

Summary

The circuit in question is designed around an Arduino UNO microcontroller and includes various sensors and output devices. The primary purpose of this circuit is to monitor environmental conditions using gas sensors (MQ6) and a temperature and humidity sensor (DHT11). It also includes a buzzer and an LED for alerts, and a SIM800L module for potential communication capabilities. The circuit is powered by the Arduino's 5V output and shares a common ground.

Component List

Arduino UNO

MQ6 Gas Sensor

DHT11 Temperature and Humidity Sensor

LED: Two Pin (red)

Resistor

Buzzer

Sim800l

Wiring Details

Arduino UNO

MQ6 Gas Sensor

DHT11 Temperature and Humidity Sensor

LED: Two Pin (red)

Resistor

Buzzer

Sim800l

Documented Code

#include <U8glib.h>
#include <DHT.h>

#define DHTPIN 2            // Pin where DHT11 is connected
#define DHTTYPE DHT11       // DHT 11
#define MQ3_PIN A0          // MQ-3 analog pin
#define MQ4_PIN A1          // MQ-4 analog pin
#define BUTTON_PIN 3        // Push button pin
#define RED_LED_PIN 4       // Red LED pin
#define GREEN_LED_PIN 5     // Green LED pin
#define BUZZER_PIN 6        // Buzzer pin

DHT dht(DHTPIN, DHTTYPE);
U8GLIB_SH1106_128X64 u8g; // Initialize display
bool measuring = false;

void setup() {
    Serial.begin(9600);
    dht.begin();
    pinMode(RED_LED_PIN, OUTPUT);
    pinMode(GREEN_LED_PIN, OUTPUT);
    pinMode(BUZZER_PIN, OUTPUT);
    pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor

    u8g.begin();
}

void loop() {
    if (digitalRead(BUTTON_PIN) == LOW) {
        delay(200); // Debounce delay
        measuring = true;
        startMeasurement();
    }

    if (!measuring) {
        // Show press to start message
        displayMessage("Press to start");
    }
}

void startMeasurement() {
    int mq3Value = 0;
    int mq4Value = 0;
    float humidity = dht.readHumidity();
    float temperature = dht.readTemperature();
    
    // Read sensor values for 20 seconds
    for (int i = 0; i < 20; i++) {
        mq3Value += analogRead(MQ3_PIN);
        mq4Value += analogRead(MQ4_PIN);
        // Update display with readings
        updateDisplay(mq3Value / (i + 1), mq4Value / (i + 1), humidity, temperature);
        delay(1000); // Wait 1 second between readings
    }

    mq3Value /= 20; // Average
    mq4Value /= 20; // Average

    // Check against threshold values (set these as needed)
    int thresholdMQ3 = 400; // Example threshold for MQ-3
    int thresholdMQ4 = 100; // Example threshold for MQ-4

    // Determine spoilage
    if (mq3Value > thresholdMQ3 || mq4Value > thresholdMQ4) {
        handleSpoilage(mq3Value, mq4Value, humidity, temperature);
    } else {
        handleFreshness(mq3Value, mq4Value, humidity, temperature);
    }
}

void updateDisplay(int mq3Value, int mq4Value, float humidity, float temperature) {
    u8g.firstPage();
    do {
        u8g.setFont(u8g_font_ncenB08);
        u8g.drawStr(0, 10, "MQ3 Value:");
        u8g.setPrintPos(0, 20);
        u8g.print(mq3Value);
        u8g.drawStr(0, 30, "MQ4 Value:");
        u8g.setPrintPos(0, 40);
        u8g.print(mq4Value);
        u8g.drawStr(0, 50, "Temp: ");
        u8g.setPrintPos(0, 60);
        u8g.print(temperature);
        u8g.drawStr(60, 50, "Humidity: ");
        u8g.setPrintPos(60, 60);
        u8g.print(humidity);
    } while (u8g.nextPage());
}

void handleSpoilage(int mq3Value, int mq4Value, float humidity, float temperature) {
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    for (int i = 0; i < 5; i++) {
        tone(BUZZER_PIN, 1000); // Beep sound
        delay(500);
        noTone(BUZZER_PIN);
        delay(500);
    }

    // Display spoilage message
    displayMessage("Food Spoilage");
    delay(8000); // Show message for 2 seconds

    // Show results for 10 seconds
    displayResults(mq3Value, mq4Value, humidity, temperature);
}

void handleFreshness(int mq3Value, int mq4Value, float humidity, float temperature) {
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(GREEN_LED_PIN, HIGH);

    // Display freshness message
    displayMessage("Food is fresh!");
    delay(8000); // Show message for 2 seconds

    // Show results for 10 seconds
    displayResults(mq3Value, mq4Value, humidity, temperature);
}

void displayResults(int mq3Value, int mq4Value, float humidity, float temperature) {
    unsigned long startTime = millis();
    while (millis() - startTime < 10000) { // 10 seconds
        updateDisplay(mq3Value, mq4Value, humidity, temperature);
    }

    // Reset LEDs and measuring state
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(GREEN_LED_PIN, LOW);
    measuring = false; // Reset measuring state
    displayMessage("Press to start"); // Prompt to start again
}

void displayMessage(const char* message) {