MQ-135 air quality sensor for Arduino

FunduinoLast updated on

Schadstoffe in der Luft messen und als einfache Ampel anzeigen

Der MQ-135 ist ein Gassensor, der auf eine ganze Reihe von Stoffen in der Raumluft reagiert – unter anderem auf Rauch, Alkohol, Ammoniak und Kohlendioxid. Er liefert keinen geeichten ppm-Wert, sondern eine Zahl, die steigt, wenn die Luft schlechter wird.

Genau daraus lässt sich etwas Praktisches bauen: In dieser Anleitung liest du den Sensor am Arduino aus, bestimmst deinen eigenen Frischluft-Referenzwert und baust daraus eine Ampel aus einer roten und einer grünen LED.

Back of the MQ-135 sensor module with blue potentiometer
Back of the MQ-135: use the potentiometer to set the threshold for the digital output.

You need this

MQ-135 Gassensor - Luftqualität, 10-1000 ppm, 5 V MQ-135 Gassensor - Luftqualität, 10-1000 ppm, 5 V 2,84 € *Item no. FUN-1010576About the product — MQ-135 Gassensor - Luftqualität, 10-1000 ppm, 5 V
Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino 13,90 € *Item no. F23107480About the product — Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino
Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei 2,90 € *Item no. FUN-1010553About the product — Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei
Breadboardkabel männlich-weiblich - 40 Stück, 20 cm Breadboardkabel männlich-weiblich - 40 Stück, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboardkabel männlich-weiblich - 40 Stück, 20 cm

All 4 parts together 21,38 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you’ll need:

  • 1× microcontroller with a suitable USB cable
  • 1× MQ-135 air quality sensor
  • 1× breadboard
  • Breadboard jumper wires

For the traffic light in the last step, you’ll also need:

  • 1× red LED and 1× green LED
  • 2× 220-ohm resistor

The connections of the MQ-135

The MQ-135 detects the concentration of various air pollutants and outputs it as an analog value. The module has four connections:

  • VCC – power supply, connect to the Arduino’s 5V pin
  • GND – connect to a GND pin on the Arduino
  • A0 – analog output, connect to pin A0 on the Arduino
  • D0 – digital output that only indicates whether a threshold has been exceeded

For this guide, you only need VCC, GND, and A0. Set the threshold for the digital output mechanically using the potentiometer on the back of the module.

Do not connect D0 to Arduino pin 0. Pin 0, together with pin 1, is part of the serial interface, which is also used to upload the sketch. If you want to use the digital output, use a free pin for it, such as pin 2.

Back of the MQ-135 sensor module with blue potentiometer
Back of the MQ-135: use the potentiometer to set the threshold for the digital output.

Burn in the sensor and determine the reference value

The MQ-135 uses a heated sensing element. A brand-new module therefore only provides stable readings after a longer burn-in period. For best results, leave the sensor continuously connected to the power supply for 24 to 48 hours.

Next, determine your personal reference value: Place the sensor in fresh air, wait a few minutes, and note the reading it displays. This value is your baseline for “good air.” In our setup, it was around 30 - yours may differ significantly.

The sensor element becomes noticeably warm during operation. This is normal. The MQ-135 measures a mixture of different gases and is not a calibrated measuring instrument—its readings are indicative, not ppm values.

Read sensor value

Upload the sketch to the microcontroller and open the Serial Monitor at 9600 baud. The current reading will appear there every second. To test it, briefly hold the sensor near a scent source, such as a felt-tip pen - the value will then increase noticeably.

Sketch 1: Read sensor value
int Sensorwert;

void setup()
{
  Serial.begin(9600);            // Baudrate 9600 festlegen, damit die Werte im Seriellen Monitor erscheinen.
}

void loop()
{
  Sensorwert = analogRead(A0);   // Der Wert am Pin A0 wird ausgelesen ...
  Serial.print("Der erfasste Wert lautet: ");
  Serial.println(Sensorwert);    // ... und im Seriellen Monitor ausgegeben.
  delay(1000);
}

Build a traffic light with two LEDs

Using the reference value from Step 3, you’ll now build a small monitoring station. Expand the setup by adding two LEDs, each with a 220-ohm resistor: connect the red LED to pin 12 and the green LED to pin 13.

If the measured value is 40 or higher, the red LED lights up for two seconds. If it is 39 or lower, the green LED lights up.

The threshold of 40 matches our reference value of around 30. If your own fresh-air reading is higher or lower, adjust the two numbers in the sketch accordingly - otherwise, the same LED will always light up.

Sketch 2: Air Quality Traffic Light with Two LEDs
int Sensorwert;
int LEDgruen = 13;               // Die gruene LED wird mit Pin 13 verbunden
int LEDrot = 12;                 // Die rote LED wird mit Pin 12 verbunden

void setup()
{
  Serial.begin(9600);            // Baudrate 9600 festlegen, damit die Werte im Seriellen Monitor erscheinen.
  pinMode(LEDgruen, OUTPUT);     // Die LEDs dienen als Ausgang
  pinMode(LEDrot, OUTPUT);
}

void loop()
{
  Sensorwert = analogRead(A0);   // Der Wert am Pin A0 wird ausgelesen ...
  Serial.print("Der erfasste Wert lautet: ");
  Serial.println(Sensorwert);    // ... und im Seriellen Monitor ausgegeben.

  if (Sensorwert >= 40)          // Ist der Sensorwert groesser oder gleich 40 ...
  {
    digitalWrite(LEDrot, HIGH);  // ... soll die rote LED ...
    delay(2000);                 // ... fuer 2 Sekunden aufleuchten ...
    digitalWrite(LEDrot, LOW);   // ... und danach wieder ausgehen.
  }

  if (Sensorwert <= 39)          // Ist der Sensorwert kleiner oder gleich 39 ...
  {
    digitalWrite(LEDgruen, HIGH); // ... soll die gruene LED ...
    delay(2000);                  // ... fuer 2 Sekunden aufleuchten ...
    digitalWrite(LEDgruen, LOW);  // ... und danach wieder ausgehen.
  }

  delay(1000);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.