DIY Low-Cost Air Quality Sensors: Coding, Construction, and Testing

Erin Weaver
12 min readMay 7, 2018

--

Introduction

This post should serve as a guide for building a low-cost air quality (AQ) monitor and an exploration of the extent of their functionality. After discussing the importance of healthy air quality and monitoring activities in the world today, I will go step-by-step through the construction, coding, and components used by myself and other members of my International Air Quality Lab at Georgetown University to build our own low-cost AQ monitors. Finally, I will describe my personal experience testing my AQ monitor, my findings, and implications for the future use of low-cost AQ sensors. Conclusions and suggestions for improving similar devices will also be discussed.

Context for air pollution and air quality monitoring

According to the WHO, outdoor air pollution is a global health burden that accounts for roughly 3 million deaths annually. Some of the adverse effects of poor air quality on human and environmental health are respiratory illness, traffic safety, and crop damage. Particulate matter, specifically PM2.5, is exceedingly detrimental to human health. One article published through Harvard University estimated that reducing PM2.5 concentrations in India to meet the National Ambient Air Quality Standard (40 µg/m3) would increase life expectancy for all Indians by roughly 3.2 years, for a total of 2.1 billion life years saved. That is just one example of the tremendous human health impacts of poor AQ around the world. There are also staggering economic consequences of outdoor air pollution; an OECD report found that air pollution-related healthcare costs will increase from USD 21 billion in 2015 to USD 176 billion in 2060 under current projections. These staggering statistics beg the question: why isn’t clean AQ a central policy, health, and geopolitical issue?

Part of that answer lies in a lack of accurate and accessible data regarding air pollution levels. According to the World Air Quality Index team, a social enterprise project that publishes AQ measurements, there are approximately 20,000 known air quality monitoring stations in the world. Of those, only around 10,000 stations in 800 major cities from 70 countries publish real-time AQ information. These monitoring stations also aren’t consistent regarding measurement cycles or specific pollutants measured. This lack of universal, available AQ data makes education and decision-making more difficult, on both the governmental and individual level. Air quality data could have varied and significant usages in the policy, scientific, health, real estate, and education communities.

As such, finding a way to measure and publish more AQ data globally would have knock-on benefits across sectors and countries. This is where low-cost air quality monitoring devices could play a role. Standard regulatory-grade AQ monitors cost approximately $20,000 and require professional maintenance at regular intervals. They also need an electric power source with an installed capacity of ~500W. Consequently, there are many cities and regions in the world where this type of rigorous monitoring is infeasible. Some AQ estimates can be made from satellite data, but these are less accurate than monitoring devices on-the-ground. Employing low-cost AQ sensors especially in these underserved areas could provide crucial data for environmental, health, and policy decisions. Low-cost AQ sensors could be used to increase AQ awareness and scientific literacy, thus equipping citizens with data to pressure their governments toward cleaner AQ regulatory reform. Deploying low-cost AQ sensors would serve to bridge information gaps and make data Findable, Accessible, Interoperable, and Reusable (FAIR).

It should be noted, however, that there are limits to the application of data from low-cost AQ sensors. According to the 2017 AQ Workshop Summary, some low-cost sensors “show no correlation to FRM/FEM (federal-grade) measurements, while others show reasonable correlations (r2~0.7).” These sensors have greater uncertainty in measurements, and thus can’t be employed for regulatory-compliant measurements. That being said, “some” correlation could still serve to inform the public and government officials on relative AQ levels and increase discussions about AQ in places that historically haven’t recorded any data.

Sensors, microcontroller, wiring

With the extent of its application in mind, we will walk through the components and assembly of the relatively low-cost AQ monitor built during our course. The monitor we built employs two major sensors, one that measures temperature and humidity and another that measures PM2.5 concentrations. The first is the DH-11 digital sensor, which uses a capacitive humidity sensor and a thermistor to measure the surrounding air and then outputs a digital signal from the data pin. Both operations of the sensor measure resistance on an integrated circuit (IC) chip and convert the data to temperature (T) and relative humidity (RH). The PPD42NS nephelometer device measures PM2.5 concentrations. It uses a small heater, measuring the amount of scattered LED light from particles moving through the device and converts that data to pcs/0.01cf.

Those sensors are connected to the microcontroller, in our case the Adafruit Feather Huzzah ESP8266; this device is based off of the original 2005 Arduino system that specializes in reading sensor data and/or controlling devices. The Feather Huzzah can run the same code as an Arduino and can use the Arduino Integrated Development Environment (IDE) for writing and installing programs. The microcontroller also has integrated WiFi capability and can use a micro USB connector or a connector to a 3.7V Lithium polymer battery for power supply. We also attached a Adafruit SSD1306 monochrome LED screen to the Feather Huzzah to display the measurements on the device.

