Air Quality with MQ-135 and Wemos D1 mini ESP8266

Measure Air quality with MQ-135 and ESP8266 Wemos D1 mini

As the MQ135 is not  really suited as a CO2 sensor (See my previous blog) and I still wanted to use it, I will use it as an air quality probe on an ESP8266.

As the title mentioned I will use an ESP8266 connected to my local WiFi router for this to send the data to of the Wemos d1 mini ESP8266 module
The ESP8266 I use is the Wemos D1 mini. The Wemos D1 mini has an USB interface and can be programmed with the normal Arduino GUI. You can add the board to it. See this article for how to add this board to the GUI.

I also use an DHT22 (AM2302) in this project to measure temperature and dht22 connectionshumidity. The DHT22 data pin is connected to pin D5 of the ESP8266 in my project. VCC is connected to the 5V of the ESP, GND is connected to GND.  You can of course any digital pin you want in your project. Just change line “#define DHTPIN D5” with the pin number you want.

For the MQ-135 you can only connect it to pin A0, as this is the only analog pin on the ESP8266.

For the air quality I store the lowest measured value in the EEPROM, so anything worse/higher that this best value is bad air quality. I use variable a1 & a2 & a3 to check if it is an new ESP8266, if these value in EEPROM are different than the stored values I presume that it is a new ESP and then set the lowest value to 510, if the values are the same I do not change the lowest value in EEPROM.

Below the program I use for this project.
I will try to describe any main step of it.

As there are some problems copying the below text I have added download links at the bottom of this page.

If you only want to use the MQ135 without the DHT22 then use the code below.

Download links.

Microsoft Explorer and Microsoft Edge seems to put this on one unusable line.
But Firefox and Google Chrome are both working.


shop at aliexpress

11 thoughts on “Air Quality with MQ-135 and Wemos D1 mini ESP8266”

  1. Please help me. I’m a beginner.
    Unfortunately, the code does not work. I can upload it without error, but it is not connected to the router. I replaced the semicolons because they did not abandon the Arduino IDE.
    Do not know where the bug is?

    1. Hi Jozsef,

      Lets try it without the multi SSID’s

      Just above the “WiFi.begin(ssid, password);” add two lines:
      ssid = ssid1;
      password = password1;
      WiFi.begin(ssid, password);

      Change the ssid1 and password1 with the login values of your router.


  2. Hello,
    This is Bhavana, I have stated this project directly with NodeMCU8266 I’m also facing the same problem can you help me as soon as possible.


  3. Hi,
    what this value means ? “int s = 25; ”
    Can i change it? or how do i know which value is ok for me?


    1. Hi Dani,

      It is used in a loop. the loop now runs 25 times and measures the air quality. All measurements are added to one total and then divided again thru 25. So the outcome is the average of the 25 measurements.
      You can change the number, but better not make it smaller, otherwise it will fluctuate much more.
      Making s bigger is no problem.


  4. Hello,
    If I understand correctly the project look at deviation of reading right?
    If it is the case how can I know the initial readings are not already base on actual bad air quality?
    is there a standard value of minimum air quality I can use a minimum reference?
    By the way, it work righ away when I tried it.

    1. Hi Eric,

      The script is storing the lowest ever measured value in flash.
      So take the meter out with an powerbank to the woods or farmlands.
      You can presume that the air will be almost clean there.
      The value will than be stored.
      So at home you can see how ‘bad/good’ the air quality is in reverence to the woods/farmlands.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.