Dries Buytaert

Building my own CO2 monitor

How I created a CO2 monitoring system for our bedroom and uncovered some surprising insights about air quality.

For years, I have worried about the CO2 levels in our kids' bedroom. Until recently, our two sons shared a small bedroom in our apartment. Every night, they insisted on shutting the door to block out light and noise. Yet, once they fell asleep, I'd quietly open the door to make sure they had enough fresh air to fuel their dreams.

As we breathe, our bodies naturally expel CO2 (carbon dioxide). When CO2 reacts with water within our body it becomes carbonate, which can subtly shift our body's internal balance. That is why high CO2 levels, like in sealed bedrooms, can be harmful.

Outdoor CO2 levels average around 400 ppm (parts per million), but indoor levels are considered healthy up to 800 ppm. Between 800 and 1200 ppm, minor discomfort may begin, and levels above 2,000 ppm indicate poor air quality, posing health risks.

A pivotal study by Harvard University found that for every 500 ppm increase in CO2, cognitive response times slow by 1.4-1.8%, and productivity decreases by 2.1-2.4%. Furthermore, another study links high CO2 levels to reduced sleep quality. These findings highlight the effects of indoor CO2 levels on both our physical health, mental performance and sleep quality.

After developing my own thermometer, I grew interested in CO2 monitoring. Although there are many commercial CO2 detectors available, I opted to build my own CO2 monitor using my thermometer project as the starting point. Replacing the temperature and humidity sensor with a CO2 sensor was a straightforward process.

It did require a deep dive into CO2 sensors, which led me to the Sensirion SCD41 sensor. Unlike many other CO2 sensors that merely estimate CO2 levels, the SCD41 sensor utilizes advanced photoacoustic NDIR technology to accurately measure the actual CO2 concentrations. According to the documentation:

The SCD4x series is based on photoacoustic NDIR technology. The technology exploits the characteristic property of CO2 molecules to strongly absorb infra-red (IR) light with wavelengths around 4.2 µm. When shining light of this wavelength through a gas sample, the CO2 concentration can thus be calculated from the proportion of light that is absorbed.
An ESP32-S3 development board is linked to an SCD41 CO2 and temperature sensor. For scale, a coin and pen are included. The SCD41 sensor is roughly equivalent in size to the coin, and the ESP32-S3 board is about twice the coin's diameter.
A development board with an ESP32-S3 chip (left) connected to a Sensirion SCD41 sensor on the right, which measures CO2 levels and temperature.

My ESP32 device measures CO2 levels every few minutes, connects to WiFi and sends this data to my web service endpoint at https://dri.es/sensors. This endpoint processes and visualizes the data. Unlike our basement temperature, I've chosen to keep the CO2 data private and not available to the public.

After I updated the client code on the ESP32 development board to use the new sensor, I also had to make small adjustments to the backend code used for data visualization.

Once I got everything working, I sneaked my project into our bedroom, sidestepping any objections by Vanessa, about turning our bedroom into a gadget lab.

The next morning, I was met with some surprising data: CO2 levels had spiked to 2,500 ppm! This was unexpected as we always sleep with the door slightly open and a ceiling fan on low.

Color-coded chart that illustrates gradually increasing CO2 concentrations during sleep.
A chart from my CO2 monitor that displays hourly air quality over four consecutive days. Each row is a day, and each square is an hour. Green squares mean normal CO2 levels and clean air; red squares mean high CO2 levels that can affect sleep and focus. Every day, the squares shift from green to red, showing air quality decreases during sleep.

Such high CO2 levels, as highlighted in Harvard University's research, can adversely impact sleep quality and cognitive functions.

After triple checking my code and monitoring the levels for several more nights, the trend was clear: CO2 concentrations consistently increased overnight, reaching levels beyond the recommended guidelines.

Armed with a few days of data, I presented my discoveries to Vanessa. Initially met with her characteristic skepticism (read: an eye-roll), she swiftly enabled the air cycling mode on our Nest thermostat. This function automatically activates the fan to circulate air, ensuring fresh air without the need to heat or cool.

The graph below shows how using the air cycling mode on our thermostat significantly improved CO2 levels in our bedroom. It proved to be much more effective than just keeping the door open and relying on a ceiling fan. What took me several nights to construct and analyze, Vanessa remedied in under a minute.

Color-coded chart indicating stable, healthy CO2 levels throughout the night.
A chart from my CO2 monitor that displays hourly air quality over four consecutive days. Each row is a day, and each square is an hour. Green squares mean normal CO2 levels and clean air; red squares mean high CO2 levels that can affect sleep and focus. The chart shows that CO2 levels remain healthy after we turned on the Nest's air cycling feature.