All of these components are connected using an electronic circuit solderless breadboard. Electrical circuits must have a complete path for current to flow to the sensors and provide power. The current flows from positive (+) to negative (-), otherwise known as ground. The Feather HUZZAH provides the +/- connections to the sensors, enabled by the breadboard.

3D-Printed parts and waterproof enclosure

For this AQ monitor, we 3D printed three components: the AQ case, the case lid, and the larger box adapter. The smaller AQ case and lid held the Feather Huzzah and DHT-11 sensor, while the box adapter connected the monitor to the charge controller, PPD42NS sensor, and the larger waterproof enclosure of the device. We printed these components using a Monoprice Maker Select Fused Deposition Modeling (FDM) style printer, and the STL files can be found on Thingiverse.

To make our devices suitable for measuring AQ outside, we placed all of these components in a large plastic weatherproof enclosure with a clear top. This case was also purchased from Adafruit. The only modification was sawing off a square of the plastic on the bottom side of the case to allow air to enter and draft upwards. The 3D-printed box adapter screws to the enclosure and secures all of the sensors and components.

Battery and solar panel

Our AQ monitor can be powered through a combination of solar and battery power or through a micro USB port. The 3.7V lithium ion battery is connected directly to the Feather Huzzah, and when the battery is full it supplies the AQ monitor with energy. The battery is further connected to a 6V Solar PV panel through a charge controller. The objective is for the solar PV to recharge the battery when the sun is shining and for the battery to sustain the system when the sun isn’t out. The intermediate connection of the charge controller is needed to control the flow of current to the battery i.e. stop the flow when the battery is full. It should be noted that some soldering was required to connect the capacitor and load wire onto the charge controller.

In a best-case scenario, this system should be able to last outside without connecting to an external power source for at least 24 hours. In my testing however, I found it impossible for the solar charging system to run the device for a significant period of time. I will address this deficiency more below, but the solar panel never fully charged the battery, even after I left it outside for multiple days. The AQ monitor works equally well with a micro-USB charger connected to a wall socket, but that can be difficult to guarantee for outdoor measurement locations.

Overall Costs

Device Cost

Feather Huzzah ESP8266 $16.95

Solderless Breadboard $5.93

DHT-11 Sensor $5.00

PPD42NS Sensor $11.50

Monochrome O-LED Graphic Display $17.50

USB Wall Charger with Micro USB Data Cable $7.99

Large Plastic Project Enclosure $19.95

3D Printed Components $1

Lithium Ion Polymer Battery $14.95

LiPo Charge Controller $17.50

6V/2W Solar Panel $29.00

Total $147.27

This sum does not include the negligible prices of the wires, bolts and nuts, and tape.

Data Connectivity and Hosting

Our AQ device connects to the internet via the Feather Huzzah’s Wifi Module. This WiFi-enabled connectivity is fast, low-cost, and works with any network given the proper authentication. Using the Wifi-enabled connectivity, we programmed our AQ monitors to send and store data on the ThingSpeak server. ThingSpeak is an Internet-of-Things platform that uses public and private channels to store data sent from apps or devices. I was able to create an account with a unique channel ID and API key. Each data entry, uploaded via WiFi from the Feather Huzzah, could then be stored on the channel with a date, timestamp and unique entry ID.

There are both advantages and drawbacks to these methods of data upload and storage. Most significantly, both the public WiFi and the ThingSpeak platform were available to us free of charge. However, gratuity is not always the case with WiFi-enabled internet use, and this type of data transfer could be problematic in regions without stable WiFi connection, in poor weather conditions, or in the case of equipment malfunctions. Furthermore, WiFi is an inherently insecure method of connectivity, and public networks can easily be compromised by hackers seeking to gain access to this or any data. Since the likelihood of malicious actors trying to manipulate AQ data is low, the risk seems worth the low price tag of public Wifi connectivity. Finally, the feasibility of operating a free account on the ThingSpeak platform might change based on the quantity of data stored or the computational and visualization needs of the user.

Description of code

