DIY-Projects

Air Quality Monitor using MQ-135 and DHT11

automated-irrigation-system

Indoor air can be worse than you think. Cooking smoke, fumes, dust and poor ventilation all build up without any smell you notice. In this guide you will build an air quality monitor that reads harmful gases with an MQ-135 sensor, measures temperature and humidity with a DHT11, shows everything on an LCD and sounds a buzzer when the air gets unhealthy. It uses an Arduino Uno, an MQ-135 gas sensor and a DHT11 temperature and humidity sensor.

You get the full parts list, wiring tables, ready-to-upload code, troubleshooting tips and upgrade ideas. New to Arduino? Read our essential Arduino sensors guide first, or browse more Arduino mini projects.

🎯 LevelBeginner
⏱️ Build timeAbout 1 hour
🧩 BoardArduino Uno or compatible
🧠 You will learnGas sensors, DHT11, I2C LCD, alarms

How an air quality monitor works

The monitor repeats three steps every second:

👃

1. Sense

The MQ-135 reacts to gases such as smoke, ammonia and CO2 and gives an analog value. The DHT11 measures temperature and humidity.

🧠

2. Think

The Arduino compares the gas reading with its clean-air baseline and turns the difference into an air quality index (AQI).

🔔

3. Act

The LCD shows the AQI, temperature and humidity. If the air is unhealthy, the buzzer turns ON. If it is normal, the buzzer stays OFF.

Components required

🧩

Arduino Uno or compatible board

The brain of the monitor. It reads both sensors, drives the LCD and controls the buzzer.

🌫️

MQ-135 gas sensor module

Detects harmful gases and air pollution. Its analog output AO goes to the Arduino.

🌡️

DHT11 temperature and humidity sensor

Measures the room temperature and the relative humidity.

🖥️

16x2 I2C LCD display

Shows the AQI, temperature and humidity. The I2C backpack needs only four wires.

🔔

Active buzzer (5V)

Sounds the alarm when the air quality is unhealthy. An active buzzer beeps by itself when powered.

Buy it Buzzers
🔌

Male to female jumper wires (10)

Connect the sensors and the buzzer to the Arduino pins.

🔗

Female to female jumper wires (4)

Used for the I2C LCD connections to the Arduino header pins.

Wiring: pin connections

Use these pins so the code below works without changes. All grounds (GND) must be joined.

Air quality monitor circuit diagram: Arduino, MQ-135 gas sensor, DHT11, I2C LCD and buzzer

Circuit diagram: Arduino, MQ-135, DHT11, I2C LCD and buzzer. The diagram uses a generic Arduino board symbol, so wire to an Arduino Uno as in the tables below.

MQ-135 to Arduino

MQ-135 pinArduino pin
VCC5V
GNDGND
AOA0

DHT11 to Arduino

DHT11 pinArduino pin
VCC5V
GNDGND
DATAD2

I2C LCD to Arduino

LCD pinArduino pin
VCC5V
GNDGND
SDAA4 (SDA)
SCLA5 (SCL)

Buzzer to Arduino

Buzzer pinArduino pin
Positive (+)D3
Negative (−)GND

Always connect every GND to the Arduino GND. Wiring problems cause most failures, so see wrong sensor connections if something does not work.

Air quality levels

Index (AQI)Air qualityBuzzerShown on LCD
0 – 50GoodOFFGOOD
51 – 150ModerateOFFMODERATE
151 – 300UnhealthyONBAD
301 – 500Very unhealthyONVERY BAD
501 and aboveHazardousONHAZARD (backlight blinks)

The AQI here is a simple relative index worked out from the MQ-135 reading, not a lab-grade measurement. Values depend on sensor calibration, temperature, humidity and the room, so adjust the thresholds in the code for your environment.

Infographic: working, connection table, components required, LCD display examples and air quality index table for the Arduino MQ-135 and DHT11 air quality monitor

Step-by-step assembly

Step 1

Place the sensors

Fit the MQ-135 and DHT11 where air can reach them. Do not cover the MQ-135 or put it inside a closed box.

Step 2

Wire the MQ-135

Connect VCC and GND to 5V and GND, and the AO pin to A0.

Step 3

Wire the DHT11

Connect VCC and GND to 5V and GND, and the DATA pin to D2.

Step 4

Wire the LCD

Connect VCC, GND, SDA and SCL of the I2C LCD to 5V, GND, A4 and A5.

Step 5

Wire the buzzer

Connect the buzzer positive to D3 and the negative to GND.

Step 6

Power up in fresh air

Connect the Arduino to USB power in a room with clean air. The code sets its clean-air baseline in the first seconds after power-on.

Arduino code

Open the Arduino IDE and install two libraries from Sketch > Include Library > Manage Libraries: LiquidCrystal I2C (by Frank de Brabander) and DHT sensor library (by Adafruit, with Adafruit Unified Sensor). Then paste the code, select your board and port, and click Upload.

Air quality monitor using MQ-135 and DHT11

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);   // try 0x3F if the LCD stays blank
DHT dht(2, DHT11);                    // DHT11 data pin on D2

const int GAS_PIN    = A0;
const int BUZZER_PIN = 3;

const int   WARMUP_SECONDS = 30;      // MQ-135 needs time to heat up
const float SCALE          = 1.0;     // raise to make the index more sensitive
const int   BUZZER_LEVEL   = 150;     // buzzer ON above this AQI
const int   HAZARD_LEVEL   = 500;     // backlight blinks above this AQI

int baseline = 0;                     // clean-air reading
bool blinkState = true;

