MQ-2 Gas Sensor with an ESP32 DevKit V1
MQ-2 is a low-cost tin-dioxide (SnO2) semiconductor sensor that detects combustible gases and smoke (LPG, propane, hydrogen, methane) and reports concentration as an analog voltage.

The MQ-2's heater needs 5 V, so on the ESP32 DevKit V1 its VCC goes to VIN and its AOUT to GPIO33, an ADC1 pin, through a 10 kΩ and 20 kΩ divider that scales its 5 V swing by 2/3. DOUT stays unwired, since the module pulls it up to 5 V; the sketch raises its alarm from AOUT instead.
Wiring the MQ-2 Gas Sensor to the ESP32 DevKit V1
Pin by pin.
| MQ-2 Gas Sensor | ESP32 DevKit V1 | Note |
|---|---|---|
| VCC | VIN | |
| GND | GND | |
| AOUT | GPIO33 (D33) | through a 10 kΩ resistor, with a 20 kΩ resistor from the board pin to GND: a divider that scales the 5 V AOUT by 2/3 |
- VCC is on VIN, which gives about 5 V only while the board is powered from USB.
- DOUT is left unwired here: the module pulls it up to its 5 V supply. The sketch raises its alarm from AOUT instead.
What to know on the ESP32 DevKit V1
- The heater takes 5.0 V ±0.2 V across about 31 Ohm, roughly 160 mA, all drawn from VIN and so from the USB port. A weak cable or hub can sag and reset the board.Hanwei MQ-2 datasheet, p. 1
- An ESP32 input is rated to VDD + 0.3 V, so AOUT reaches GPIO33 through 10 kΩ with 20 kΩ to GND, which puts at most about 3.3 V on the pin. DOUT is the LM393's open-collector output with its pull-up to the module's 5 V, which is why the page leaves it off the ESP32.Espressif ESP32 Series Datasheet v5.3, p. 52
- AOUT is on GPIO33, ADC1 channel 5, so it keeps reading with Wi-Fi on; ADC2 pins return nothing once the Wi-Fi driver starts. The sketch reads it with analogReadMilliVolts() at 11 dB, multiplies by 1.5 to undo the divider, and alarms at 30 % between its 0.30 V clean-air and 4.50 V smoke points; record your own after the preheat.Espressif ESP32 Series Datasheet v5.3, p. 14; ESP-IDF Programming Guide, ADC Oneshot Mode Driver; Espressif arduino-esp32 ADC API documentation
MQ-2 Gas Sensor code for the ESP32 DevKit V1
Compiled for esp32:esp32:esp32 on 2026-10-30.
// MQ-2 Gas Sensor on ESP32 DevKit V1: wiring and a free simulation at https://www.tinkered.ai/components/mq2-gas-sensor/esp32
// MQ-2 smoke/gas sensor on an ESP32 DevKit. AOUT reaches GPIO33 through a 10 kΩ / 20 kΩ divider
// (the pin sees 2/3 of AOUT). DOUT stays unwired: the module pulls it up to 5 V, so the
// alarm here is a threshold on AOUT, as the module's trimmer sets one for DOUT.
const int MQ2_AOUT = 33;
const float DIVIDER = 1.5; // (10 kΩ + 20 kΩ) / 20 kΩ: AOUT = pin voltage x 1.5
const float CLEAN_AIR_V = 0.30; // AOUT in clean air, warmed up (0 %)
const float SMOKE_V = 4.50; // AOUT in heavy smoke (100 %)
const int ALARM_PERCENT = 30; // set it from your own readings in clean air and near a gas source
void setup() {
Serial.begin(9600);
analogSetPinAttenuation(MQ2_AOUT, ADC_11db); // 11 dB, the widest range: Espressif rates it accurate to about 2.45 V
}
void loop() {
float aout = analogReadMilliVolts(MQ2_AOUT) / 1000.0 * DIVIDER;
int gasPercent = round((aout - CLEAN_AIR_V) * 100.0 / (SMOKE_V - CLEAN_AIR_V));
gasPercent = constrain(gasPercent, 0, 100);
bool alarm = gasPercent >= ALARM_PERCENT;
Serial.print("AOUT: ");
Serial.print(aout, 2);
Serial.print(" V | Gas level: ");
Serial.print(gasPercent);
Serial.print(" % | Alarm: ");
Serial.println(alarm ? "GAS DETECTED" : "clear");
delay(500);
}
MQ-2 Gas Sensor on the ESP32 DevKit V1: questions
Can I run the MQ-2 on the ESP32's 3.3 V?
No. The heater is specified at 5.0 V ±0.2 V (Hanwei datasheet, p. 1); at 3.3 V the sensor runs cold and reads wrong. Power it from VIN or a 5 V supply and divide AOUT, as the page does.
Which ESP32 pin should the MQ-2's AOUT use?
An ADC1 pin, GPIO32 to GPIO39 (ESP32 datasheet, p. 46), through a divider. The page uses GPIO33, which keeps working with Wi-Fi.
Keep going
This guide as plain Markdown. /components/mq2-gas-sensor/esp32.md