tutorialspinouttools
Sensors · ESP32-C3

MQ-2 on OLED: Smoke & LPG Gas

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The MQ-2 is a low-cost gas sensor that responds to smoke, LPG, propane, methane and hydrogen. It's the classic "gas leak / smoke alarm" hobby sensor. How it works: A heated tin-dioxide element changes resistance when combustible gas touches it. You read that as an analog voltage on AO — higher = more gas. This sketch shows a raw 0–4095 reading mapped to a 0–100 relative level (true ppm needs per-gas calibration). Wiring: MQ-2 AO -> GPIO0 (analog out) MQ-2 VCC -> 5V (the heater needs 5V — 3V3 won't heat it properly) MQ-2 GND -> GND OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 VOLTAGE WARNING: AO can swing up to ~5V, but the ESP32-C3 ADC only tolerates 3.3V. Add a divider on AO (e.g. 10k from AO to GPIO0 and 15k from GPIO0 to GND) so the max stays under 3.3V, or use a level-safe module. Libraries: only "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: a relative gas level (0–100) and the raw ADC value. Wave a lighter (unlit, gas only) nearby to see it jump. Gotchas: The heater needs 24–48 hours of initial burn-in for stable readings, and a few minutes of warm-up each power-on. Numbers are relative, not calibrated ppm — good for "is there gas?" alarms, not precise measurement.

Circuit Diagram

MQ-2 on OLED: Smoke & LPG Gas wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);

#define MQ 0                  // AO on GPIO0 (via a divider to stay <= 3.3V)

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}

void loop() {
  int raw = analogRead(MQ);                   // 0..4095
  int level = map(raw, 0, 4095, 0, 100);      // relative 0..100

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("MQ-2 Smoke/LPG");
  display.setTextSize(3);
  display.setCursor(0, 18);
  display.printf("%d", level);
  display.setTextSize(1);
  display.setCursor(0, 52); display.printf("raw %d", raw);
  display.display();

  Serial.printf("Gas level: %d  (raw %d)\n", level, raw);
  delay(400);
}
Browse all tutorials ESP32-C3 pinout Calculators