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

Arduino UNO-Based Environmental Monitoring System with Gas and Temperature Sensors

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Environmental Monitoring System with Gas and Temperature Sensors

Circuit Documentation

Summary

This circuit is designed to monitor environmental conditions using various sensors and provide feedback through LEDs and a buzzer. The system is built around an Arduino UNO microcontroller, which reads data from a temperature sensor (LM35), an MQ-2 gas sensor, and an MQ-135 gas sensor. Based on the sensor readings, the system controls a four-pin LED and a buzzer to indicate different environmental statuses.

Component List

  1. Arduino UNO

    • Description: A 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. MQ135

    • Description: A gas sensor used to detect air quality.
    • Pins: VCC, GND, A0, D0
  3. Temperature Sensor (LM35)

    • Description: A temperature sensor that provides an analog output proportional to the temperature.
    • Pins: +Vs, Vout, GND
  4. MQ-2

    • Description: A gas sensor used to detect combustible gases.
    • Pins: GND, VCC, ANALOG, Digital
  5. Buzzer

    • Description: An audio signaling device.
    • Pins: PIN, GND
  6. LED: Four Pin

    • Description: A four-pin LED with red, green, and blue cathodes and a common anode.
    • Pins: red cathode, common anode, green cathode, blue cathode

Wiring Details

Arduino UNO

MQ135

Temperature Sensor (LM35)

MQ-2

Buzzer

LED: Four Pin

Documented Code

// Pin definitions
const int tempSensorPin = A0;      // Temperature sensor on A0
const int mq2AnalogPin = A1;       // MQ-2 analog on A1
const int mq2DigitalPin = 2;       // Changed from D2 to 2
const int mq135AnalogPin = A2;     // MQ-135 analog on A2
const int mq135DigitalPin = 3;     // Changed from D3 to 3
const int redLEDPin = 4;           // Changed from D4 to 4
const int buzzerPin = 5;           // Changed from D5 to 5
const int greenLEDPin = 6;         // Changed from D6 to 6
const int blueLEDPin = 7;          // Changed from D7 to 7

// Thresholds
const float TEMP_HIGH = 30.0;
const float TEMP_MEDIUM = 20.0;
const int GAS_THRESHOLD = 300;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Set pin modes
  pinMode(tempSensorPin, INPUT);
  pinMode(mq2AnalogPin, INPUT);
  pinMode(mq2DigitalPin, INPUT);
  pinMode(mq135AnalogPin, INPUT);
  pinMode(mq135DigitalPin, INPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  
  // Initial LED test
  digitalWrite(redLEDPin, HIGH);
  delay(500);
  digitalWrite(redLEDPin, LOW);
  digitalWrite(greenLEDPin, HIGH);
  delay(500);
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(blueLEDPin, HIGH);
  delay(500);
  digitalWrite(blueLEDPin, LOW);
  
  Serial.println("System Initialized");
}

void loop() {
  // Read temperature sensor value
  int tempValue = analogRead(tempSensorPin);
  // Modified temperature calculation for LM35 sensor
  float temperature = (tempValue * 5.0 * 100.0) / 1024.0;
  
  // Read MQ-2 sensor values
  int mq2AnalogValue = analogRead(mq2AnalogPin);
  int mq2DigitalValue = digitalRead(mq2DigitalPin);
  
  // Read MQ135 sensor values
  int mq135AnalogValue = analogRead(mq135AnalogPin);
  int mq135DigitalValue = digitalRead(mq135DigitalPin);
  
  // Print sensor values to serial monitor with better formatting
  Serial.println("--------------------");
  Serial.print("Temperature: ");
  Serial.print(temperature, 1);  // One decimal place
  Serial.println(" °C");
  
  Serial.print("MQ-2 Sensor - Analog: ");
  Serial.print(mq2AnalogValue);
  Serial.print(" Digital: ");
  Serial.println(mq2DigitalValue ? "HIGH" : "LOW");
  
  Serial.print("MQ-135 Sensor - Analog: ");
  Serial.print(mq135AnalogValue);
  Serial.print(" Digital: ");
  Serial.println(mq135DigitalValue ? "HIGH" : "LOW");
  
  // Control LEDs based on temperature with status message
  if (temperature > TEMP_HIGH) {
    setLEDs(HIGH, LOW, LOW);
    Serial.println("Status: HIGH Temperature Alert!");
  } else if (temperature > TEMP_MEDIUM) {
    setLEDs(LOW, HIGH, LOW);
    Serial.println("Status: Normal Temperature");
  } else {
    setLEDs(LOW, LOW, HIGH);
    Serial.println("Status: Low Temperature");
  }
  
  // Control buzzer based on gas detection with pattern
  if (mq2DigitalValue == HIGH || mq135DigitalValue == HIGH) {
    Serial.println("WARNING: Gas Detected!");
    // Beeping pattern
    for (int i = 0; i < 3; i++) {
      tone(buzzerPin, 1000);  // 1kHz tone
      delay(200);
      noTone(buzzerPin);
      delay(200);
    }
  } else {
    noTone(buzzerPin);
  }
  
  // Calculate gas concentrations as percentage
  int mq2Percentage = map(mq2AnalogValue, 0, 1023, 0, 100);
  int mq135Percentage = map(mq135AnalogValue, 0, 1023, 0, 100);
  
  Serial.print("Gas Levels - MQ2: ");
  Serial.print(mq2Percentage);
  Serial.print("%, MQ135: ");
  Serial.print(mq135Percentage);
  Serial.println("%");
  
  Serial.println("--------------------\n");
  
  // Main loop delay
  delay(2000);  // Increased to 2 seconds for better readability
}

// Helper function to set LED states