How To Measure Particulate Matter With a Raspberry Pi

Written by OpenSchoolZ | Published 2018/02/28
Tech Story Tags: raspberry-pi | tech | technology | open-source | edtech

TLDRvia the TL;DR App

We regularly measure particulate matter in the air at our school. Particularly between February and May the values here in South-East Asia are very high, because it hardly rains during this time, it is very dry and hot and many meadows and fields are burned down. All this has a negative effect on the quality of the air. Today I would like to show you how to measure particulate matter with a Raspberry Pi. However, I would like to comment briefly on the question first: What is particulate matter and what do we want to measure?

What is particulate matter?

It can be said quite roughly that fine dust is a very small particle in the air. A distinction is made between PM10 and PM2.5. PM10 are all particles in the air that are smaller than 10µm, whereas PM2.5 are all particles that are smaller than 2.5µm. The smaller the particles, i.e. everything smaller than 2.5µm, the more dangerous they are to health as they can penetrate into the alveoli.

The WHO recommends the following limit values:

  • Annual average PM10 20 µg/m³
  • Annual average PM2,5 10 µg/m³ per year
  • Daily average PM10 50 µg/m³ without permitted days on which exceeding is possible.
  • Daily average PM2,5 25 µg/m³ without permitted days on which exceeding is possible.

These values are below the limits set in most countries. In the European Union, an annual average of 40 µg/m³ for PM10 is allowed.

What is the Air Quality Index (AQI)?

The Air Quality Index can be calculated on the basis of the particulates in the air. It indicates how “good” or “bad” the air is straight. Unfortunately, there is no uniform standard here, because different countries calculate this differently or have different scales. The Wikipedia article on the Air Quality Index provides a good overview. At our school, we are guided by the classification established by the EPA (United States Environmental Protection Agency).

So much for a brief outline on the subject of particulate matter and AQI.

What do we need for measuring particulate matter?

Actually, it only takes two things:

That’s it :)

Whoever chooses a Raspberry Pi Zero W needs an adapter cable to a standard USB port, because the Zero has only Micro-USB. The sensor is best purchased at Aliexpress. It’s about $17–20 there. The sensor comes with a USB adapter for the serial interface.

Installation

For our Raspberry Pi we download the corresponding Raspbian Lite Image and write it on the Micro-SD card. This is documented here, for example. At this point, I will not go into setting up the WLAN connection. There are many tutorials on the Internet.

If you want to have SSH enabled after booting, you need to create an empty file named ssh in the boot partition. The IP of the Raspberry Pis can best be obtained via your own router / DHCP server. You can then log in via SSH (default password is raspberry):

$ ssh pi@192.168.1.5

First we need to install some packages on the Pi:

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

Before we can start, we need to know which serial port the USB adapter is connected to. dmesg helps us:

$ 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 our interface: ttyUSB0. We now need two things: a small Python script that reads the data and saves it in a JSON file, and then we will create a small HTML page that reads and displays the data.

Reading data on the Raspberry Pi

We first create an instance of the sensor and then read the sensor every 5 minutes for 30 seconds. These values can of course be adjusted. Between the measuring intervals, we put the sensor into a sleep mode to increase its life span (according to the manufacturer, the lifetime is approx. 8000 hours).

We can download the script with this command:

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

In order for the script to run without errors, two small things are still needed:

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

Now you can start the script:

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

Run the script automatically

So that we don’t have to start the script manually every time, we can let it start with a cronjob e.g. with every restart of the Raspberry Pi. To do this, open the crontab file

$ crontab -e

and add the following line at the end:

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

Now our script starts automatically with every restart.

HTML page for display of measured values and AQI

We have already installed a lightweight webserver lighttpd. So we have to save our HTML, Javascript and CSS files in the directory /var/www/html/ so that we can access the data from another computer / smartphone. With the next three commands we simply 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

The main work is done in the Javascript file, which opens our JSON file, takes the last value and calculates the AQI based on this value. Then the background colors are adjusted according to the scale of the EPA.

Now you simply open the address of the Raspberry Pi in your browser and look at the current particulates values, e.g. http://192.168.1.5:

The page is very simple and can be extended, e.g. with a chart showing the history of the last hours, etc. Pull requests are welcome :)

The complete source code is on Github.

Conclusion

For relatively little money we can now measure particulate matter with a Raspberry Pi. Whether permanently installed outdoors or as a mobile measuring device — there are many possible applications. At our school we have both in use. On the one hand, there is a sensor that measures outdoor values day and night, and a mobile sensor that checks the effectiveness of our air condition filters in the classrooms.

At http://luftdaten.info there is another possibility to build a similar sensor. The software is delivered ready to use and the measuring device is even more compact, because no Raspberry Pi is used. Great project!

A particulates sensor is a project that can be done well with students in computer science classes or a workshop!

What do you use a Raspberry Pi for?

Originally published at openschoolsolutions.org. Sign up to our newsletter to get access to a FREE PDF with great open source apps for your classroom or follow @OpenSchoolZ on Twitter.


Published by HackerNoon on 2018/02/28