Use Coupon WELCOME100 for රු100.00 Off!
Eleberric
Back to Blog
IoT & Wireless Automation

Building an IoT Air Quality Monitor with ESP32 and MQ-135

June 4, 2026
11 min read
Building an IoT Air Quality Monitor with ESP32 and MQ-135

Air pollution and greenhouse gases inside homes represent a critical concern. In this guide, we will configure an MQ-135 Air Quality Sensor alongside an ESP32 to monitor harmful gas levels and publish real-time parameters.

Introduction & Concept

The MQ-135 is a metal oxide semiconductor gas sensor highly sensitive to ammonia, nitrogen oxide (NOx), alcohol vapor, benzene, carbon monoxide, and carbon dioxide. Its internal heating element is heated up by 5V. As toxic gases interact with the hot sensor surface, chemical resistance drops. This creates a rising analog output voltage representing the air toxicity level.

Required Components

Component Name Purpose Store Link
ESP32 WROOM-32 Board Processor board with built-in 2.4GHz Wi-Fi for logging data. Buy ESP32 Board
MQ-135 Gas Sensor Sensitive to NH3, NOx, Alcohol, Benzene, Smoke, and CO2. Buy MQ-135 Sensor

Wiring Connection Schematic Diagram

       [ESP32 Board]                  [MQ-135 Sensor]
     +---------------+              +-----------------+
     |          VIN  |------------->| VCC (Requires 5V)
     |           GND |------------->| GND             |
     |  GPIO 34 (ADC)|<-------------| Analog Out (AO) |
     +---------------+              +-----------------+
          

Calibration & Preheating

Important Tip: Standard metal oxide gas sensors require a preheat period of 24-48 hours prior to initial use to burn off moisture on the heating filament. For standard runs, allow at least 3-5 minutes of warm-up before relying on readings.

Firmware Code

const int mqPin = 34; // ADC channel on ESP32

void setup() {
  Serial.begin(115200);
  pinMode(mqPin, INPUT);
}

void loop() {
  int rawValue = analogRead(mqPin);
  float voltage = (rawValue / 4095.0) * 3.3; // Convert digital reading to voltage
  
  Serial.print("Raw ADC Level: ");
  Serial.print(rawValue);
  Serial.print(" | Volts: ");
  Serial.println(voltage);
  
  delay(1000);
}

Ready to build this project?

Shop all the required components at Eleberric Sri Lanka with fast island-wide delivery.