# Gas leak alarm with an MQ-2 sensor and calibration on Arduino: design worksheet and code

> A ₹846 design worksheet for a hobby gas and smoke alarm using an MQ-2 module, covering the warm-up, the sensor-resistance maths, a clean-air calibration and a threshold with hysteresis, plus a clear warning that it is not a certified detector.

Canonical: https://roboticsengineering.in/projects/gas-leak-alarm-mq2-calibration-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹846 gas and smoke alarm for learning how MQ-series sensors behave. An MQ-2 module’s analog output is converted to the sensor’s resistance, compared with its resistance in clean air, and an alarm sounds when the ratio drops. The page covers warm-up, calibration and thresholds, because getting those wrong is the usual reason such alarms are useless.**

**Scope:** intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The table below was computed by script for an example 10 kilohm load resistor and a 20 kilohm clean-air resistance. This is a hobby project and not a certified gas detector. It must never be relied on to protect people from gas, fire or carbon monoxide; fit proper certified alarms. MQ sensors also respond to several gases at once, so the reading cannot tell you which gas it is.

## How it works

The MQ-2 has a heated sensing element whose resistance falls when combustible gases or smoke are present. The module puts that resistance in a divider with a load resistor RL, and the Arduino reads the divider’s output Vout. The sensor resistance is then Rs = RL × (Vc − Vout) / Vout. For example with RL = 10 kΩ and Vout = 1 V from a 5 V supply, Rs = 10k × 4 / 1 = 40 kΩ.

Because every sensor differs, the sketch measures the clean-air resistance R0 after warm-up and then watches the ratio Rs / R0. In clean air the ratio is about 1; gas lowers it. The alarm starts below 0.6 and clears above 0.7 so it does not flutter around one value. The heater needs time to settle: datasheets and guides commonly recommend a long first burn-in and a few minutes of warm-up at every start, so the sketch ignores the sensor for the first two minutes.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| MQ-2 gas sensor module | 1 | ₹150 | ₹150 |
| Active buzzer module | 1 | ₹15 | ₹15 |
| LEDs (green, yellow, red) | 3 | ₹5 | ₹15 |
| 220 ohm resistors | 3 | ₹2 | ₹6 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹846 |  |  |

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

## The numbers

| Rs / R0 | Rs (R0 = 20 kΩ) | Vout (RL = 10 kΩ, 5 V) | ADC count |
| --- | --- | --- | --- |
| 1.00 (clean air) | 20 kΩ | 1.67 V | 341 |
| 0.60 | 12 kΩ | 2.27 V | 465 |
| 0.30 | 6 kΩ | 3.13 V | 639 |
| 0.10 | 2 kΩ | 4.17 V | 853 |

*Vout = 5 × RL / (RL + Rs) for a divider with RL to ground; count = 1023 × Vout / 5. Check how your module is wired: some put the load resistor on the other side, which reverses the trend.*

The heater alone draws on the order of 150 mA from the 5 V supply on many modules, so power it from a stable source. Readings also move with temperature and humidity, so a fixed threshold in a bathroom and in a garage are different problems.

## Power and wiring

| Part | Connects to | Note |
| --- | --- | --- |
| MQ-2 module VCC and GND | 5V and GND | Heater needs a steady 5 V |
| MQ-2 AOUT | A0 | Not the digital DOUT pin |
| LEDs through 220 ohm | D2, D3, D4 | Green, yellow, red |
| Buzzer | D8 | Active buzzer |

*Mount the sensor where air can reach it, away from cooking steam and from the heat of the Arduino.*

## The sketch

