Gas / smoke alarm (hobby)

MQ-2 sensor with a buzzer and a web page. A hobby add-on, not a certified detector.

Safety Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 1–2 h

⚠ Caution

A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.

A hobby sensor with honest limits

The MQ-2 is a heated metal-oxide sensor. It reacts to LPG, methane, smoke and many other things, and it cannot tell them apart. It is sensitive to humidity and ages. Treat it as an extra, a noisy early hint, never as a replacement for a certified gas or smoke detector.

Using it sensibly

The heater draws about 150 mA at 5 V and needs a warm-up of at least a minute. The first time you use a new sensor, run it for a day or two so that the baseline settles. The analog output can reach 5 V, so use a divider before the ESP32 ADC.

  • LPG is heavier than air and collects low, methane is lighter and rises. Mount the sensor accordingly.
  • Calibrate against clean air and set the threshold above the daily noise, not at the first bump.
  • Never make the sensor the only thing that closes a gas valve.
  • Keep a certified detector in the kitchen anyway.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • MQ-2 gas/smoke sensor module
  • Active buzzer module 5V
  • Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k)

Wiring

Module Module pin ESP32 pin Note
MQ-2 VCC / GND 5V / GND heater ~150 mA, warm-up 2–3 min
MQ-2 AO analog GPIO34 via 10k/20k divider AO can reach 5 V — protect the ADC
Buzzer signal GPIO25

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN";   // from @BotFather
const char* TG_CHAT  = "YOUR_CHAT_ID";

// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
  if (WiFi.status() != WL_CONNECTED) return false;
  WiFiClientSecure client;
  client.setInsecure();   // simple; pin the certificate for production
  HTTPClient http;
  String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
  if (!http.begin(client, url)) return false;
  http.addHeader("Content-Type", "application/json");
  String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
  int code = http.POST(body);
  http.end();
  return code == 200;
}
const int PIN_GAS = 34, PIN_BUZ = 25;
int LIMIT = 1800;                 // raw ADC threshold: calibrate in clean air first
bool sent = false;
void setup() {
  pinMode(PIN_BUZ, OUTPUT);
  Serial.begin(115200);
  connectWiFi();
  delay(120000);                  // sensor warm-up
}
void loop() {
  long v = 0; for (int i = 0; i < 16; i++) { v += analogRead(PIN_GAS); delay(10); }
  v /= 16;
  Serial.println(v);
  bool alarm = v > LIMIT;
  digitalWrite(PIN_BUZ, alarm ? HIGH : LOW);
  if (alarm && !sent) sent = telegramSend("Gas / smoke level high! Ventilate and leave the room if in doubt.");
  if (!alarm) sent = false;
  delay(1000);
}
// IMPORTANT: this is a hobby indicator, NOT a certified gas or fire detector. Use certified alarms for safety.

Build tips

  • Burn in a new sensor for 24–48 hours and set GAS_DELTA after observing clean-air values.
  • Mount it high for propane/smoke detection and near the floor for heavy gases only after reading the datasheet.

What you need

Buy the parts kit · Estimated total: $7.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
MQ-2 gas/smoke sensor module × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Active buzzer module 5V × 1
$0.50
Buy at AliExpress Buy at Amazon.com
Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Other countries: order these parts on AliExpress with the links below.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

Freezer / fridge alarm

A DS18B20 probe inside the freezer; a buzzer and a message if the temperature rises or the door stays open.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit
⚠ Caution

A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.

Carbon monoxide (CO) guard (hobby)

An MQ-7 sensor with a buzzer and display for a garage, boiler room or generator shed. A hobby add-on, not a certified CO alarm.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

3D printer enclosure guard

Temperature probe and smoke sensor inside the enclosure with an alarm, a message and an optional power cut for the printer.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–4 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.