We programmed the code for our AQ monitor on Arduino’s Integrated Development Environment. In broad strokes, the code begins by including the relevant libraries to interpret the sensor data and transmit it to a Wifi network. Then, it defines the Wifi network name and password. The ‘ThingSpeak settings’ section outlines the unique Channel and API Key where the data should be sent. The DHT-11 sensor is set up to measure temperature and humidity, and its data pin on our Huzzah was defined as DHTPIN 2. The PPD42NS sensor was defined at PPDPIN 16 and is set to calculate PM2.5 ratio measurements at 15 minute intervals (specified in milliseconds). The SSD1306 settings create a binary for turning on and off the display on the monitor, and the utility function is then meant to cycle through text to display. The three following ‘setup functions’ in the code prime the device to begin looking for a WiFi connection, displaying that connection information on the LED screen, and refreshing the process until WiFi is found. The ‘while statement’ here commands that if the Feather Huzzah doesn’t connect to Wifi, it is to wait 1000 milliseconds and try again. After that connection is successful, the loop is set to begin. When the reset button is pressed, the monitoring begins again and the duration of the LOW pulse from the PPD42NS sensor will be tracked. This sets off the 30 second interval during which the ratio of Low:High values is calculated. There is a formula for calculating the PM count, and only after that is calculated are the temperature and humidity values read in the code. This means that all three measurements correspond and are snapshotted for 30 seconds every 15 minutes. The code then displays the data on the LED screen. If the device is connected to the ThingSpeak server via WiFi, it then pushes those three fields of data to the unique channel ID online. The LED display then acknowledges that it “Sent data to ThingSpeak,” and the connection to the server is closed. Finally, the stopwatch and start time are reset so that measurements can begin again with the Low pulse or with a reset.

The master version of the code for our AQ monitors can be found on the GitHub Repository here.

Description of testing

To test the AQ monitor, I placed it in my backyard for a continuous period of 24 hours. The location is in Northwest Washington, DC USA. To allow air flow to enter the device, I taped it above a wall outlet. To sustain a longer experiment, it would be advantageous to bolt the monitor to a vertical surface as the bottom needs airflow. I initially attempted to power the AQ monitor via the solar panel which I taped below the waterproof enclosure, but I was never able to successfully operate the device in that manner. As such, I positioned the outdoor experiment next to a wall socket. This was effective in measuring temperature, humidity, and PM2.5.

Photos of the AQ Monitor Testing in my Backyard

The data successfully uploaded to ThingSpeak during those 24 hours, and I recorded more than 100 data points for each of the three variables. Below is a snapshot of how the data is displayed on ThingSpeak. The customization on ThingSpeak could use improvements, so I also chose to export the data to Excel and look at it there. A screenshot of my results in Excel is also provided below.

Humidity Readings from ThingSpeak, Temperature Readings from Excel

The changes in temperature and humidity observed over the 24 hour period mirrored the weather conditions in Washington, DC on May 5th-6th, although the temperature readings were a bit extreme. Over the course of that day, AccuWeather measured the high and low temperatures as 27 and 16 degrees Celsius, which is partially out of the range measured by our AQ monitor. The average PM2.5 reading was 160.903ppm which is higher than was recorded for the DC area (approximately 60ppm). Still, it is worth noting that my house is on a street with a public bus route and heavy traffic at rush hours. Overall, the humidity, temperature, and PM2.5 readings seemed sporadic but within an acceptable range for experimentation purposes.

Suggestions for improvement

The most significant shortcoming that I would seek to improve would be the solar power component of the device. Stronger solar panels or other power sources should be explored if this device were to prove useful in the long-term. I would also use smaller and higher-quality wires and sensors since their fragility and overflowing arrangement could have compromised the data measurements. To test AQ in an outdoor location again, I would also want to firmly secure the device and solar panel upright. Furthermore, I would try to minimize human contact with the device and sensors. The trial-and-error style of our device construction might have worsened the accuracy of the low-cost, lower-quality sensors. Finally, I might also try to find another data hosting service with more data analysis functions than ThingSpeak. Nonetheless, the AQ monitor provided measurements and learning experience well worth the $147.27 price tag.

Conclusion

The building and programming of this device was a hands-on learning experience that I thoroughly enjoyed. While the theory and functionality behind some of the sensors was at times difficult to grasp, the actual construction and coding were relatively straightforward. Testing the device was also rewarding, and I found it very easy to upload the data to ThingSpeak. Being confident in the data that I did record was less straight forward, as the sensors seemed to vary day-to-day and student-to-student. It made me appreciate the significance of having a set of sensors working together to control for particularly irregular readings from any one. The disappointing performance of the solar panel would also be worth revisiting. Through this process, I recorded some reasonable data points and gained significant insight into the experimental and collaborative nature of AQ measurement worldwide. These kinds of low-cost air quality devices should continue to be perfected and used for scientific and AQ education. Though it doesn’t seem likely that any policy or legal decisions could be based on the accuracy of these devices, they can arm citizens with a general sense of current AQ conditions in a region and how those compare both locally and globally.

--

--