How to build your own air quality meter with a Raspberry Pi and CO2 sensor
Image: Foundry
A reliable indicator of the air quality in offices, classrooms, or living rooms is the CO2 content.
In fresh air, the concentration of carbon dioxide is around 400 ppm. This stands for “parts per million”, in this case 400 CO2 molecules per million air molecules. Although this is only 0.04 per cent, it has a noticeable effect.
Indoors, the concentration rises rapidly due to human respiration (ie. breathing). Surprisingly, during a film screening in a movie theater, CO2 levels of several thousand ppm can be quickly reached. If CO2 levels are high enough, this can lead to lack of concentration, tiredness, and headaches.
A CO2 measuring station can tell you when it’s the right time to open the windows—fresh air is needed if CO2 levels reach 1500 ppm or higher.
What you’ll need
Firstly, you need a Raspberry Pi with a reasonably up-to-date Raspberry Pi OS. It doesn’t matter which Raspberry model you use.
You also need a CO2 sensor. The MH-Z19C sensor from the Chinese manufacturer Winsen Electronics Technology is used in this article. The MH-Z19B model works in the same way.
When buying, you should choose a model with pins, otherwise you will have to solder it yourself. Pins or headers are small pins that you use to connect the sensor to the Raspberry Pi. The version with headers is available on Amazon so make sure you choose that version when you buy it.
Finally, you will also need jumper wires. You will need the female/female variant for the sensor.
Overall, the investment is less than $40, whereas professional CO2 measuring devices cost many times more if you were to buy ready-made models.
Important: To read out the sensor, the Raspberry Pi communicates via the serial interface, which is deactivated by default. To change this, use the tool
raspi-config
and select “Serial Port” under “Interface Options”.
How the sensor works
The MH-Z19C (or B) is a non-dispersive infrared sensor (NDIR). These are based on the concentration-dependent absorption of electromagnetic radiation in the infrared wavelength range. Carbon dioxide absorbs infrared radiation in a specific and measurable way.
The light source transmits IR radiation through the air to be measured by the actual sensor. A filter is attached to the front which allows the absorption wavelength of the target gas to pass through.
If the concentration increases, the absorption increases according to Lambert-Beer’s law and the signal decreases accordingly. The sensor determines the resulting light intensity and converts it into ppm.
How to connect the sensor to the Raspberry Pi. The sensor should be viewed from the underside.
Foundry
Setting up the circuit
To set up the circuit, first shut down the Raspberry Pi and disconnect it from the power supply. Then connect the circuit board to the sensor. You will need four jumper wires female/female, i.e. with sockets at both ends.
You can see the function of each connection on the underside of the sensor. Connect pin 6 of the sensor (labelled “VCC” or “Vin”) to pin 4 on the Raspberry Pi. This is the power supply with five volts. Pin 4 is the second from the top on the right-hand side of the GPIO strip.
Now connect pin “GND” (7) on the sensor to pin 6 on the Raspberry board. This is the earth wire. It is located directly under pin 4.
Halfway through, continue on the other side of the sensor, which has five connections: Pin “Rx” (2) on the sensor must be connected to pin 8 on the mini computer, directly under pin 6.
Finally, connect pin “Tx” (3) on the sensor to pin 10 on the Raspberry Pi, directly below pin 8. The illustration above shows the complete setup again. Finally, start the Raspberry Pi by reconnecting it to the power supply.
Read out measured values
The CO2 sensor is ready for use. A Python script is needed to read out the current CO2 value. Thankfully, one was developed by a Japanese developer – just for information, to explain the Github address. You install the script together with other components by opening a terminal on the Raspberry Pi and entering the following command:
git clone https://github.com/UedaTakeyuki/mh-z19.git
Then change to the newly created folder “~/mh-z19” and execute the installation script:
./setup.sh
To determine the current CO2 value, use this command in future:
sudo python -m mh_z19
You will receive a result such as “{“co2″: 3128}”. The unit is the current ppm of CO2.
The measured values can be automatically displayed online and clearly show the effect of ventilation.
Foundry
Measured values online
Always reading out the measured values manually on the Raspberry Pi is annoying and too tedious in the long run. So it’s better to display the measured values online over time.
Use a free online service to access the history of the measured values via PC or smartphone. Go to monitor3.uedasoft.com and enter an e-mail address and password to log in. You will receive a “view_id”, which you do not need.
Click on the menu item Elements. Make a note of the second letter combination in the list, in the example “prgrvpqg”, and set the corresponding option to Active. Click on Save to finalize the setup.
Now open a terminal on the Raspberry Pi and switch back to the “mh-z19” folder. There you call the command
./setid.sh [abcdefgh]
replacing the sequence of letters with the eight-digit letter combination that you have just noted. This call is used to check whether everything is working properly:
sudo python -m pondslider
If the word “true” appears in the bottom line of the output, then everything is OK. In order to regularly receive measured values from now on and enter them in the Monitor app, enter the command
./autostart.sh --on
In the browser, you can now see how the diagram on the Monitor page grows by one measured value every five minutes. The number of measured values displayed can be set directly below using the “Settings” button.
To cover ten hours, you need 120 values. With the diagram, you can see at a glance whether someone has been airing the room while you were at lunch or shopping.
The graph with the measured values can also be conveniently called up with a smartphone and downloaded as a CSV file via the “Download” button in order to analyze it in Excel. After rebooting the Raspberry Pi, the measurement script restarts automatically.
The concept can be expanded further. With a red LED, a suitable 330 Ohm resistor and a slightly modified Python script, a circuit can be created in which the LED lights up as soon as the measured value exceeds a specified CO2 limit value.
This article originally appeared on our sister publication PC-WELT and was translated and localized from German.
Author: Brad Chacos, Executive Editor, PCWorld
Brad Chacos spends his days digging through desktop PCs and tweeting too much. He specializes in graphics cards and gaming, but covers everything from security to Windows tips and all manner of PC hardware.
