This ESPHome configuration builds firmware for a DIY indoor air quality monitor. It monitors:
- CO₂ concentration
- PM2.5 and PM10 concentration
- temperature, humidity and pressure
It shows feedback for the current air quality with an RGB LED: green if the air quality is good, yellow if it's acceptable, and red if it's bad.
You can build this on a perfboard in a project box like this:
- ESP8266 or ESP32 board
- Winsen MH-Z19B CO₂ sensor
- Nova Fitness SDS011 particulate matter (PM) sensor
- Bosch BME280 breakout board (the 3.3 V version)
- Common cathode RGB LED (or separate red, green and blue LEDs)
- 220 Ω resistor and two 47 Ω resistors
- ESPHome
I have tested this code with:
- the NodeMCU v2 ESP8266
- the ESP32-DevKitC V4
Here are the connections to the pins of these boards:
Component | NodeMCU ESP8266 | ESP32-DevKitC V4 | |
---|---|---|---|
BME280 SCL | D1 | GPIO21 | |
BME280 SDA | D2 | GPIO22 | |
MH-Z19B TX | D4 | GPIO35 | |
MH-Z19B RX | D5 | GPIO32 | |
SDS011 TX | D3 | GPIO34 | |
SDS011 RX | D6 | GPIO33 | |
LED red | D7 | GPIO5 | |
LED green | D8 | GPIO17 | |
LED blue | TX | GPIO16 |
Make sure to connect the power and ground connections too. The BME280 needs 3.3 V, the MH-Z19B and SDS011 need 5 V.
The red component of the RGB LED needs a current-limiting resistor of 220 Ω, while the other two color components need a 47 Ω resistor.
This is a modular ESPHome configuration split up in various YAML files that you can import as packages. You can find these in the directory common:
- aqi.yaml
- Computes the air quality index (AQI) value (good, acceptable, bad) based on the current CO₂ concentration and the 24-hour averages of the PM2.5 and PM10 concentrations. This value is published as a text sensor and shown as a color (green, yellow, red) on the RGB LED.
- base.yaml
- Sets up the basic ESPHome functionality for the board, including Wi-Fi, a captive portal, logger, Home Assistant API and OTA support. It also sets the threshold values for the CO₂, PM2.5 and PM10 concentrations, as well as the messages when the air quality is good, acceptable or bad.
- bme280.yaml
- Sets up the BME280 sensor for temperature, humidity and pressure and the I²C bus it uses.
- mh-z19b.yaml
- Sets up the MH-Z19B CO₂ sensor, a binary sensor that shows whether the sensor has been calibrated yet (and sets the LED to blue when it isn't) and a switch to calibrate the sensor.
- rgb_led_esp32.yaml
- Sets up the RGB LED on the ESP32 with its LEDC peripheral (a hardware PWM).
- rgb_led_esp8266.yaml
- Sets up the RGB LED on the ESP8266 with software PWM.
- sds011.yaml
- Sets up the SDS011 PM sensor.
- secrets.yaml.example
- Contains the secrets used in this ESPHome project. Copy this file to a file
secrets.yaml
in thecommon
directory and enter your Wi-Fi, API and OTA credentials.
To use this configuration, create a YAML file with:
- substitutions for all pin numbers used by the components, your device's name, platform and board and parameters like update intervals.
- packages that include the relevant YAML files in the
common
directory.
There are two example configurations in this repository:
- esp8266_example.yaml for the NodeMCU v2 ESP8266
- esp32_example.yaml for the ESP32-DevKitC V4
After this, flash the firmware to your device, e.g. with:
esphome run esp32_example.yaml
After you have added your device to Home Assistant's ESPHome integration, the air quality measurements are available in Home Assistant and you can start the calibration of the CO₂ sensor from within Home Assistant too.
If you successfully created a configuration for another ESP8266 or ESP32 board, please contribute this configuration with a pull request.
Thanks to the modularity of the code, it shouldn't be that difficult to create a variant of this project with other sensors. Here are some suggestions:
- Change the temperature, humidity and pressure sensor
- This sensor is currently not referenced in the other YAML files, so you can just add a configuration file for another sensor, disable the package for the BME280 and add a package for the other sensor.
- Change the CO₂ sensor
- Make sure you give the CO₂ value of your sensor the ID
co2_value
and create a binary sensor for its calibration state with IDco2_calibrated
. - Change the RGB LED
- You can swap the classical RGB LED for another light, as long as it has the ID
led_rgb
and it's a light with platformrgb
(with red, green and blue components). - Change the PM sensor
- Make sure you give the PM2.5 and PM10 values of your sensor the IDs
pm2_5_value
andpm10_value
.
With these changes, the rest of the code should still work.
If you successfully created a customization, please contribute this with a pull request, ideally with an example configuration.
More complex customizations could be supported in future versions of this project.
If you want to learn more about ESPHome, read my book Getting Started with ESPHome: Develop your own custom home automation devices and the accompanying GitHub repository koenvervloesem/Getting-Started-with-ESPHome.
This project is provided by Koen Vervloesem as open source software with the MIT license. See the LICENSE file for more information.
The C++/runtime codebase of the ESPHome project (file extensions .c, .cpp, .h, .hpp, .tcc, .ino) are published under the GPLv3 license. The Python codebase and all other parts of the ESPHome codebase are published under the MIT license. See the ESPHome License for more information.