How to Use Raspberry Pi to Measure Particulate Matter (PM 2.5)

How to Use Raspberry Pi to Measure Particulate Matter (PM 2.5)

Build an air quality detector using two simple hardware devices and a few lines of code.
— Stephan Tetzel
Acknowledgements
Translated from | https://opensource.com/article/18/3/how-measure-particulate-matter-raspberry-pi | Author | Stephan Tetzel | Translator | Hank Chow (HankChow) 🌟 🌟 Total translations: 4 Contribution time: 118 days

Build an air quality detector using two simple hardware devices and a few lines of code.

We regularly measure particulate matter in schools in Southeast Asia. The measurements here are very high, especially between February and May, where various factors such as dry heat and land drought adversely affect air quality. In this article, I will show how to use Raspberry Pi to measure particulate matter.

What is particulate matter?

Particulate matter refers to dust or tiny particles in the air. The difference between PM10 and PM2.5 is that PM10 refers to particles with a diameter of less than 10 micrometers, while PM2.5 refers to particles with a diameter of less than 2.5 micrometers. The smaller the particles (less than 2.5 micrometers), the more they can be inhaled into the alveoli and affect the respiratory system, thus posing a greater health hazard.

The World Health Organization recommends that particulate matter concentrations[1] should be:

◈ Annual average PM10 not exceeding 20 µg/m³
◈ Annual average PM2.5 not exceeding 10 µg/m³
◈ Daily average PM10 not exceeding 50 µg/m³ during non-exceedance periods
◈ Daily average PM2.5 not exceeding 25 µg/m³ during non-exceedance periods

These values are actually lower than the standards of most countries, for example, the EU allows an annual average PM10 value of no more than 40 µg/m³.

What is the Air Quality IndexAQI?

The Air Quality Index is used to evaluate the quality of air based on particulate matter measurements, but since the calculation methods vary between countries, there is no unified standard for this index. Wikipedia provides an overview of the Air Quality Index[2]. Our school uses the classification established by the United States Environmental Protection AgencyEnvironment Protection Agency[3] (EPA) as the basis.

How to Use Raspberry Pi to Measure Particulate Matter (PM 2.5)

Air Quality Index

What preparations are needed to measure particulate matter?

Measuring particulate matter requires only the following two devices:

â—ˆ Raspberry Pi (any model, preferably with WiFi)
â—ˆ SDS011 particulate matter sensor

How to Use Raspberry Pi to Measure Particulate Matter (PM 2.5)

Particulate Matter Sensor

If you are using a Raspberry Pi Zero W that only has a Micro USB port, you will also need an adapter cable to connect to a standard USB port, which costs only $20, and the sensor comes with an adapter for the serial interface.

Installation process

For the Raspberry Pi, you just need to download the corresponding Raspbian Lite image and write it to a Micro SD card[4] (there are many tutorials online on how to set up WLAN connections, which I won’t detail here).

If you want to use SSH, you also need to create an empty file named ssh in the boot partition. The Raspberry Pi’s IP is obtained through the router or DHCP server, and then you can log in to the Raspberry Pi via SSH (the default password is raspberry):

$ ssh [email protected]

First, we need to install these packages on the Raspberry Pi:

$ sudo apt install git-core python-serial python-enum lighttpd

Before we start, we can use dmesg to get the serial interface connected to the USB adapter:

$ dmesg
[ 5.559802] usbcore: registered new interface driver usbserial
[ 5.559930] usbcore: registered new interface driver usbserial_generic
[ 5.560049] usbserial: USB Serial support registered for generic
[ 5.569938] usbcore: registered new interface driver ch341
[ 5.570079] usbserial: USB Serial support registered for ch341-uart
[ 5.570217] ch341 1–1.4:1.0: ch341-uart converter detected
[ 5.575686] usb 1–1.4: ch341-uart converter now attached to ttyUSB0

In the last line, you can see the interface ttyUSB0. Then we need to write a Python script to read the sensor’s data and store it in JSON format, which can then be displayed through an HTML page.

Reading data on the Raspberry Pi

First, create an instance of the sensor that reads the sensor data every 5 minutes for 30 seconds, and these values can be adjusted later. Between two measurements, we put the sensor into sleep mode to extend its lifespan (the manufacturer estimates the component’s lifespan at about 8000 hours).

We can use the following command to download the Python script:

$ wget -O /home/pi/aqi.py https://raw.githubusercontent.com/zefanja/aqi/master/python/aqi.py

Additionally, you need to execute the following two commands to ensure the script runs correctly:

$ sudo chown pi:pi /var/www/html/
$ echo '[]' > /var/www/html/aqi.json

Now you can execute the script:

$ chmod +x aqi.p
$ ./aqi.py
PM2.5:55.3, PM10:47.5
PM2.5:55.5, PM10:47.7
PM2.5:55.7, PM10:47.8
PM2.5:53.9, PM10:47.6
PM2.5:53.6, PM10:47.4
PM2.5:54.2, PM10:47.3
…

Automating script execution

We can use services like crontab so that we don’t have to manually start the script each time. Open the crontab file with the following command:

$ crontab -e

Add this line at the end of the file:

@reboot cd /home/pi/ && ./aqi.py

Now our script will automatically execute every time the Raspberry Pi restarts.

HTML page displaying particulate matter measurement values and air quality index

We have already installed a lightweight web server lighttpd, so we need to place the HTML, JavaScript, and CSS files in the /var/www/html directory so that the relevant data can be accessed via computer and smartphone. Execute the following three commands to download the corresponding files:

$ wget -O /var/www/html/index.html https://raw.githubusercontent.com/zefanja/aqi/master/html/index.html
$ wget -O /var/www/html/aqi.js https://raw.githubusercontent.com/zefanja/aqi/master/html/aqi.js
$ wget -O /var/www/html/style.css https://raw.githubusercontent.com/zefanja/aqi/master/html/style.css

In the JavaScript file, the process of opening the JSON file, extracting data, and calculating the air quality index is implemented, and then the background color of the page will change according to the EPA’s classification standards.

You just need to access the Raspberry Pi’s address with your browser to see the current particulate matter concentration values and other data: http://192.168.1.5:

This page is relatively simple and extensible, such as adding a table to display historical data from the past hours, etc.

This is the complete source code on Github[6].

Conclusion

In a relatively tight budget, Raspberry Pi is a choice. In addition, there are many applications available for measuring particulate matter, including outdoor fixed devices and mobile measurement devices, etc. Our school uses both: fixed devices measure particulate matter concentrations outdoors all day, while mobile measurement devices detect the effectiveness of air conditioning filters indoors.

Luftdaten.info[7] provides an introduction on how to design similar sensors, with excellent software performance, and because it does not use Raspberry Pi, the hardware is more compact.

For students, designing a particulate matter sensor is indeed an excellent extracurricular project.

How do you plan to use your Raspberry Pi[8]?

via: https://opensource.com/article/18/3/how-measure-particulate-matter-raspberry-pi

Author: Stephan Tetzel[10] Translator: HankChow Proofreader: wxy

This article is originally translated by LCTT, launched with honor by Linux China

Leave a Comment