tutorialspinouttools
Sensors · ESP32-C3

MQ-135 on OLED: Air Quality

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

Overview & Wiring

What it is: The MQ-135 is a general air-quality sensor sensitive to CO2, ammonia, benzene, smoke and other pollutants. It's the popular "air quality index" hobby sensor. How it works: A heated element whose resistance falls as pollutant concentration rises, read as an analog voltage on AO. This sketch shows a relative 0–100 air-quality index plus raw ADC (a real ppm figure needs calibration against clean air). Wiring: MQ-135 AO -> GPIO0 MQ-135 VCC -> 5V MQ-135 GND -> GND OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 VOLTAGE WARNING: divide AO to under 3.3V for the ESP32-C3 ADC. Libraries: only "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: relative air-quality level (0–100, higher = dirtier) and raw ADC. Breathe on it to see it climb. Gotchas: Needs burn-in and warm-up. For calibrated CO2/ppm use a dedicated NDIR sensor like the MH-Z19 (tutorial 21) — the MQ-135 is a relative indicator.

Circuit Diagram

MQ-135 on OLED: Air Quality 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

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

void loop() {
  int raw = analogRead(MQ);
  int aqi = map(raw, 0, 4095, 0, 100);

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("MQ-135 Air");
  display.setTextSize(3);
  display.setCursor(0, 18);
  display.printf("%d", aqi);
  display.setTextSize(1);
  display.setCursor(0, 52); display.printf("raw %d", raw);
  display.display();

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