Of course, opening a window is a simple method to improve indoor air quality and would likely reduce CO2 levels more effectively than the Nest's air cycling mode. However, I'm told that living in a city and having white curtains makes us hesitant to do so.

Nevertheless, this project highlights how a bit of curiosity and creativity can enhance the health and comfort of our living spaces.

Starting your own CO2 monitor project can be an exciting and rewarding endeavor. In the rest of this blog post, I'll help you get started. I've detailed my hardware setup and provided the client-side code. As mentioned, the backend code builds on my thermometer project, so please consult that for further details.

Hardware used

For this project, I bought:

  1. Adafruit ESP32-S3 Feather: A microcontroller board with Wi-Fi and Bluetooth capabilities, serving as the central processing unit of my project.
  2. Adafruit SCD41 sensor: A high-accuracy CO2 and temperature sensor.
  3. 3.7v 500mAh battery: A small and portable power source.
  4. STEMMA QT / Qwiic JST SH 4-pin cable: To connect the sensor to the board without soldering.

Client code

What I also love about Sensirion is that they have Arduino libraries for their sensors, including for the SCD4x series (https://github.com/Sensirion/arduino-i2c-scd4x). These can easily be installed through Adafruit IDE.

A screenshot of the Arduino IDE with a dialog box for installing the 'Sensirion Core' dependency for the 'Sensirion I2C SCD4x' library.
Installing the Sensirion SCD4x library via the Arduino IDE.

Once installed, incorporating it into your project is straightforward–simply include #include "SensirionI2CScd4x.h" in your code.

Below is the complete client code. It comes with very detailed code comments to make it easy to understand.

#include "SensirionI2CScd4x.h"
#include "Adafruit_MAX1704X.h"
#include "WiFiManager.h"
#include "ArduinoJson.h"
#include "HTTPClient.h"
#include "Wire.h"

// The Adafruit_SCD4x sensor is a CO2, temperature and humidity sensor with 
// an I2C interface.
SensirionI2CScd4x scd4x;

// The Adafruit ESP32-S3 Feather comes with a built-in MAX17048 LiPoly / LiIon
// battery monitor. The MAX17048 provides accurate monitoring of the battery's
// voltage. Utilizing the Adafruit library helps us not only obtain the raw
// voltage data from the battery cell, but also converts this data into a more
// intuitive battery percentage or charge level. We will pass on the battery
// percentage to the web service endpoint, which can visualize it or use it to 
// send notifications when the battery needs recharging.
Adafruit_MAX17048 maxlipo;

// The setup() function is used to initialize the device's hardware and 
// communications. It's executed once at startup. Here, we begin serial 
// communication, initialize sensors, connect to Wi-Fi, and send initial 
// data. 
void setup() {

  // Wait for the serial connection to establish before proceeding further.
  // This is crucial for boards with native USB interfaces. Without this loop,
  // initial output sent to the serial monitor is lost. This code is not
  // needed when running on battery.
  // delay(5000);

  // Generates a unique device ID from a segment of the MAC address. 
  // Since the MAC address is permanent and unchanged after reboots, 
  // this guarantees the device ID remains consistent. To achieve a 
  // compact ID, only a specific portion of the MAC address is used, 
  // specifically the range between 0x10000 and 0xFFFFF. This range 
  // translates to a hexadecimal string of a fixed 5-character length, 
  // giving us roughly 1 million unique IDs. This approach balances 
  // uniqueness with compactness.
  uint64_t chipid = ESP.getEfuseMac();
  uint32_t deviceValue = ((uint32_t)(chipid >> 16) & 0x0FFFFF) | 0x10000;
  char device[6]; // 5 characters for the hex representation + the null terminator.
  sprintf(device, "%x", deviceValue); // Use '%x' for lowercase hex letters

  // Initialize the MAX17048 sensor.
  if (maxlipo.begin()) {
    Serial.println(F("MAX17048 battery monitor initialized."));
  else {
    Serial.println(F("Could not find MAX17048 battery monitor!"));

  // Initialize the SHT4x sensor.

  uint16_t error;

  // Adjust the temperature sensor's offset to 1 degree to correct for deviations,
  // including sensor self-heating and environmental factors (e.g., sun exposure).
  // The factory default is 4 degrees. Customize this offset for your specific
  // environment to enhance the accuracy of temperature readings.
  error = scd4x.setTemperatureOffset(1.0);
  if (error) {
    Serial.print(F("Error trying to set temperature offset: "));

  // Initiate a one-time measurement of CO2 concentration, relative humidity, and
  // temperature. We use "single shot" mode, which means the sensor performs a
  // one-time measurement. This process takes approximately 5 seconds to complete.
  // After the measurement, the result is available for retrieval.
  error = scd4x.measureSingleShot();
  if (error) {
    Serial.print(F("Error trying to put sensor in single shot mode: "));

  // Implement a delay of 1 second before initiating the next measurement. This
  // delay helps ensure the sensor has adequate time to prepare for the next
  // reading. This practice aligns with general sensor operation guidelines,
  // where a brief pause between measurements can help in achieving more accurate
  // and stable readings by allowing the sensor's internal components to stabilize.

  // According to the sensor's datasheet, we should ignore the first CO2 reading
  // after the sensor has been powered on or reset. The rationale behind this is
  // that the sensor's readings need one measurement to stabilize. Thus, we
  // perform a second "single shot" measurement, and use the results of this
  // second reading.
  error = scd4x.measureSingleShot();
  if (error) {
    Serial.print(F("Error trying to put sensor in single shot mode: "));

  // Read the CO2, temperature and humidity values from the sensor.
  uint16_t co2 = 0;
  float temperature = 0.0f;
  float humidity = 0.0f;
  error = scd4x.readMeasurement(co2, temperature, humidity);
  if (error) {
    Serial.print(F("Error trying to read measurement: "));

  // Read the battery charge level and cap it at 100%. This step corrects any
  // readings above 100%, which seems to occur due to measurement anomalies or
  // calculation inaccuracies. This ensures the displayed or reported battery
  // level is credible.
  float batteryPercent = maxlipo.cellPercent();
  batteryPercent = (batteryPercent > 100) ? 100 : batteryPercent;

  WiFiManager wifiManager;

  // Uncomment the following line to erase all saved WiFi credentials.
  // This can be useful for debugging or reconfiguration purposes.
  // wifiManager.resetSettings();
  // This WiFi manager attempts to establish a WiFi connection using known
  // credentials, stored in RAM. If it fails, the device will switch to Access
  // Point mode, creating a network named "Temperature Monitor". In this mode, 
  // connect to this network, navigate to the device's IP address (default IP
  // is using a web browser, and a configuration portal will be 
  // presented, allowing you to enter new WiFi credentials. Upon submission, 
  // the device will reboot and try connecting to the specified network with 
  // these new credentials.
  if (!wifiManager.autoConnect("CO2 Monitor")) {
    Serial.println(F("Failed to connect to WiFi ..."));

    // If the device fails to connect to WiFi, it will restart to try again.
    // This approach is useful for handling temporary network issues. However,
    // in scenarios where the network is persistently unavailable (e.g. router
    // down for more than an hour, consistently poor signal), the repeated
    // restarts and WiFi connection attempts can quickly drain the battery.

    // Mandatory delay to allow the restart process to initiate properly.


  // Send collected data as JSON to the specified URL.
  sendJsonData("https://dri.es/sensors", device, co2, temperature, humidity, batteryPercent);

  // WiFi consumes significant power so turn it off when done.
  // Enter deep sleep for 10 minutes. The ESP32-S3's deep sleep mode minimizes 
  // power consumption by powering down most components, except the RTC. This
  // mode is efficient for battery-powered projects where constant operation 
  // isn't needed. When the device wakes up after the set period, it runs
  // setup() again, as the state  isn't preserved.
  Serial.println(F("Going to sleep for 10 minutes ..."));
  ESP.deepSleep(10 * 60 * 1000000); // 10 mins * 60 secs/min * 1,000,000 μs/sec.

bool sendJsonData(const char* url, const char* device, float co2, float temperature, float humidity, float battery) {

  StaticJsonDocument<200> doc;

  // Round floating-point values to one decimal place for efficient data
  // transmission. This approach reduces the JSON payload size, which is
  // important for IoT applications running on battery.
  doc["device"] = device;
  doc["co2"] = String(co2, 0);
  doc["temperature"] = String(temperature, 1);
  doc["humidity"] = String(humidity, 1);
  doc["battery"] = String(battery, 0);

  // Serialize JSON to a string.
  String jsonData;
  serializeJson(doc, jsonData);

  // Initialize an HTTP client with the provided URL.
  HTTPClient httpClient;
  httpClient.addHeader("Content-Type", "application/json");

  // Send a HTTP POST request.
  int httpCode = httpClient.POST(jsonData); 

  // Close the HTTP connection.

  // Print debug information to the serial console.
  Serial.println("Sent '" + jsonData + "' to " + String(url) + ", return code " + httpCode);
  return (httpCode == 200);

void loop() {
  // The ESP32-S3 resets and runs setup() after waking up from deep sleep,
  // making this continuous loop unnecessary.

— Dries Buytaert

8 min read time