How I built my own CO2 monitor using a Raspberry Pi for Home Office.

Yannis Burghardt
6 min readJul 12, 2022

--

Image Courtesy: Author

In the last two years, more and more people started working from home. So it is important to have the best possible workplace in order to continue to work productively. To have an optimal workplace requires much more than just a good desk and a desk chair. The environment in your workplace is also very important. In particular, the quality of the air in the room you are working. The carbon dioxide (CO₂) concentration plays a major role here.

Why is the CO₂ level so important?

Classification of air quality according to European standard DIN EN 13779.
Image Courtesy: Author — CO₂ concentration is measured in ppm (parts per million) [1]

CO₂ is released into the air when people breathe. If the room is not ventilated well and regularly, the CO₂ level increases. This reduces the ability to concentrate and thus productivity. Studies of the U.S. Department of Health and Human Services showed cognitive scores were up to 61% higher when working in a building with a low CO₂ (and VOC) level in comparison to working in a building with common air quality [2]. Fortunately, it is easy to build a cheap CO₂ monitor with a Raspberry Pi and a MH-Z19C sensor to be reminded early to ventilate.

The image above shows the official classification of air quality according to the European standard DIN/EN 13779. So if the CO₂ level in your room is around 800-1000ppm (or even higher) you should start ventilating.

What have I built?

Showcase of the web dashboard (CO₂ monitor)
Image Courtesy: Author

My solution to ensure good air quality was to develop a CO₂ monitor. The very basic version you see above shows the current level, so you know when you have to ventilate. Any computer in the network (or even outside if you set it up properly) can access this dashboard to check the current CO₂ level.

Actually, I built a much more complex version with graph history, HomeKit support, and notifications, but that would be too much for this article. Just drop a comment if you are interested in this.

Legal Disclaimer
This is not a guide, just an editorial documentation of how I implemented my own project. I have tested this and created the blog post to the best of my knowledge, but I take no responsibility for the steps shown I took to build my very own CO2 monitor. I assume no liability in any respect. Everything you do is at your own risk.

What did I need?

  • A Raspberry Pi (Model 3B+)
  • MH-Z19C CO₂ Sensor (pin-version — I bought the JST ZH version — a bit more complicated to wire)
  • 4 jumper wires (female2female)
MH-Z19C connected to a Raspberry 3B+
Image courtesy: Author — I did not purchase the pin version. This made wiring more complicated.

Connecting the Sensor

GPIO CLI tip & GPIO layout I used for my CO₂ sensor.
Image courtesy: Author — Use the pinout command to check your GPIO layout.

Tip: The pinout command is an easy way to check the GPIO (General Purpose Input/Output) layout of your raspberry.

After I got all my parts, I just had to do these things to connect my MH-Z19C CO₂ sensor to my Raspberry Pi.

  1. sudo raspi-config To enable the serial interface which is used to communicate with the MH-Z19C. This tool is asking quite a few questions… (Interface Options — Serial Port — No — Ok)
  2. Power down the Raspberry Pi
  3. Connect the first jumper wire to a GND (Ground) GPIO pin and to the GND sensor pin.
  4. Connect the second jumper wire to the GPIO15 (RXD) pin and to the Tx pin of the sensor. By connecting these pins, the Raspberry is able to receive data from the sensor.
  5. To also transmit data to the sensor, I had to connect the third jumper wire to theGPIO14 (TXD) pin on the Raspberry and to the Rx pin on the MH-Z19C sensor.
  6. Lastly I had to connect another jumper wire to a 5V GPIO pin and to the Vin sensor pin.

The graphic I made visualizes how I connected everything to the Raspberry Pi. I had to use exactly the pins GPIO14 and GPIO15, because they are the default pins for UART communication. UART is the way the sensor communicates with the raspberry. (If you are interested in this topic, you can check out this article to learn more about UART. Pretty cool topic 😎)

Develop the Software

I wrote two different application-parts to make this project happen.

Backend

Example of using the backend API
Image Courtesy: Author

First there is the backend, which is programmed in Go (golang).
It spins up a simple HTTP-Restful API which reports the latest CO₂ measurement in JSON format. This enables me to integrate this backend into different applications, not only my CO₂ monitor dashboard. I could use this API for example in Home Assistant or ioBroker too, because it uses the open standard JSON.

You can check out the code and more about the project on GitHub.

Frontend

The React UI of the CO₂ monitor.
Image Courtesy: Author

The visual interface is built with React and Typescript. It polls the CO₂ value every few seconds by communicating with the backend. The UI updates the current CO₂ value automatically. The Gauge or Speedometer, name it whatever you want, changes its color based on the CO₂ levels classified in the DIN/EN 13779 — I told you about that earlier, you remember?

The code for the frontend is pretty basic. There is just some basic math in the Gauge component to make it work correctly and some CSS to make the UI less ugly.

Code of the App component.
Image Courtesy: Author

You can check out the code and more about the project on GitHub.

Conclusion

I learned many things while building this project. CO₂ is impacting our productivity and well-being. It is important to be aware of this. If you are, there is probably not the need to build something like this. But let's be honest, this is pretty cool. 🙂
Additionally, according to the HHS, it makes a difference how “green” your room is, so consider having some more plants in your office. 🪴

I usually build much more complex software projects, but building this kind of hardware projects is so cool too! And how would you know that CO₂ levels are also standardized in the European Union?

Thanks for reading this! If you enjoyed this article make sure to leave a 👏, and if you need any other information or want to request a topic I should cover, feel free to drop a comment below!

Sources & Links:

[1] German Committee on Indoor Air Guide values. Umweltbundesamt. Retrieved July 10, 2022, from https://www.umweltbundesamt.de/en/topics/health/commissions-working-groups/german-committee-on-indoor-air-guide-values#german-committee-on-indoor-air-guide-values-air

[2] U.S. Department of Health and Human Services. (n.d.). Associations of cognitive function scores with carbon dioxide, ventilation, and volatile organic compound exposures in office workers: A Controlled Exposure Study of green and conventional office environments. National Institute of Environmental Health Sciences. Retrieved July 11, 2022, from https://ehp.niehs.nih.gov/doi/10.1289/ehp.1510037

Learn more about UART: https://www.analog.com/en/analog-dialogue/articles/uart-a-hardware-communication-protocol.html
Learn more about Go: https://go.dev/
Learn more about React: https://reactjs.org/
Learn more about TypeScript: https://www.typescriptlang.org/

--

--

Yannis Burghardt

Computer science student who loves to code and learn new things. Here I share my tips and tricks on how to be more productive and efficient with technology.