How To Measure Particulate Matter With a Raspberry Pi

Published by Stephan on

Last Updated on

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 is all particles in the air that are smaller than 10µm, whereas PM2.5 is 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, for example:

  • 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 e.g. 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.


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 net.

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):

For our sensor we use a small Python module, which saves us a lot of work. To do this we need to install some packages and clone the repository on the Pi.

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

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).

[Update, 27.02.2018] I noticed that my Python3 version of the script could not access the serial ports, e.g. after a reboot. The data could only be read after starting the script several times. I didn’t find out why, but with Python2.7 it works without any problems. So here is a new version of the script based on Python2.7.

In the home directory we create a file named and copy the following content into it:

Or you can just run that:

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

Now you can start the script:

Run the script automatically [Update]

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 Pis. To do this, open the crontab file

and add the following line at the end:

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:


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 Pis in your browser and look at the current particulates values, e.g.

AQI values

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.


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 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 implemented well with students in computer science classes or a workshop!

Also check out our following post, where we add a touch screen to the Raspberry Pi to have a full mobile sensor!

What do you use a Raspberry Pi for?




I'm a teacher and IT system administrator in an international school. I love open source software and I used it over a decade in my private and work life. My passion is to solve problems with open source software!