```
// Gas alarm with MQ-2: Uno + MQ-2 module + LEDs + buzzer
// Design worksheet: hobby learning project, NOT a certified detector.

const int SENSOR = A0;
const int LED_G = 2, LED_Y = 3, LED_R = 4, BUZ = 8;
const float VC = 5.0, RL = 10000.0;           // check your module's load resistor
const unsigned long WARMUP_MS = 120000UL;     // 2 minutes
const float ALARM_BELOW = 0.60, CLEAR_ABOVE = 0.70;

float R0 = 0;
bool alarm = false;

float readRs() {
  long sum = 0;
  for (int i = 0; i < 16; i++) { sum += analogRead(SENSOR); delay(5); }
  float v = (sum / 16.0) * VC / 1023.0;
  if (v < 0.05) v = 0.05;                     // avoid divide by zero
  return RL * (VC - v) / v;
}

void setup() {
  pinMode(LED_G, OUTPUT); pinMode(LED_Y, OUTPUT);
  pinMode(LED_R, OUTPUT); pinMode(BUZ, OUTPUT);
  Serial.begin(9600);

  digitalWrite(LED_Y, HIGH);                  // warming up
  delay(WARMUP_MS);

  float sum = 0;                              // measure clean air with the sensor in fresh air
  for (int i = 0; i < 20; i++) { sum += readRs(); delay(100); }
  R0 = sum / 20.0;
  digitalWrite(LED_Y, LOW);
  Serial.print("R0 = "); Serial.println(R0);
}

void loop() {
  float ratio = readRs() / R0;
  if (!alarm && ratio < ALARM_BELOW) alarm = true;
  if (alarm && ratio > CLEAR_ABOVE) alarm = false;

  digitalWrite(LED_G, !alarm);
  digitalWrite(LED_R, alarm);
  digitalWrite(BUZ, alarm && (millis() / 300) % 2 == 0);
  Serial.println(ratio, 2);
  delay(200);
}
```

Start it in fresh air, because whatever the sensor sees in the first minutes after warm-up becomes its idea of clean air. If the ratio rises when you introduce gas, your module is wired the other way round and the comparison must be flipped. The sketch has been proofread but not compiled on this site.

## Test plan

1. **Warm-up:** record how the reading settles over the first ten minutes after power-up.
2. **Clean-air baseline:** note R0 over several days and different rooms.
3. **Safe stimulus:** bring a lit match’s smoke, or the vapour from a lighter with the flame off, near the sensor from a safe distance in a ventilated room, and watch the ratio fall. Never release gas into a closed room.
4. **Recovery:** time how long the ratio takes to return to normal in fresh air.
5. **Humidity:** compare the baseline after a hot shower or cooking.
6. **Alarm threshold:** adjust ALARM_BELOW and CLEAR_ABOVE until false alarms stop in your normal conditions.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Alarm at every start-up | Warm-up too short or started in smoke | Wait longer and start in fresh air |
| Never alarms | Wired the other way or threshold too low | Print the ratio and check its direction |
| False alarms near the kitchen | Cooking fumes and steam | Move the sensor or raise the alarm level |
| Readings drift for days | Sensor ageing or humidity | Re-measure R0 regularly |
| Wrong gas assumed | MQ-2 reacts to several gases | Treat it as a general smoke and gas indicator |

## Upgrades

- **Second sensor:** add an MQ-7 for a rough carbon monoxide indication, still not a certified alarm.
- **Relay:** switch a vent fan, with a safe design for the mains side.
- **Logging:** record ratio and time to see trends.
- **Wi-Fi notification:** use an ESP32 to send an alert, as an extra on top of certified alarms.

## Cost and parts notes

The ₹846 total is an illustration and does not include shipping. Use the [cost calculator](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**Can this replace a smoke or gas alarm?**

No. It is a learning project. Fit certified alarms for real safety.

**Why calibrate in clean air?**

Each sensor’s resistance differs, so the sketch measures its own baseline and compares against it.

**Why wait for warm-up?**

The heater must reach temperature before the resistance is meaningful, and guides commonly suggest a long first burn-in as well.

**Can it tell which gas it is?**

No. MQ sensors respond to several gases and cannot identify them.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.
