Recently on social media, especially X (formerly known as Twitter), there is quite some hype on air quality control devices to understand when to open the windows and let some fresh air in…I took the chance to dig a bit into the topic to learn something new and understand how these sensors work. I got so interested that I decided to build one myself and see if I could connect it to Home Assistant to collect data and maybe do new interesting things in the future. Let’s dive into it!
The problem
I wanted to add some basic air quality monitoring to my place and have that integrated into Home Assistant so that I can collect data and if needed automate actions based on that. A fireplace is the main heating system we use for heating the house (it is in the open space at ground floor where we spend most of our time during the day) so monitoring CO2 is not a bad idea in the end.
The solution
By the end of this newsletter the goal is to build a basic prototype of CO2 sensor that can be connected to Home Assistant and provide information about air quality of the room where it will be placed. Later on the data generated (level of CO2) could be used as a trigger for automations.
Main components for this DIY project are two:
ESP32 development board: The ESP32 is a series of low-cost and low-power System on a Chip (SoC) microcontrollers developed by Espressif that include Wi-Fi and Bluetooth wireless capabilities and dual-core processor
SCD40 sensor: it’s a miniature CO2 sensor that builds on the photoacoustic NDIR sensing principle and Sensirion’s technologies to offer high accuracy at an unmatched price and smallest form factor
I decided to go with the SCD40 because of the small form factor and its accuracy in measurement thanks to the photoacoustic non-dispersive infra-red principle (NDIR) it uses. Before diving into the DIY part I think it is important to explain a bit more on the types of sensors available on the market for these projects and why some are better than the others. When looking to final products (like the fancy sensors with display you might have gotten into on social media lately) you will basically see them based on three types of sensors:
TVOC sensors (Total Volatile Organic Compound) estimate CO2 via organic components in the air. Problem is that other indoor sources (deodorizers for example) can add components to the air and and thus the estimations might be affected;
Transmissive NDIR sensors typically feature an IR (infra-red) emitter and an optical detector, such as a photodiode, at opposite ends of a specially designed optical cavity. The optical detector measures the amount of IR light energy that is not absorbed by (i.e., transmitted through) the gas sample. As the CO2 concentration in the optical cavity increases, the amount of light detected decreases. These sensors generally require a minimal optical path length (distance between the IR emitter and the photodiode) in the centimeter scale for enough IR absorption to occur to accurately measure lower CO2 concentrations so they are bigger than the others and this;
Photoacoustic NDIR sensors detect the amount of energy that is absorbed by CO2 molecules. When pulsing the IR emitter, CO2 molecules absorb infra-red light periodically. This causes additional molecular vibration resulting in a pressure wave inside the measurement chamber. The higher the CO2 concentration, the more light is absorbed, and thus the greater the amplitude of this acoustic wave becomes. A microphone inside the gas chamber measures this, from which the CO2 concentration can then be calculated. The emitter and the microphone can stay much closer and allow for greater miniaturization of the measurement chamber which results in a smaller sensor.
Going back to the DIY part I started with flashing the ESP32 development board using the ESPHome add-on available in Home Assistant. First basic firmware is installed on the board and allows you to connect it to the wi-fi so it will automatically show up in Home Assistant’s UI. In this case the development board needs to be connected to a computer with a cable while all future updates can be installed wirelessly (so no need to move the device from where you’re going to place it).
At this point the ESP32 is connected to HA but does not play any relevant role just yet. So with the help of a breadboard and some jumper wires I connected the SCD40 sensor to the ESP32 development board. Keep reading to know how you can do it.
Thanks to the I2C (Inter-Integrated Circuit) bus only 2 wires are needed for the data transmission connecting SDA (serial data line) and SCL (serial clock line) pins of the sensor to the dedicated pins of the ESP32 respectively 21 (GPIO21) and 22 (GPIO22). For powering the connections needed are between VCC to 3V3 pins and GND to GND. For the moment just do a leap of faith about the pins used for the connection, if you want a more detailed description of the pins out of the ESP32 you can check this nice image from Espressif.
The resulting prototype is like this at the moment with red wire for VCC-3V3, black for GND-GND, blue for SDA-GPIO21 and orange for SCL-GPIO22.
At this point the sensor and the development board are wired properly so we only need to tell the ESP32 about that and explain where the SCD40 is connected. For doing that it is convenient to use again the ESPHome add-on since it allows to edit directly the configuration in a yaml file to add the following sections:
i2c:
sda: 21
scl: 22
scan: True
sensor:
- platform: scd4x
co2:
name: "CO2"
temperature:
name: "Temperature"
id: scd40_temperature
humidity:
name: "Humidity"
id: scd40_humidity
The i2c section defines the mapping of the wiring explained previously and the sensor section defines the type of sensor connected (see the platform) and the additional configuration variables for the CO2, temperature and humidity included in the SCD40.
After saving the yaml file and clicking on the install button the ESPHome add-on will compile the firmware and transfer it over the air to the ESP32 that will finally start to send data to Home Assistant.
Considering that values between 400 and 600 ppm are excellent and anyway good until 800 ppm I can consider myself satisfied with the readings we get during the day. Increases correspond to the periods where everyone is home producing more CO2 simply breathing.
Next steps will be improving the prototype and setting up some automations based on the data collected by the sensor so that air circulation is privileged in order to keep CO2 levels low. Don’t miss future issues where I’ll send out updates on this project subscribe now!
I am also active on X where I post smaller progress updates on these projects. If you want to follow me you can find me by the handle @dlondero.
Until next time!
Daniel