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.
Components Needed
- Arduino (e.g., Uno, Nano)
- MQ-2 Gas Sensor
- Buzzer or LED (optional for feedback)
- Breadboard and jumper wires
Circuit Setup
- Connect the VCC pin of the MQ-2 sensor to the 5V pin on the Arduino.
- Connect the GND pin of the MQ-2 sensor to the GND pin on the Arduino.
- Connect the analog output pin of the MQ-2 sensor to an analog input pin on the Arduino (e.g., A0).
- Connect a buzzer or LED to a digital pin (optional, for feedback).
Ensure the sensor is exposed to the air for accurate gas detection.
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:
analogRead(pin)
: Reads the voltage from the sensor and converts it to a value between 0 and 1023.Serial.println()
: Sends the gas concentration to the serial monitor for display.digitalWrite(pin, HIGH/LOW)
: Turns the buzzer or LED on/off depending on the sensor reading.
Troubleshooting
- If the sensor is not triggering at the expected levels, adjust the threshold value in the code.
- Ensure the sensor is adequately ventilated to detect gases properly.
- If no output is displayed on the serial monitor, check the wiring and ensure the correct analog pin is used.