Gas leak detector with an MQ-2 on an Arduino Uno
An MQ-2 on A0 waits out its warm-up, then shows the gas level on a 16x2 I2C LCD and a traffic light, and a buzzer beeps when LPG passes the limit. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $37. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Gas leak detector: an MQ-2 gas sensor, a traffic light module, an active
// buzzer and a 16x2 I2C LCD on an Arduino Uno.
//
// A0 the MQ-2 board's AO (its VCC on 5 V, GND on GND)
// 2, 3, 4 the traffic light module's G, Y and R (each LED has its resistor)
// 8 the active buzzer's plus leg, its other leg on GND
// LCD SDA -> A4 SCL -> A5 VCC -> 5 V GND -> GND
//
// The MQ-2 is a heated tin dioxide element over a 1 k load resistor. Gas lets
// it conduct, so AO rises: a higher reading means more gas. A cold element
// reads high too, as if the room were full of gas, so for the first 20
// seconds the yellow light is on and nothing is believed.
//
// Clean air reads about 11 here once it has warmed up, and 1000 ppm of LPG
// settles near 120. The alarm sounds above LIMIT and stops only below CLEAR,
// a little lower, so a reading that hovers on the line does not chatter the
// buzzer on and off. Your own sensor will read differently: run it in clean
// air, note the number, and set LIMIT well above it.
//
// Press Run, wait for the green light, then drag the slider on the MQ-2.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int GAS_PIN = A0;
const int GREEN_PIN = 2;
const int YELLOW_PIN = 3;
const int RED_PIN = 4;
const int BUZZER_PIN = 8;
const unsigned long WARMUP_MS = 20000; // the heater's settling time
const int LIMIT = 100; // alarm at or above this reading
const int CLEAR = 80; // and quiet again below this one
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool alarm = false;
void lights(bool green, bool yellow, bool red) {
digitalWrite(GREEN_PIN, green ? HIGH : LOW);
digitalWrite(YELLOW_PIN, yellow ? HIGH : LOW);
digitalWrite(RED_PIN, red ? HIGH : LOW);
}
void setup() {
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Gas leak detector");
lcd.init();
lcd.backlight();
}
void loop() {
int gas = analogRead(GAS_PIN);
unsigned long now = millis();
lcd.setCursor(0, 0);
if (now < WARMUP_MS) {
lights(false, true, false);
digitalWrite(BUZZER_PIN, LOW);
lcd.print("Warming up ");
lcd.print((WARMUP_MS - now) / 1000 + 1);
lcd.print("s ");
Serial.print("warming up ");
} else {
if (gas >= LIMIT) alarm = true;
if (gas < CLEAR) alarm = false;
if (alarm) {
lights(false, false, true);
// Beep: on for one reading, off for the next.
digitalWrite(BUZZER_PIN, digitalRead(BUZZER_PIN) == HIGH ? LOW : HIGH);
lcd.print("GAS LEAK! ");
Serial.print("GAS LEAK! ");
} else {
lights(true, false, false);
digitalWrite(BUZZER_PIN, LOW);
lcd.print("Air is clean ");
Serial.print("clean ");
}
}
lcd.setCursor(0, 1);
lcd.print("Gas level: ");
lcd.print(gas);
lcd.print(" ");
Serial.print("gas level ");
Serial.println(gas);
delay(250);
}
Parts list
6 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.
How it is wired
10 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Traffic light module pin GND; Buzzer pin 2
- Arduino Uno R3 pin 8; Buzzer pin 1
- Arduino Uno R3 pin 4; Traffic light module pin R
- Arduino Uno R3 pin 3; Traffic light module pin Y
- Arduino Uno R3 pin 2; Traffic light module pin G
- Arduino Uno R3 pin 5V; Gas sensor, MQ-2 pin VCC; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Gas sensor, MQ-2 pin GND; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A0; Gas sensor, MQ-2 pin AO
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.