air pollution air quality HackSpace monitoring Raspberry Pi Resources

Monitor air quality with a Raspberry Pi

Add a sensor and a few Python 3 to your Raspberry Pi to keep tabs in your local air air pollution, within the venture taken from Hackspace magazine problem 21.

Air is the very stuff we breathe. It’s about 78% nitrogen, 21% oxygen, and 1% argon, after which there’s the various ‘other’ bits and pieces – lots of which have been spewed out by people and our associated equipment. Carbon dioxide is obviously an necessary polluter for local weather change, however there are different bits we must be involved about for our well being, including particulate matter. That is simply actually small bits of stuff, like soot and smog. They’re grouped collectively based mostly on their measurement – crucial, from a well being perspective, are those which might be smaller than 2.5 microns in width (referred to as PM2.5), and PM10, that are between 10 and a couple of.5 microns in width. This air pollution is linked with respiratory sickness, heart illness, and lung cancer.

Clearly, this is one thing that’s essential to find out about, however it’s one thing that – here within the UK – we’ve comparatively little knowledge on. Whereas there are official sensors in most main towns and cities, the consequences might be very localised around busy roads and trapped in valleys. How does the actual make-up of your space have an effect on your air quality? We set out to monitor our surroundings to see how concerned we ought to be about our native air.

Getting started

We picked the SDS011 sensor for our venture (see ‘Picking a sensor’ under for particulars on why). This sends output by way of a binary knowledge format on a serial port. You’ll be able to learn this serial connection immediately when you’re using a controller with a UART, but the sensors additionally often come with a USB-to-serial connector, permitting you to plug it into any trendy pc and skim the info.

The USB-to-serial connector makes it straightforward to attach the sensor to a pc

The very easiest method of utilizing this is to attach it to a pc. You’ll be able to learn the sensor values with software resembling DustViewerSharp. For those who’re just excited about reading knowledge sometimes, that is a completely superb method of utilizing the sensor, but we would like a steady monitoring station – and we didn’t need to depart our laptop computer in a single place, operating on a regular basis. In terms of small, low-power boards with USB ports, there’s one that all the time springs to mind – the Raspberry Pi.

First, you’ll want a Raspberry Pi (any model) that’s arrange with the newest model of Raspbian, related to your native network, and ideally with SSH enabled. In the event you’re not sure how to do this, there’s steerage on the Raspberry Pi website.

The wiring for this venture is simply concerning the simplest we’ll ever do: connect the SDS011 to the Raspberry Pi with the serial adapter, then plug the Raspberry Pi into a energy source.

Earlier than getting began on the code, we also have to arrange a knowledge repository. You possibly can retailer your knowledge wherever you want – on the SD card, or upload it to some cloud service. We’ve opted to upload it to Adafruit IO, an internet service for storing knowledge and making dashboards. You’ll want a free account, which you’ll be able to sign up for on the Adafruit IO web site – you’ll have to know your Adafruit username and Adafruit IO key in an effort to run the code under. In the event you’d somewhat use a totally different service, you’ll need to regulate the code to push your knowledge there.

We’ll use Python three for our code, and we’d like two modules – one to learn the info from the SDS011 and one to push it to Adafruit IO. You’ll be able to set up this by getting into the following commands in a terminal:

pip3 install pyserial adafruit-io

You’ll now have to open a text editor and enter the following code:

This does a few issues. First, it reads ten bytes of knowledge over the serial port – precisely ten because that’s the format that the SDS011 sends knowledge in – and sticks these knowledge points together to type a record of bytes that we name knowledge.

We’re all in favour of bytes 2 and three for PM2.5 and 4 and 5 for PM10. We convert these from bytes to integer numbers with the marginally complicated line:

pmtwofive = int.from_bytes(b’’.be a part of(knowledge[2:4]), byteorder=’little’) / 10

from_byte command takes a string of bytes and converts them into an integer. Nevertheless, we don’t have a string of bytes, we have now a record of two bytes, so we first have to convert this into a string. The b’’ creates an empty string of bytes. We then use the be a part of technique of this which takes a record and joins it together utilizing this empty string as a separator. As the empty string incorporates nothing, this returns a byte string that simply incorporates our two numbers. The byte_order flag is used to denote which approach around the command should read the string. We divide the end result by ten, as a result of the SDS011 returns knowledge in models of tens of grams per metre cubed and we would like the end in that format aio.send is used to push knowledge to Adafruit IO. The first command is the feed value you want the info to go to. We used kingswoodtwofive and kingswoodten, because the sensor is predicated in Kingswood. You may need to select a more geographically related identify. Now you can run your sensor with:


