Digital room air sensor. Home Assistant connection via MQTT.
about the project
Adequate ventilation is important for health. The MQ-135 sensor monitors the room air. To put it more precisely: According to the manufacturer's data sheet, it monitors the room air for CO2, alcohol, benzene, NOx, and NH3.
The MQ-135 sensor is available in two versions:
The MQ-135 module for breadboards has its own logic and can therefore also output digital values. The module has a potentiometer to set the threshold value for the digital output. At the digital pin, the sensor outputs the value 1 (everything OK) or the value 0 (bad air) in the Arduino sketch via digitalRead, depending on whether the threshold value set on the potentiometer is exceeded.
If the room air is OK, our indicator LED lights up green. Should the room be ventilated, our indicator LED flashes red.
The calibration using the potentiometer is based on the subjective air quality perception. For example, by adjusting this so that the indicator LED shows green in fresh air (outside) and flashes red in an apartment that has not been ventilated for a long time.
The digital values are also output via MQTT and can thus be transferred to common smart home systems (such as Home Assistant).
The setup requires certain electronics and PC knowledge. A computer with an installed Arduino IDE is required to install the software.
Disclaimer: This is a tutorial for hobbyists who are enthusiastic about IOT. No responsibility can therefore be assumed for the functionality of the air quality measurement. The MQ-135 sensor itself gets warm and should therefore only be operated when you are present. In addition, the MQ-135 sensor has a certain burn-in time during which the values can still fluctuate.
The following components are required for assembly:
- MQ-135 sensor module
- WEMOS D1 Mini NodeMCU
- Breadboard, 400 pin
- 40 x jumper cable 10 cm, male male
- 5mm RGB LED, 4 pin, common cathode
- 3 x 220 ohm resistors
The complete set of components in our shop:
So that the "D1 Mini NodeMCU" can be soldered to the Leipterplatte, it must first be soldered to the PINs. Please make sure that the PINs are soldered on at right angles so that they later also fit the circuit board.
In the next step, all components are placed on the breadboard. The following Fritzing diagram gives a good overview.
The following photos show the process in detail.
The pins of the D1 Mini that are not required can be used to bend the resistors in order to obtain the desired spacing.
Putting the resistors in the breadboard is a little tricky. It's best to hold the resistors on the sides so that the wires don't bend too much. If necessary, correct the wires a little after inserting the resistors if they were bent during insertion.
Please make sure that the polarity of the LED is correct, otherwise the "D1 Mini NodeMCU "can be destroyed. The longest pin (cathode) is connected to the black jumper cable. The other three pins to the resistors.
Finally, the jumper cables are plugged into the breadboard.
If the components are incorrectly soldered and the NodeMCU is connected to the PC, this can damage the PC's USB port. Therefore, we first test by connecting the NodeMCU to a classic USB charger or a power bank with a USB cable. If nothing gets too hot there, that's a good sign.
Download and install the Arduino IDE
We need the Arduino IDE, which can be downloaded for free here: https://www.arduino.cc/en/Main/Software
It's even easier on Windows PCs, where the Arduino IDE can be installed via the Microsoft Store.
Add ESP 8266 board
First, add the following "Additional Board Administrator URL" to the default settings: http://arduino.esp8266.com/stable/package_esp8266com_index.json
To do this, we proceed as follows:
1. We call up the default settings:
2. We add the URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
Install "ESP8266" board
To do this, we navigate to "Tools -> Board -> Board Manager".
Now we search for "ESP8266" and install the board.
Select "Generic ESP8266 Module" as the board
To do this, we proceed as follows:
1. We navigate via "Tools -> Board -> ESP8266 Boards"
2. We select "Generic ESP8266 Module" as the board
Select upload speed
Under "Tools -> Upload Speed" we select 115200.
We need the following Arduino libraries:
The library "ESP8266WiFi.h "is automatically available by selecting the" Generic ESP8266 Module "as a board (see previous step).
We install the other two libraries as follows:
We can install the required libraries under “Tools -> Libraries”.
1. We search for "PubSubClient" and select Nick O'Leary's library and click on "install":
2. We search for "ArduinoJason" and select the Benoit Blanchon library and click on "install":
Copy the Arduino sketch from GitHub
Now we call up the following Arduino sketch from GitHub and copy it into a new sketch in our Arduino IDE using "Copy / Paste":
Now we have to add individual information to the Arduino sketch (see comments in the sketch), e.g. for
- WLAN access data
- MQTT server data
Please read all comments in the sketch carefully.
Upload the Arduino sketch to the NodeMCU
Now we connect our air quality sensor to our computer using a USB cable.
Then we select the relevant USB interface (e.g. COM3 or COM4) via "Tools -> Port".
Now we click on "Sketch -> Upload" and if there are no more errors in the sketch, it can be successfully uploaded and we can now use our "digital room air sensor".
As mentioned at the beginning of this tutorial, the calibration is carried out using the potentiometer on the MQ-135 module based on the personal subjective air quality perception.
The MQ-135 module itself has two small LEDs:
- Red LED: indicates that the MQ-135 module is in operation.
- Green LED: indicates that the sensor has triggered. So the air quality has reached the threshold value (bad air) set via the potentiometer.
For calibration you need on the one hand an "area with fresh air" (open window, balcony, garden) and on the other hand an "area with bad air" (e.g. a small room in which a person stayed for an hour and which was not ventilated during this time became).
Now you first expose the sensor to fresh air and turn the potentiometer to the right so that the green LED on the sensor is off. The indicator LED on the breadboard should now light up green.
Then you take the sensor into the room with bad air and turn the potentiometer very slowly to the left until the green LED on the sensor comes on. The indicator LED on the breadboard should start blinking red.
Now you leave the room with bad air and expose the sensor to fresh air. Now the green LED on the sensor should go out again and the indicator LED on the breadboard should light up green.
Now you switch between "Area with fresh air" and "Area with bad air" until you have correctly adjusted the desired threshold value.
Since the MQ-135 sensor initially needs up to 48 hours of burn-in time, a waiting time and / or recalibration is necessary during or after this time until the display works as desired.
Please note: this is a tutorial for hobbyists who are enthusiastic about IOT. Therefore, playfulness (can I manage to trigger the sensor? Does the indicator LED react? Will the data be transmitted via MQTT?) Should be in the foreground. The structure is not suitable for professional indoor air monitoring, where e.g. health or medical factors are in the foreground (there are special, professionally calibrated measuring devices from other providers).
Connection to smart home system
The NEOE IOT kits are developed and tested for home assistants, as this is the smart home system we currently prefer due to the extensive and well-engineered functionalities. For more information on Home Assistant, see https://www.home-assistant.io.
For the basic setup of Home Assistant, see https://www.home-assistant.io/getting-started/.
We designed the Arduino Sketch to support Home Assistant's MQTT Discovery functionality. As a result, the sensor is automatically recognized by Home Assistant and the data transmitted via MQTT can be displayed directly in the dashboards. To activate the MQTT Discovery function in Home Assistant, see https://www.home-assistant.io/docs/mqtt/discovery/.
We recommend "Button" map for display in Home Assistant because it is nice and large and optically fits the digital sensor well.
If the room air is OK, a white tick is displayed in front of a blue background:
If there is ventilation, a white exclamation mark is displayed on a yellow background:
Other smart home systems
Thanks to the use of the MQTT protocol, it should also be possible to connect to all other smart home systems that support MQTT. However, due to the large number of different systems, we cannot guarantee this. To connect to other smart home systems via MQTT, please consult the documentation for the respective smart home systems.
Depending on the system status, the indicator LED changes as follows:
- System start: blue
- Indoor air OK: green
- Time to ventilate: red (flashing)
Further possible expansion stages
Whenever the sensor reports "thick air", you could use the Home Assistant to control ventilation so that it automatically ventilates the room until the air quality in the room is okay again.
Frequently asked questions
The indicator LED does not light up at all
- Have the WLAN access data been stored correctly? Attention: upper and lower case is important.
The Arduino program code does not work and an error message appears in the Arduino IDE
- Automatic translation from English to German is activated in some browsers. As a result, the Arduino program code on GitHub is automatically translated into German and is therefore displayed incorrectly. In this case, please deactivate the automatic translation before copying and transferring the Arduino program code.
Disclaimer - all information without guarantee:
The information contained in this tutorial (contribution) has been researched and compiled to the best of our knowledge and belief. However, mistakes can happen to us too. And something can also go wrong during the implementation of the tutorial or the content can be misunderstood. We cannot therefore accept any liability for any damage caused by following this tutorial. We are continuing to develop our tutorials. If something is inconsistent or unclear, please let us know so that we can correct or add to the point concerned. Thanks very much.