Introduction
Gas sensors play a crucial role in detecting hazardous gases in the environment. The MQ-2 sensor is widely used for its sensitivity to gases like LPG, propane, methane, hydrogen, and smoke. By connecting this sensor to an Arduino Uno, you can create a system that monitors gas levels and triggers alerts when necessary.
Parts Required
Before you start, ensure you have the following components:
- MQ-2 Gas Sensor
- Arduino Uno
- Jumper Wires
- LED
- Bread Board
MQ-2 Gas Sensor Pinout
Understanding the pinout of the MQ-2 sensor is essential for making the right connections:
- VCC: Connects to the 5V power supply of the Arduino.
- GND: Connects to the ground (GND) on the Arduino.
- AO (Analog Output): Outputs an analog signal proportional to the concentration of gas. Connect this to an analog input pin on the Arduino.
- DO (Digital Output): Outputs a digital signal (high or low) when the gas concentration exceeds a certain threshold. This can be connected to a digital pin on the Arduino.
- D11 : Connects to the input for the Positive pin of the Red LED
- D12 : Connects to the positive pin of the Green LED
CODE:
int mq2Pin = A0; // Analog pin connected to MQ-2 sensor
int redLEDPin = 13; // Digital pin connected to Red LED
int greenLEDPin = 12; // Digital pin connected to Green LED
int buzzerPin = 11; // Digital pin connected to Buzzer
int threshold = 500; // Threshold value for alcohol detection (adjust as needed)
void setup() {
pinMode(redLEDPin, OUTPUT); // Set the Red LED pin as output
pinMode(greenLEDPin, OUTPUT); // Set the Green LED pin as output
pinMode(buzzerPin, OUTPUT); // Set the Buzzer pin as output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(mq2Pin); // Read the analog value from the sensor
Serial.print(“Sensor Value: “); // Print the sensor value to the Serial Monitor
Serial.println(sensorValue);
// Compare the sensor value to the threshold
if (sensorValue > threshold) {
digitalWrite(redLEDPin, HIGH); // Turn on the Red LED if alcohol is detected
digitalWrite(greenLEDPin, LOW); // Turn off the Green LED
digitalWrite(buzzerPin, HIGH); // Turn on the Buzzer
Serial.println(“Alcohol Detected!”); // Print message to Serial Monitor
} else {
digitalWrite(redLEDPin, LOW); // Turn off the Red LED
digitalWrite(greenLEDPin, HIGH); // Turn on the Green LED if no alcohol is detected
digitalWrite(buzzerPin, LOW); // Turn off the Buzzer
}
delay(1000); // Wait for 1 second before the next reading
}
GITHUB link : https://github.com/makertribe/IoT-Codes/blob/9453e325a378052d0904ed1e72199af2e369fba3/MQ2%20Gas%20sensor%20connection%20with%20arduino
OUTPUT :