…assuming you referred to as the Python file
and it’s saved in the identical listing the terminal’s in.

At this level, the whole lot should work and you may set about operating your sensor, however as one remaining point, let’s set it up to start routinely once you flip the Raspberry Pi on. Enter the command:

crontab -e

…and add this line to the file:

@reboot python3 /residence/pi/

With the code and electronic setup working, your sensor will want somewhere to reside. If you need it outdoors, it’ll need a waterproof case (however embrace a way for air to get in). We used a Tupperware box with a gap minimize within the backside mounted on the wall, with a USB cable carrying energy out by way of a window. How you do it, although, is as much as you.

Now let’s democratise air quality knowledge so we will make better selections concerning the places we reside.

Choosing a sensor

There are a variety of particulate sensors available on the market. We picked the SDS011 for a couple of causes. Firstly, it’s low cost enough for a lot of makers to have the ability to buy and construct with. Secondly, it’s been fairly properly studied for accuracy. Both the hackAIR and InfluencAir tasks have in contrast the readings from these sensors with costlier, better-tested sensors, and the outcomes have come back favourably. You possibly can see more details at and

The one caveat is that the results are unreliable when the humidity is at the extremes (both very high or very low). The SDS011 is just rated to work as much as 70% humidity. When you’re accumulating knowledge for a research, then it is best to discard any readings when the humidity is above this. HackAIR has a components for trying to right for this, nevertheless it’s not dependable enough to neutralise the effect utterly. See their web site for more details:

Protected levels

Once you’re monitoring your PM2.5 knowledge, what do you have to look out for? The World Well being Organisation air quality guideline stipulates that PM2.5 not exceed 10 µg/m3 annual imply, or 25 µg/m324-hour imply; and that PM10 not exceed 20 µg/m3 annual imply, or 50 µg/m3 24-hour mean. Nevertheless, even these won’t be protected. In 2013, a giant survey revealed in The Lancet “found a 7% increase in mortality with each 5 micrograms per cubic metre increase in particulate matter with a diameter of 2.5 micrometres (PM2.5).”

The place to locate your sensor

Commonplace recommendation for finding your sensor is that it ought to be outdoors and 4 metres above ground degree. That’s good recommendation for basic environmental monitoring; nevertheless, we’re not essentially typically environmental monitoring – we’re all for figuring out what we’re breathing in.

Finding your monitor near your workbench will provide you with an concept of what you’re truly inhaling – ineffective for any environmental research, but helpful should you spend a lot of time in there. We discovered, for example, that the glue gun produced large amounts of PM2.5, and we’ll be much more careful with ventilation when using this device in the future.

Adafruit IO

You should use any knowledge platform you want. We chose Adafruit IO because it’s straightforward to use, permits you to share visualisations (within the form of dashboards) with others, and connects with IFTTT to perform actions based mostly on values (ours tweets when the air pollution is above legal limits).

One thing to concentrate on is that Adafruit IO only holds knowledge for 30 days (on the free tier at the very least). If you need historical knowledge, you’ll want to join the Plus choice (which shops knowledge for 60 days), or use an alternate storage technique. You should use a number of knowledge stores in case you like.

Checking accuracy

Now you’ve received your monitoring station up and operating, how have you learnt that it’s operating correctly? Perhaps there’s a problem with the sensor, or maybe there’s a drawback with the code. The simplest technique of calibration is to test it towards an correct sensor, and most cities here within the UK have monitoring stations as a part of Defra’s Automated Urban and Rural Monitoring Network. Yow will discover your native station right here. Many other nations have equivalent public networks. Until there isn’t a different choice, we might warning towards using crowdsourced knowledge for calibration, as these sensors aren’t themselves calibrated.

With a USB battery pack, you possibly can head to your local monitoring point and see in case your monitor is getting comparable outcomes to the monitoring network.

HackSpace magazine #21 is out now

You’ll be able to learn the remainder of this function in HackSpace magazine problem 21, out at this time in Tesco, WHSmith, and all good unbiased UK newsagents.

Or you should purchase HackSpace magazine immediately from us — worldwide supply is obtainable. And when you’d wish to own a useful digital version of the magazine, you may also download a free PDF.