MQ-2 Gas Sensor Experiment

MQ-2 Gas Sensor Experiment

Introduction

The MQ-2 Gas Sensor detects gases such as smoke, methane, and LPG. In this experiment, we will use the sensor to detect smoke levels and trigger an alarm when a threshold is reached.

MQ-2 Gas Sensor

Components Needed

Circuit Setup

  1. Connect the VCC pin of the MQ-2 sensor to the 5V pin on the Arduino.
  2. Connect the GND pin of the MQ-2 sensor to the GND pin on the Arduino.
  3. Connect the analog output pin of the MQ-2 sensor to an analog input pin on the Arduino (e.g., A0).
  4. Connect a buzzer or LED to a digital pin (optional, for feedback).

Ensure the sensor is exposed to the air for accurate gas detection.

MQ-2 Gas Sensor Circuit Setup

Code for MQ-2 Gas Sensor

Upload the following code to your Arduino to read gas levels:


// Define the analog pin for the sensor
int sensorPin = A0;  
int sensorValue = 0;

void setup() {
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  sensorValue = analogRead(sensorPin);  // Read the value from the sensor
  Serial.println(sensorValue);  // Print the value to the serial monitor
  if (sensorValue > 400) {  // Threshold for detecting smoke
    digitalWrite(13, HIGH);  // Turn on LED or buzzer
  } else {
    digitalWrite(13, LOW);  // Turn off LED or buzzer
  }
  delay(1000);  // Wait for a second
}
            

Explanation

The MQ-2 sensor uses a heating element to detect changes in gas concentrations. The Arduino reads the analog output and triggers an alert when the gas concentration exceeds a preset threshold:

Troubleshooting