HomeKit
ESP8266 HomeKit Air Quality Sensor with an MQ135
A NodeMCU with an MQ135 gas sensor and a DHT22 as an air quality, temperature and humidity sensor in Apple Home, built as a test setup.
- ESP8266
- NodeMCU
- 3 min read
An air quality sensor for Apple Home, built on a NodeMCU (ESP8266) with an MQ135 gas sensor and a DHT22. HomeKit gets three things from it: the air quality, the temperature and the humidity.
It is a test setup, made to see which values the sensors can measure. Air quality is an interesting subject to build with.
What the MQ135 measures
The MQ135 is a semiconductor gas sensor. Its sensing material, tin dioxide (SnO2), conducts poorly in clean air and conducts better as the concentration of polluting gases goes up. The manufacturer’s manual (Winsen, version 1.4) gives these figures:
| Property | Value |
|---|---|
| Target gases | Ammonia, sulfide and benzene series vapour. It also responds to smoke and other gases |
| Detection range | 10 to 1000 ppm, for ammonia, toluene, hydrogen and smoke |
| Heater voltage | 5.0 V ±0.1 V, AC or DC |
| Heater power | 950 mW at most |
| Circuit voltage | 24 V DC at most |
| Preheat | Over 48 hours in the standard test |
The sensor does not name one gas. It gives a single value for the mix of gases in the air, so it is a good indicator of a change in air quality and not a meter for one substance. The manual describes the response as a resistance ratio, Rs/R0, where R0 is the resistance in clean air. A reading in ppm therefore needs a calibration in clean air.

Handling the sensor
The manual has several warnings that matter for this build:
- Let it age. A sensor that was stored without power needs aging with the heater on before its readings are stable: at least 48 hours after less than a month of storage, 72 hours after one to six months and 168 hours after more than six months. Expect drifting values in the first days.
- Keep it away from silicone. Vapour from silicone sealant, glue or plastic with silicone makes the sensor lose its sensitivity for good.
- Keep it dry. Water spray, condensation and freezing lower the sensitivity.
- Do not exceed the voltage. A higher voltage on the sensor can damage the heater. On the bare six-pin sensor, pins 2 and 5 are the heater and voltage on the wrong pins can break the leads. On the module this is already wired for you.
Wiring
The module has four pins: 5 V, GND, a digital output and an analog output. The diagram uses the analog output.

The analog output of the module can reach 5 V, and the analog input A0 of the NodeMCU accepts 3.3 V at most. A voltage divider of 10 kΩ and 2.5 kΩ sits between the two, so the input is never overloaded.


The DHT22 (AM2302) for temperature and humidity is connected with three wires and runs on 3.3 V. For its specifications, see the ESP8266 HomeKit Temperature and Humidity Sensor.
Flash and install
The installation is the same as for the other ESP8266 HomeKit accessories on this site. The firmware runs on ESP-Open-RTOS with Life Cycle Manager 2, which sets up the WiFi and installs updates over the air. The full steps, from installing esptool to flashing, joining the LCM- WiFi network and pairing, are in ESP8266 HomeKit Blinds.
In the portal, use these values:
| Field | Value |
|---|---|
| OTA repository | AchimPieters/ESP8266-HomeKit-Air-Quality-Sensor |
| OTA binary file | main.bin |
When the installation is done, scan the QR code the device generates in the Home app to add the sensor.
Releases
| Version | Date |
|---|---|
| 0.0.2 | 4 February 2021 |
| 0.0.1 | 3 February 2021 |
Each release has a main.bin and a signature file, main.bin.sig. The repository holds the releases and the images and no source code, so how the reading of the MQ135 is turned into the air quality levels of HomeKit is not documented here.
Good to know
- The HomeKit Accessory Protocol specification the firmware follows is the non-commercial version, for accessories you build yourself and do not distribute or sell.
- The repository is under the MIT licence.
- Do not use it as a safety device. The MQ135 gives an indication of air quality and is not a certified gas alarm.
The project is in the ESP8266-HomeKit-Air-Quality-Sensor repository on GitHub.
More HomeKit
Keep building.
Switches, sensors, plugs and blinds: more accessories that you build yourself and add to the Home app.
All HomeKit
