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


Measure CO2 with MQ-135 and Arduino Uno

Measure CO2 with MQ-135 and Arduino

I had bought 3 MQ-135 gas sensors on AliExpress to test if it is possible to measure CO2 with them.

First I started with a very simple analog read to check the values in my computer/hobby room with a CO2 ppm around 650.


24 hours Burn-In

I connected the 5V power to the sensors and let them alone for 24 hours to burn in.

After these 24 hours I checked the values measured with the above little test program.
The values were 13 – 32 – 55
Breathing on them gave very little difference, as the values were only doubled to 28 – 61 – 103

With these values you can say that the first two are useless to measure CO2 as the difference is to little.
13 – 28 for CO2 ppm of about 500 – 2000 gives a resolution of about 100ppm/value measurement
32 – 61 gives about 52ppm/value
55 – 103 gives the best resolution of about 31ppm/value

These resolutions are valid if the scale is lineair, but the scale is logirithmic.
So in the lower ppm part the resolution is much better, but above the 1000ppm the resolution will be very low.

Wrong hardware

Found out that the resistor towards ground was just 1K ohm, after replacing the resister with one 22K ohm the results were getting much better.

In my room s the real ppm was about 770 ppm according to my NETATMO.

The raw value measured with the arduino was now 241.
Using MQ135-master from G.Krocker site and modifying MQ135.h with the correct RLOAD resistor value of 22K and a RZERO of 879.13

and using the below Arduino code

The Arduino sends out the following output to the serial port.

With these values I am very close to the values of the NETATMO
I will of course also run the calibration outside where it should show about 400ppm, and eventually adjust the RZERO in MQ135.h

Next day

The next morning ppm was down to 500 according to the NETATMO, but the arduino showed a ppm of 600. I change the RZERO to 819 and the arduino also showed 500.

This is no good of course, so something must go wrong in the calculations in the MQ135 library. Or I am using a MQ-135 sensor with a bad response curve.

The next day I have tested the same with another MQ-135, but the results were about the same.

Wrong formula in the MQ135.h library

The formula to calculate the resistance of the sensor is wrong.
It should read:

MQ135 not suitable for CO2

I think that I will stop my attempts for using the MQ-135 as a CO2 meter. With 1 sensor I measured different voltages on the analog port with same amount of CO2. It probably is to responsive to other gases in my surroundings. (Airport and highway).
One evening while my wife was one floor lower and took some perfume the ppm went sky high from 640ppm to 5570ppm, and then slowly (30 minutes) went down again.

I think that I will connect it to an ESP8266E to measure the outside air quality and sent the data over WiFi to .