Gas sensors play a crucial role in detecting harmful gases and ensuring safety. In this blog, we will interface the MQ-2 gas sensor with Arduino Uno and display the readings on a 16×2 LCD with I2C. We will also discuss wiring, errors, and troubleshooting tips.
🛠 Components Used
- Arduino Uno – Microcontroller board
- MQ-2 Gas Sensor – Detects gases like LPG, butane, methane, CO, etc.
- 16×2 LCD (I2C) – Displays gas readings
- I2C Module for LCD – Reduces wiring complexity
- Jumper Wires – For connections
- Breadboard – To make stable connections
🔌 Wiring Diagram
MQ-2 Gas Sensor to Arduino
MQ-2 Pin | Arduino Uno |
---|---|
VCC | 5V |
GND | GND |
A0 | A0 |
16×2 LCD (I2C) to Arduino
LCD Pin | Arduino Uno |
---|---|
VCC | 5V |
GND | GND |
SDA | A4 |
SCL | A5 |
💻 Arduino Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define MQ2_PIN A0 // MQ-2 analog output connected to A0
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address 0x27
void setup() {
Serial.begin(9600);
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("MQ-2 Gas Sensor");
delay(2000); // Display message for 2 seconds
}
void loop() {
int gasValue = analogRead(MQ2_PIN); // Read MQ-2 sensor
float voltage = gasValue * (5.0 / 1023.0); // Convert to voltage
// Convert to estimated PPM (for reference only)
float ppm = gasValue * (1000.0 / 1023.0); // Approximate ppm calculation
// Display values on Serial Monitor
Serial.print("Gas Value: ");
Serial.print(gasValue);
Serial.print(" | Voltage: ");
Serial.print(voltage);
Serial.print("V | PPM: ");
Serial.println(ppm);
// Display data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas: ");
lcd.print(gasValue); // Raw ADC value
lcd.setCursor(0, 1);
lcd.print("PPM: ");
lcd.print(ppm, 1); // Display PPM value with 1 decimal place
delay(1000); // Update every second
}
📟 Understanding the Output
The LCD and Serial Monitor will display three key values:
- Gas Value (0-1023): The raw analog output from the sensor
- Voltage (0-5V): Converts the gas value into voltage
- PPM (Approximate): An estimation of gas concentration
Example Output
📟 LCD Display
Gas: 300
PPM: 293.0
🖥 Serial Monitor
Gas Value: 300 | Voltage: 1.47V | PPM: 293.0
Gas Value: 310 | Voltage: 1.52V | PPM: 302.0
🔧 Common Errors & Solutions
1️⃣ LCD Not Displaying Anything
✔️ Check if the I2C address (0x27
) is correct. Run an I2C scanner sketch to find the correct address. ✔️ Ensure lcd.init();
and lcd.backlight();
are used. ✔️ Double-check SDA (A4) and SCL (A5) connections.
2️⃣ MQ-2 Sensor Always Showing the Same Value
✔️ Ensure the sensor is preheated for at least 20 seconds before reading. ✔️ Check wiring connections (VCC, GND, A0). ✔️ Use delay(1000);
to allow stable readings.
3️⃣ PPM Readings Not Accurate
✔️ This is a rough estimation. To get precise values, use a calibrated gas chamber. ✔️ Consider using MQ-2 sensor libraries for better accuracy.
🚀 Conclusion
By following this guide, you can successfully interface the MQ-2 gas sensor with Arduino and display the readings on a 16×2 LCD. This project is useful for gas leakage detection in homes and industries.
📢 Next Steps: Want to add a buzzer or IoT integration? Let me know in the comments! 🚀🔥