A Low-cost IoT Air Quality Monitor Based on RaspberryPi 4
Let’s take a break on all this theory and focus on how to measure particulate matters using a Raspberry Pi and the SDS011 sensor
The HW connection is in fact very simple. The sensor is sold with a USB adapter to interface the output data from its 7 pins UART with one of the RPi’s standard USB connectors.
SDS011 pinout:
- Pin 1 — not connected
- Pin 2 — PM2.5: 0–999μg/m³; PWM output
- Pin 3–5V
- Pin 4 — PM10: 0–999 μg/m³; PWM output
- Pin 5 — GND
- Pin 6 — RX UART (TTL) 3.3V
- Pin 7 — TX UART (TTL) 3.3V
For this tutorial, I am using for the first time, a brand new Raspberry-Pi 4. But of course, any previous model will also work fine.
As soon you connect the sensor on one of the RPi USB ports, you automatically will start to listen to the sound of its fan. The noise is a little bit annoying, so maybe you should unplug it and wait until you have all set with SW.
The communication between the sensor and RPi will be through a serial protocol. Details about this protocol can be found here: Laser Dust Sensor Control Protocol V1.3. But for this project, the best is to use a python interface to simplify the code to be developed. You can create your own interface or use some that are available on the internet, as Frank Heuer’s or Ivan Kalchev’s. We will use the last one, which is very simple and works fine (you can download the sds011.py script from its GitHub or mine).
The file sds011.py must be at the same directory where you create your script.
During the development phase, I will use a Jupyter Notebook, but you can use any IDE that you like (Thonny or Geany, for example, that are part of Raspberry Pi Debian package are both very good).
Start importing sds011, and creating your sensor instance. SDS011 provides a method to read from the sensor using a UART.
from sds011 import *
sensor = SDS011("/dev/ttyUSB0")<br>
You can turn on or off your sensor with the command sleep:
pmt_2_5, pmt_10 = sensor.query()
Wait at least 10 seconds for stabilization before measurements and at least 2 seconds to start a new one (see code above).
And this is all you need to know in terms of SW to use the sensor. But let’s go deeper on Air Quality Control! At the beginning of this article, if you have explored the sites that give information about how good or bad is the air, you should realize that colors are associated with those values. Each color is an Index. The most known of that is the AQI (Air Quality Index), used in the US and several other countries.