int readGas() {
  long total = 0;
  for (int i = 0; i < 10; i++) {
    total += analogRead(GAS_PIN);
    delay(10);
  }
  return total / 10;
}

void printLine(int row, const char* text) {
  lcd.setCursor(0, row);
  lcd.print(text);
  for (int i = strlen(text); i < 16; i++) lcd.print(' ');
}

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  dht.begin();
  lcd.init();
  lcd.backlight();

  printLine(0, "Warming up...");
  for (int s = WARMUP_SECONDS; s > 0; s--) {
    char msg[17];
    snprintf(msg, sizeof(msg), "Fresh air: %ds", s);
    printLine(1, msg);
    delay(1000);
  }
  baseline = readGas();               // clean-air reference
  lcd.clear();
}

void loop() {
  int gas = readGas();
  int aqi = (int)((gas - baseline) * SCALE);
  aqi = constrain(aqi, 0, 999);

  const char* label;
  if      (aqi <= 50)  label = "GOOD";
  else if (aqi <= 150) label = "MODERATE";
  else if (aqi <= 300) label = "BAD";
  else if (aqi <= 500) label = "VERY BAD";
  else                 label = "HAZARD";

  digitalWrite(BUZZER_PIN, aqi > BUZZER_LEVEL ? HIGH : LOW);

  if (aqi > HAZARD_LEVEL) {           // blink the backlight in hazardous air
    blinkState = !blinkState;
    if (blinkState) lcd.backlight(); else lcd.noBacklight();
  } else {
    lcd.backlight();
  }

  float t = dht.readTemperature();
  float h = dht.readHumidity();

  char line1[17];
  snprintf(line1, sizeof(line1), "AQI:%d %s", aqi, label);
  printLine(0, line1);

  if (isnan(t) || isnan(h)) {
    printLine(1, "DHT11 error");
  } else {
    char line2[17];
    snprintf(line2, sizeof(line2), "T:%dC H:%d%%", (int)t, (int)h);
    printLine(1, line2);
  }

  delay(1000);                        // DHT11 needs about 1 second between reads
}

How the code works

  • readGas() averages ten readings from A0 so the value is steady.
  • baseline is the clean-air reading taken after the warm-up. The AQI is how far the gas reading rises above it.
  • loop() picks the air quality label, switches the buzzer when the AQI is above 150 and blinks the LCD backlight above 500.
  • printLine() pads each line to 16 characters so old text never stays on the screen.

Calibrate and tune your monitor

🔥 Let the sensor burn in. A new MQ-135 gives better readings after it has been powered for several hours.
🌬️ Start in fresh air. The baseline is set at power-on, so reset the board in a clean room or near an open window.
🎚️ Change SCALE. A higher value makes the index react more to small changes in gas.
🔔 Change BUZZER_LEVEL. Lower it for an earlier alarm, raise it if the buzzer sounds too often.

Troubleshooting

ProblemWhat to check
The LCD is blank or shows blocksTurn the contrast screw on the I2C backpack, or change the address from 0x27 to 0x3F.
The LCD shows "DHT11 error"Check the DATA wire on D2 and the 5V and GND connections. Make sure the DHT library is installed.
AQI is high as soon as it startsThe sensor is still warming up or the room air was dirty at power-on. Wait and press reset in fresh air.
AQI never changesCheck the AO wire on A0 and raise SCALE. Test with a gas lighter held near the sensor, without lighting it.
The buzzer does not soundCheck the polarity on D3 and GND. Make sure it is an active buzzer.
The readings drift during the dayMQ-135 readings change with temperature and humidity. Reset the board in fresh air to set a new baseline.

Upgrade ideas

🚦

Add LED indicators

A green, yellow and red LED show the air quality at a glance.

🌀

Switch an exhaust fan

Use a relay to turn on a fan when the air turns unhealthy.

📶

Send readings over Wi-Fi

Use an ESP32 to see the air quality on your phone.

🕒

Log data with time

Add a real-time clock to record when the air quality changed.

More ideas: see our Top 20 STEM projects and 20 Arduino mini projects.

Frequently asked questions

What does the MQ-135 sensor detect?

The MQ-135 reacts to gases such as ammonia, smoke, benzene and carbon dioxide. It shows how the air changes but cannot tell you exactly which gas is present.

Is the AQI on the LCD accurate?

It is a relative index for learning and room monitoring, not a certified air quality measurement. For accurate results the sensor needs calibration against a known reference.

Why does the MQ-135 need to warm up?

The sensor has a small heater inside. It takes time to reach a stable temperature, and readings improve after a few hours of use.

Can I use an ESP32 instead of an Arduino Uno?

Yes. Change the pin numbers, use the ESP32 ADC pin for the MQ-135 and take care with its 3.3 V logic level.

Can I change the LCD colour with the air quality?

A standard I2C LCD has one backlight colour, so the code blinks the backlight in hazardous air. For colour alerts, add green, yellow and red LEDs.

Electronics learning with Robopro.in

At Robopro.in, students and makers can find Arduino boards, gas sensors, temperature sensors, displays and buzzers for building practical projects.

Ready to build your air quality monitor?

Get every part for this project in one place. Schools and STEM labs can also order in bulk.

Shop gas sensors Bulk orders for schools Contact us

Conclusion

An air quality monitor teaches sensing, calibration and alarm logic in one useful project. Once it works, you can add LED indicators, a fan or Wi-Fi and keep learning.

Keep reading

Leave a Reply

Your email address will not be published. Required fields are marked *