Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

MQ-2 gas sensor with Arduino Nano — LPG and smoke alarm on A7

Wire an MQ-2 gas module to an Arduino Nano with AO on the analog-only A7 and DO on D2, warm it up, and raise an LED alarm above a threshold.

Arduino C++Verified — core avr 1.8.8, 3 Nov 2026Compile-only

Community draft — report a mistake

The MQ-2 senses LPG, propane, methane and smoke — the gases that matter in an Indian kitchen. It gives an analog voltage (AO) that rises with gas concentration and a digital alarm (DO) set by its trimmer.

On the Nano, AO goes on A7, one of the two analog-only pins, and DO on D2. The sketch waits for the heater to warm up, prints both outputs every second and lights the on-board LED when either crosses the limit.

Wiring

Arduino NanoOnly the pins used are shown · 5 V logicMQ-2 gas sensor moduleA75VGNDD2VCCGNDDOAO
Every connection
Arduino NanoMQ-2 gas sensor moduleWireNotes
5VVCCredThe heater draws about 150 mA — fine from USB or the 5V pin.
GNDGNDblack
A7AOyellowA7 is analog-only — exactly what AO needs.
D2DOgreenComparator output; goes LOW above the trimmer level.

5 V (heater: about 150 mA) part, 5 V board. The heater needs 5 V. AO can reach about 5 V, so on ESP32 and Pico feed AO through a 10 kΩ / 20 kΩ divider (or similar) to stay under 3.3 V. DO is driven by the comparator from VCC — divide it too on 3.3 V boards.

Board quirks

  • The Nano is 5 V, so AO (0–5 V) goes straight to an analog pin — no divider, unlike on ESP32.
  • Putting AO on A7 uses a pin that can’t do anything else, leaving A0–A5 free for an LCD on A4/A5.
  • A new sensor needs 24–48 hours of burn-in before readings settle, and about a minute of warm-up after every power-on. The can gets hot — that is normal.
  • Raw values depend on the sensor, temperature and humidity. Note the clean-air value in your kitchen and set ALARM_LEVEL about 150 above it.
  • A hobby sensor is not a certified gas detector. Use it as an extra warning next to a proper LPG alarm, never instead of one.

Code

mq2-nano.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// MQ-2 gas/smoke module on an Arduino Nano: AO on the analog-only pin A7, DO on D2.
// VCC → 5V, GND → GND, AO → A7, DO → D2. The heater needs a warm-up before readings settle.
const uint8_t PIN_AO = A7;
const uint8_t PIN_DO = 2;
const int ALARM_LEVEL = 400;  // raw ADC; set it from your own clean-air reading + margin
const unsigned long WARMUP_MS = 60000UL;

void setup() {
  Serial.begin(115200);
  pinMode(PIN_DO, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println(F("MQ-2 heating up (1 min; 24 h on first use)"));
}

void loop() {
  int raw = analogRead(PIN_AO);
  bool comparator = digitalRead(PIN_DO) == LOW;  // module's DO goes LOW above its trimmer level
  bool warm = millis() > WARMUP_MS;
  bool alarm = warm && (raw > ALARM_LEVEL || comparator);
  digitalWrite(LED_BUILTIN, alarm ? HIGH : LOW);
  Serial.print(warm ? F("") : F("[warming] "));
  Serial.print(F("AO raw: "));
  Serial.print(raw);
  Serial.print(F("  ("));
  Serial.print(raw * 5.0 / 1023, 2);
  Serial.print(F(" V)  DO: "));
  Serial.print(comparator ? F("GAS") : F("clear"));
  if (alarm) Serial.print(F("  ** ALARM **"));
  Serial.println();
  delay(1000);
}

Expected serial output

Serial Monitor · 115200Output
MQ-2 heating up (1 min; 24 h on first use)
[warming] AO raw: 512  (2.50 V)  DO: clear
AO raw: 187  (0.91 V)  DO: clear
AO raw: 642  (3.14 V)  DO: GAS  ** ALARM **

Common errors

Reading keeps drifting down for hours

The sensor is still burning in. Leave it powered for a day before setting the threshold.

DO never changes

Turn the blue trimmer: clockwise usually makes it less sensitive. Test with a lighter’s gas (unlit) held near the can.

↑↓ to move · Enter to open