MQ-2 Gas Sensor with a Raspberry Pi Pico
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 Raspberry Pi Pico its VCC goes to VBUS, which carries the USB supply only while the board is plugged into USB. AOUT reaches GP28 through a 10 kΩ and 20 kΩ divider that scales its 0 to 5 V swing by two thirds, and DOUT is left unconnected because the module pulls it up to 5 V.
Wiring the MQ-2 Gas Sensor to the Raspberry Pi Pico
Pin by pin.
| MQ-2 Gas Sensor | Raspberry Pi Pico | Note |
|---|---|---|
| VCC | VBUS | |
| GND | GND | |
| AOUT | GP28 | 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 VBUS, 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 Raspberry Pi Pico
- VBUS is wired straight to pin 1 of the micro-USB connector, so the heater's roughly 160 mA comes from the USB port, not from the Pico's 3.3 V regulator. A Pico running from a battery on VSYS has VBUS at 0 V and a cold heater; give the sensor its own 5 V supply then, with the grounds joined.Raspberry Pi Pico datasheet, release 21, p. 8; Hanwei MQ-2 datasheet, p. 1
- With AOUT at a full 5 V the divider puts 3.33 V on GP28, inside the rail-plus-300 mV limit of the ADC pins. Without it, AOUT would push current through GP28's internal diode into the 3.3 V rail.Raspberry Pi Pico datasheet, release 21, section 4.2, p. 19
- The sketch multiplies the pin voltage by DIVIDER (1.5) to get AOUT back, then scales it between CLEAN_AIR_V 0.30 V and SMOKE_V 4.50 V, which are the Uno sketch's 61 and 921 counts turned into volts. They are starting points, not your sensor's values: after the preheat, read AOUT in clean air and near a gas source, and set both constants and ALARM_PERCENT from what you see.Hanwei MQ-2 datasheet, p. 1
- DOUT comes from the module's LM393, an open-collector output pulled up to the sensor's 5 V, while the Pico's GPIO is fixed at 3.3 V. To use the trimmer's threshold, bring DOUT in through a divider like AOUT's or a level shifter; otherwise the sketch's AOUT threshold does the same job.TI LM393 datasheet (SLCS005AH), p. 17; Raspberry Pi Pico datasheet, release 21, section 4.2, p. 19
- Expect the values to wander on a new sensor: Hanwei characterises the MQ-2 after more than 48 hours of preheat, and one stored unpowered for a long time needs extended aging before it settles.Hanwei MQ-2 datasheet, p. 1
MQ-2 Gas Sensor code for the Raspberry Pi Pico
Compiled for rp2040:rp2040:rpipico on 2026-10-30.
// MQ-2 Gas Sensor on Raspberry Pi Pico: wiring and a free simulation at https://www.tinkered.ai/components/mq2-gas-sensor/raspberry-pi-pico
// MQ-2 smoke/gas sensor on a Raspberry Pi Pico. AOUT reaches GP28 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 = 28;
const float ADC_VREF = 3.3; // the Pico's ADC reference: its 3.3 V rail
const float ADC_STEPS = 4096.0; // 12 bits
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);
analogReadResolution(12); // arduino-pico defaults to 10 bits
}
void loop() {
float aout = analogRead(MQ2_AOUT) * ADC_VREF / ADC_STEPS * 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 Raspberry Pi Pico: questions
Can a Raspberry Pi Pico power an MQ-2 gas sensor?
From VBUS while it runs on USB, yes: the heater needs 5.0 V ±0.2 V and about 160 mA (Hanwei datasheet, p. 1). The 3V3 pin is too low for the heater.
Why does the MQ-2 need a voltage divider on the Pico?
AOUT swings up to the module's 5 V, and the Pico's ADC pins must stay within about 300 mV of its 3.3 V rail (Pico datasheet, p. 19). 10 kΩ in series with 20 kΩ to GND brings 5 V down to 3.33 V.
Why is the MQ-2's DOUT not connected on the Pico?
The module pulls DOUT up to 5 V, too high for a Pico pin. The sketch raises its alarm when AOUT passes ALARM_PERCENT instead, which you can tune in code rather than with the trimmer.
Keep going
This guide as plain Markdown. /components/mq2-gas-sensor/raspberry-pi-pico.md