Arduino Uno MQ-Series Gas Sensor: Industrial Detection Platform
This professional MQ-series gas sensor platform integrates MQ-2 (smoke/LPG/propane), MQ-3 (alcohol/ethanol), MQ-5 (natural gas/methane), MQ-7 (carbon monoxide), and MQ-135 (CO2/ammonia/air quality) sensors with Arduino Uno R3 for precision analog detection through tin-dioxide (SnO2) semiconductor elements heated to 200-500°C operating temperature.
Sensor resistance decreases exponentially with target gas PPM concentration following the power law Rs/R0 = A[gas]^B, where Arduino A0 (0-5V, 10-bit ADC) converts resistance ratios to digital thresholds enabling reliable leak detection, air quality assessment, automated safety response, and industrial process monitoring.
Advanced features include auto R0 calibration, multi-sensor arrays, LCD/OLED real-time display, relay-based ventilation control, EEPROM data logging, WiFi IoT integration, and comprehensive diagnostics for production deployment.
Complete MQ-Series Components Specification
- Arduino UNO R3 microcontroller development board (ATmega328P)
- MQ-Series Gas Sensor Module (MQ-2/3/5/7/135 - select based on target gas)
- Male-to-male jumper wires (minimum 6 pieces, 22AWG recommended)
- Solderless breadboard (830-point minimum) for prototyping
- External 12V DC power supply (1A heater capacity for multiple sensors)
- 220Ω status LED resistor + 5mm LED (bicolor red/green)
- 16x2 LCD display with I2C backpack (optional real-time readout)
- 5V 2-channel relay module (ventilation/exhaust fan control)
- 10kΩ potentiometer (fine threshold adjustment)
- 0.96" OLED SSD1306 display (advanced option)
- ESP8266 WiFi module (IoT data transmission)
System Block Architecture

