Home IoT Hub Series · 10

[Part 10] Get warned before gas smell becomes obvious - MQ-5 kitchen gas node

Use an MQ-5 sensor node as a gas leak warning slot for the hub.

This node adds gas-leak warning using an MQ-5 module. It is a practical warning slot, but it is not a certified safety device.

MQ sensors need warm-up time and stable power. Install it where air can reach the sensor, not inside a sealed box.

[Part 10] Get warned before gas smell becomes obvious - MQ-5 kitchen gas node hero image
System architecture and data pipeline note
This article is part of the Home IoT Hub system connected to the central NodeMCU server built in Part 2: Building the Brain. The MQ-5 sensor updates the hub warning slot when gas concentration rises, which then drives buzzer logic. The collected data is sent over the local IoT_Hub_Net network and updates slot 7 for gas leak warning on the central server (/set?no7=value), becoming part of the decision pipeline for the 20-channel smart-home controller.

Parts and cost

PartQtyUnit price
Wemos D1 Mini1 pc$1.50
MQ-5 gas sensor module1 pc$1
Jumper wires 3 pc
Total$2.50

Pin wiring

[Part 10] Get warned before gas smell becomes obvious - MQ-5 kitchen gas node wiring diagram
Sensor / moduleBoard pin
VCC5V (VU pin)
GNDGND
AOUTA0 (analog)

Full code

[Part 10] Get warned before gas smell becomes obvious - MQ-5 kitchen gas node - Code 2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "IoT_Hub_Net";
const char* password = "iothub1234";
const char* hubURL   = "http://192.168.1.1";

#define GAS_PIN    A0
#define SLOT_NO    7
#define THRESHOLD  400   // suspect a leak above this value
#define INTERVAL   5000

void setup() {
  Serial.begin(115200);
  pinMode(GAS_PIN, INPUT);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to IoT_Hub_Net");
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
  Serial.println("\nConnected!");
  Serial.println("MQ-5 warming up (60s)...");
  delay(60000); // note
  Serial.println("Ready.");
}

void loop() {
  int raw = analogRead(GAS_PIN);
  int leak = (raw > THRESHOLD) ? 1 : 0;

  Serial.print("MQ-5: "); Serial.print(raw);
  Serial.print(" -> "); Serial.println(leak ? "LEAK!" : "OK");

  WiFiClient client; HTTPClient http;
  String url = String(hubURL) + "/set?no"
             + String(SLOT_NO) + "=" + String(leak);
  http.begin(client, url); http.GET(); http.end();
  Serial.println("HTTP -> " + url);

  delay(INTERVAL);
}

Upload and check

Connect the Wemos D1 Mini to the PC with a USB data cable, select the ESP8266 board and port in the Arduino IDE, upload the sketch, then open Tools → Serial Monitor and set the baud rate to 115200.

Serial Monitor

After uploading, open Tools → Serial Monitor in the Arduino IDE and set the baud rate to 115200. The following is an example Serial Monitor flow for checking normal operation. This is not a real screenshot. It shows MQ-5 warm-up, threshold checking, and slot 7 warning updates.

[BOOT] Home IoT Node #10 - MQ-5 gas warning
[WARMUP] MQ-5 heater active, baseline=184
[SENSOR] gas_raw=221 threshold=420
[FILTER] alcohol_vapor_guard=PASS
[HTTP] GET /set?no7=0
[HUB] 200 OK - slot no7=NORMAL
[LOOP] next gas sample in 5000 ms
  • If Connected or connected appears, the Wi-Fi step passed.
  • If HTTP 200 or 200 OK appears, the value reached the brain server.
  • If the value does not update, check both the slot number in the URL and the sensor wiring.

Build notes

  • Use the serial monitor to observe normal room values first. Set the warning threshold after you understand the baseline in your home.
  • Power on the hub brain first so the node can join IoT_Hub_Net.
  • Use the same slot number written in the source code.
  • Open the serial monitor at 115200 baud and confirm HTTP 200.
  • If the value does not appear on the hub, recheck wiring, GPIO number, and Wi-Fi connection.

Where and How to Use the MQ-5 Gas Leak Sensor

The MQ-5 detects combustible gases such as LPG, LNG (city gas), and butane, sounding a buzzer at dangerous concentrations as an early-warning helper device. Paired with the other nodes of your home IoT hub, it can go beyond a local beep to push a smartphone alert.

Troubleshooting checkpoints

The node code can look short, but real home installation is affected by placement, power, Wi-Fi quality, and sensor noise. Check these points before letting the node become a decision input for the brain server.

SymptomLikely causeField fix
Alerts keep firing right after installationThe MQ sensor heater has not stabilized yetTreat the first 24-48 hours as burn-in and disable alerts or log only.
Cooking wine or alcohol vapor triggers alertsMQ-5 is also sensitive to alcohol vaporPlace it at least 1.5 m away from the stove side or ceiling area and ignore short spikes.
The value does not fall quicklyGas or vapor remains in the sensor chamber and recovery is slowClear alerts only after normal readings continue for a fixed duration.
The ESP8266 reboots unexpectedlyThe MQ-5 heater current makes a weak USB supply sagUse a stable 5V supply for the sensor and keep common GND while leaving enough power margin.

Use Cases

  • Early kitchen leak warning: If you forget to close the stove valve or a slow leak develops, the buzzer reacts before your nose does.
  • Boiler and utility rooms: Unattended spaces benefit most from constant machine monitoring.
  • Single-person and elderly safety: In homes where someone lives alone or has a dulled sense of smell, it catches leaks that are easy to miss.
  • Alerts while away: Send readings to your server through the hub and you'll know instantly on your phone if a leak happens while the house is empty.

Recommended Placement

Height changes completely depending on the gas type. This is the most important point.

  • LPG (propane/butane): Heavier than air, so it sinks to the floor. Mount the sensor about 30cm above the floor.
  • City gas (LNG/methane): Lighter than air, so it rises. Mount the sensor about 30cm below the ceiling.
  • Keep 1-3m from gas appliances (stove, boiler). Too close causes frequent false alarms while cooking; too far slows the response.
  • Avoid placing it right in front of vents, windows, or the range hood, where airflow disperses gas and delays detection.

Practical Tips and Cautions

  • Warm-up: After power-on, allow at least 2-3 minutes; for precise readings, let it stabilize 24-48 hours. Don't trust the value right after startup.
  • Sensitivity tuning: Set the threshold with the onboard potentiometer, but only as sensitive as your normal cooking environment allows without false alarms.
  • False triggers: It can also react to cooking smoke, oil vapor, alcohol, sprays, and detergent fumes. Filter these out with placement and sensitivity.
  • Lifespan and replacement: The sensing element is a consumable. Accuracy degrades, so replace it roughly every 2-3 years.
  • Supplementary only: The MQ-5 is a hobby/auxiliary alarm. It cannot replace a certified home gas detector; leave life-safety to certified products.

Frequently Asked Questions

Q. My home uses city gas — where should I mount the sensor? City gas (LNG) is light and rises, so install it about 30cm below the ceiling. If you use LPG instead, mount it near the floor (about 30cm).

Q. It goes off every time I cook — is it broken? Not broken; the sensitivity is too high or it's too close to the appliance. Move it 1-3m away and lower the sensitivity with the potentiometer.

Q. Can this replace buying a real gas detector? No. The MQ-5 is a supplementary alarm, not a substitute for a certified gas detector. Always use both together.