Precision Hardware Integration Protocol
MQ-Series Sensor to Arduino UNO Pin Mapping
MQ Sensor VCC: Arduino 5V (powers 150-200mA heater + sensing circuit)
MQ Sensor GND: Arduino GND rail (common ground essential)
MQ Sensor A0 (Analog Out): Arduino Analog Pin A0 (0-5V concentration signal, 4.88mV resolution)
MQ Sensor D0 (Digital Out): Arduino Digital Pin 2 (threshold comparator output)
Preheat Protocol: Maintain 60-second preheat period for SnO2 stabilization. Onboard potentiometer adjusts D0 threshold sensitivity from 200-800 ADC equivalent.
Power Architecture: Arduino USB powers sensing circuit; external 12V supply recommended for relay loads and multi-sensor heater current.
// Arduino Uno MQ-Series Gas Sensor - Complete Industrial Detection System
// Features: A0 analog reading, D0 threshold, LCD display, relay control, EEPROM logging
#include <LiquidCrystal.h>
#include <EEPROM.h>
const int gasPin = A0;
const int digitalPin = 2;
const int alertLed = 13;
const int buzzerPin = 8;
const int relayPin = 7;
const int contrastPin = 9;
// LCD pins (4-bit mode)
LiquidCrystal lcd(12, 11, 5, 4, 3, 10);
// Threshold values (calibrate per environment/sensor)
const int cleanAir = 250;
const int gasThreshold = 500;
int baseline = 300; // Auto-calibrated
void setup() {
Serial.begin(9600);
pinMode(digitalPin, INPUT);
pinMode(alertLed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(contrastPin, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
digitalWrite(alertLed, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(relayPin, LOW);
Serial.println("MQ Gas Sensor - Professional Edition");
Serial.println("Preheat 60s required for SnO2 stabilization...");
lcd.setCursor(0,0);
lcd.print("PREHEATING...");
lcd.setCursor(0,1);
lcd.print("60 seconds");
delay(60000); // Critical heater preheat
// Auto-baseline calibration (100 clean air samples)
calibrateBaseline();
Serial.println("System Ready - Monitoring Active");
lcd.clear();
lcd.print("Gas Monitor Ready");
delay(2000);
}
void calibrateBaseline() {
long sum = 0;
for(int i = 0; i < 100; i++) {
sum += analogRead(gasPin);
delay(50);
}
baseline = sum / 100;
EEPROM.write(0, baseline >> 8);
EEPROM.write(1, baseline & 0xFF);
Serial.print("Baseline: "); Serial.println(baseline);
}
void loop() {
int gasValue = analogRead(gasPin);
int digitalValue = digitalRead(digitalPin);
// Real-time display
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gas: ");
lcd.print(gasValue);
lcd.print(" ADC");
lcd.setCursor(0,1);
lcd.print("Base:");
lcd.print(baseline);
lcd.print(" TH:");
lcd.print(gasThreshold);
// Serial logging
Serial.print("Gas ADC: ");
Serial.print(gasValue);
Serial.print(" | Digital: ");
Serial.print(digitalValue);
Serial.print(" | Baseline: ");
Serial.println(baseline);
// Detection logic
if(gasValue > gasThreshold || digitalValue == LOW) {
digitalWrite(alertLed, HIGH);
digitalWrite(buzzerPin, HIGH);
digitalWrite(relayPin, HIGH); // Activate ventilation
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*** GAS DETECTED ***");
lcd.setCursor(0,1);
lcd.print("EVACUATE NOW!");
Serial.println("*** GAS DETECTED *** EVACUATE IMMEDIATELY ***");
} else {
digitalWrite(alertLed, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(relayPin, LOW);
lcd.setCursor(12,1);
lcd.print("CLEAR");
Serial.println("Air Quality: CLEAR");
}
delay(1000);
}
Arduino IDE Professional Deployment Protocol
- Install Arduino IDE 2.x with ATmega328P board package
- Install LiquidCrystal library (built-in) for LCD integration
- Copy complete firmware ensuring A0 analog reference and pin assignments
- Upload maintaining mandatory 60-second preheat cycle
- Monitor via Serial (9600 baud) and LCD simultaneously
- Verify baseline calibration completes (250-400 typical clean air)
Advanced Gas Concentration & PPM Calculation
MQ sensors obey Rs/R0 = A[gas]^B power law where R0 = clean air baseline resistance (measured after 24-48h burn-in). Arduino calculates PPM via sensor-specific exponential curve fitting and lookup tables. Rs derived from voltage divider: Rs = (VC × RL / VRL) - RL.
Calibration captures 100 clean air samples establishing R0 ±10% accuracy. Sensor-specific coefficients (A,B) from datasheets enable PPM calculation across 10-10000ppm range.
// MQ-Series PPM Calculator - MQ-2 LPG, MQ-5 Methane, MQ-7 CO
// Sensor-specific calibration curves and R0 auto-detection
const int mqPin = A0;
float R0 = 1.0;
float RL_VALUE = 10.0; // Load resistor (kOhm)
// Sensor-specific curve coefficients [A, B]
const float MQ2_LPG_A = 574.25;
const float MQ2_LPG_B = -0.351;
const float MQ5_METHANE_A = 60724;
const float MQ5_METHANE_B = -2.201;
const float MQ7_CO_A = 100000;
const float MQ7_CO_B = -2.862;
void setup() {
Serial.begin(9600);
delay(60000); // Preheat
// Auto-calibrate R0 (100 clean air samples)
R0 = calibrateR0();
Serial.print("R0 Clean Air Baseline: ");
Serial.print(R0, 2);
Serial.println(" kOhm");
}
float calibrateR0() {
long r0Sum = 0;
for(int i = 0; i < 100; i++) {
int val = analogRead(mqPin);
float resistance = ((1023.0/val) * RL_VALUE) - RL_VALUE;
r0Sum += resistance;
delay(50);
}
return r0Sum / 100.0;
}
float calculatePPM(float ratio, float A, float B) {
return pow(10, ((log10(ratio) - log10(A)) / B));
}
void loop() {
int rawValue = analogRead(mqPin);
float Rs = ((1023.0 / rawValue) * RL_VALUE) - RL_VALUE;
float ratio = Rs / R0;
// Calculate PPM for different gases
float lpg_ppm = calculatePPM(ratio, MQ2_LPG_A, MQ2_LPG_B);
float methane_ppm = calculatePPM(ratio, MQ5_METHANE_A, MQ5_METHANE_B);
float co_ppm = calculatePPM(ratio, MQ7_CO_A, MQ7_CO_B);
Serial.print("Ratio: ");
Serial.print(ratio, 3);
Serial.print(" | LPG: ");
Serial.print(lpg_ppm, 0);
Serial.print(" | CH4: ");
Serial.print(methane_ppm, 0);
Serial.print(" | CO: ");
Serial.print(co_ppm, 0);
Serial.println(" PPM");
// Safety thresholds
if(lpg_ppm > 1000 || methane_ppm > 500 || co_ppm > 50) {
Serial.println("*** LEAK DETECTED ***");
}
delay(2000);
}
MQ-Series Sensor Comparison & Selection Guide
- MQ-2: Smoke, LPG, Propane, Hydrogen, Methane (200-5000ppm)
- MQ-3: Alcohol, Ethanol (25-500ppm) - breathalyzers
- MQ-5: Natural Gas, LPG, Methane (300-10000ppm)
- MQ-7: Carbon Monoxide (20-2000ppm) - requires 1.5min ON/90s OFF cycle
- MQ-135: CO2, Ammonia, Benzene, Alcohol, Smoke (10-1000ppm)
- Cross-sensitivity: All MQ sensors detect multiple gases - use arrays for specificity
Multi-Gas Sensor Array Implementation
Deploy 3-5 sensor array using Arduino analog multiplexer (CD74HC4067) or additional Arduino Nano slaves. Composite Air Quality Index (AQI) aggregates normalized readings with weighted gas danger factors.
Industrial Safety & Environmental Applications
- Kitchen gas leak detection with automatic solenoid valve shutoff
- Industrial solvent vapor monitoring with ventilation activation
- HVAC air quality optimization through CO2 demand control ventilation
- Garage CO monitoring with exhaust fan and alert notification
- Battery charging room hydrogen detection
- Wastewater treatment ammonia monitoring
- Greenhouse CO2 enrichment control
// Multi-Sensor Industrial Safety System with Ventilation Control
const int mq2Pin = A0; // Smoke/LPG
const int mq7Pin = A1; // CO
const int mq135Pin = A2; // Air Quality
const int relayPin = 7; // Exhaust fan
const int valvePin = 6; // Gas valve
int thresholds = {600, 400, 500}; // Per sensor
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(valvePin, OUTPUT);
delay(60000); // Preheat
}
void loop() {
int mq2Value = analogRead(mq2Pin);
int mq7Value = analogRead(mq7Pin);
int mq135Value = analogRead(mq135Pin);
// Safety decisions
bool danger = (mq2Value > threshesholds ||
mq7Value > thresholds [phippselectronics](https://www.phippselectronics.com/using-the-mq-series-of-gas-sensors-with-arduino/) ||
mq135Value > thresholds [github](https://github.com/Bobbo117/MQ135-Air-Quality-Sensor));
if(danger) {
digitalWrite(relayPin, HIGH); // Ventilation ON
digitalWrite(valvePin, LOW); // Gas OFF
Serial.println("*** EMERGENCY *** VENTILATION + VALVE SHUTOFF ***");
} else {
digitalWrite(relayPin, LOW);
digitalWrite(valvePin, HIGH);
}
// Real-time dashboard
Serial.print("MQ2:"); Serial.print(mq2Value);
Serial.print(" MQ7:"); Serial.print(mq7Value);
Serial.print(" MQ135:"); Serial.println(mq135Value);
delay(1000);
}
// Advanced AQI Station with OLED + ESP8266 WiFi Logging
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int mq2Pin = A0;
const int mq135Pin = A1;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED failed");
for(;;);
}
display.clearDisplay();
delay(60000);
}
void loop() {
int mq2Value = analogRead(mq2Pin);
int mq135Value = analogRead(mq135Pin);
// Composite AQI (0-500)
int AQI = map((mq2Value + mq135Value)/2, 200, 800, 0, 500);
// OLED Dashboard
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("AQI: ");
display.print(AQI);
if(AQI < 50) display.print(" GOOD");
else if(AQI < 100) display.print(" MOD");
else if(AQI < 150) display.print(" UNHL");
else display.print(" DANG");
display.setCursor(0,20);
display.print("MQ2: "); display.print(mq2Value);
display.setCursor(0,35);
display.print("AQ: "); display.print(mq135Value);
display.display();
// WiFi logging (ESP8266 serial commands)
Serial.print("AQI:"); Serial.print(AQI);
Serial.print(",MQ2:"); Serial.print(mq2Value);
Serial.print(",AQ:"); Serial.println(mq135Value);
if(AQI > 150) {
Serial.println("*** POOR AIR QUALITY - VENTILATE ***");
}
delay(2000);
}
Gas Sensor Operating Characteristics & Physics
200-300°C SnO2 heater consumes 150-200mA during 60s preheat establishing stable R0 baseline resistance. Sensitivity curves: MQ-2 peaks LPG/propane 200-5000ppm; MQ-7 CO 20-2000ppm (90s OFF cycle required); MQ-135 CO2 400-2000ppm. T90 response <10s; full recovery 24-48h burn-in required.
Production Deployment Specifications
- A0 10-bit ADC provides 4.88mV resolution across 0-5V range (1024 levels)
- 60s mandatory preheat prevents false baseline readings (±20% error)
- Onboard potentiometer calibrates D0 threshold 200-800 ADC
- 4-20mA transmitter conversion enables PLC/SCADA integration
- EEPROM R0 storage compensates sensor aging (6-12 month drift)
- Watchdog timer prevents system hangs (500ms timeout)
- Brown-out detection for power stability
Field Calibration & Maintenance Protocol
- Capture 100 clean air samples establishing R0 average ±10%
- Verify known PPM test gas confirming power law coefficients A,B
- Monthly zero/span checks maintain NIST traceability
- Replace sensors after 2 years (heater degradation)
- Store R0 values in EEPROM with checksum validation
- Temperature compensation: +1.5%/°C above 25°C
Troubleshooting & Diagnostics
- NaN/zero readings: Check 5V supply, heater current (150mA min)
- Drift >20%: 24h burn-in or sensor replacement needed
- False alarms: Adjust potentiometer, add 100nF bypass cap
- Relay chatter: Add 10s hysteresis in threshold logic
- WiFi disconnects: ESP8266 3.3V logic level converter required
Advanced IoT Integration
ESP8266 serial bridge transmits AQI/PPM data to ThingSpeak, Blynk, or MQTT brokers. Node-RED dashboard provides real-time visualization, SMS alerts, and historical trending.