Wi-Fi PM2.5 Sensor with PMS5003 and ESP01
Introduction
The pollution in Bangkok is quite bad in winter and last year I’ve bought a PM2.5 sensor to check how much pollution is actually in the air. The sensor communicates through serial protocol and I’ve previously built an Android application to read data from it.
But now I’m looking for something more autonomous that can give me readings without connecting to a phone. In addition, it would be very useful to be able to retrieve historical data via Wi-Fi in case of projects such as testing my air purifier.
So I’ve bought a micro-controller, a screen and wired everything up together. This is the final product:
Components
The following are the main components, all of which can be found on Aliexpress.
- PMS5003 (~$13) — This is the air quality sensor from Plantower. It can be substituted by other models such as PMS3003, PMS7003 or PMSA003.
- ESP01 (~$2) — A surprisingly cheap micro-controller with built-in Wi-Fi. The I/O is a little bit limited but it’s good enough for our use-case.
- SSD1306 (~$2) — A black-white OLED screen that can be driven by 2 wires and thus compatible with the ESP01.
- AMS1117 (<$1) —A step-down DC-DC converter to bring power from 5V to the 3.3V required by the ESP01 and SSD1306
Of particular interest is the ESP01 micro-controller. It has only 4 IO ports. Two serial TX/RX port and two GPIO ports. The two GPIO ports will be used to connect to our OLED screen. The RX port will be used to receive data from the PMS5003 sensor. Finally, the TX port can either be used to send commands to the PMS5003 sensor or for debugging. Since the PMS5003 will continuously send update without sending to send commands to it, I chose to use the TX port for debugging otherwise it’d be nearly impossible to find out what’s going on when something’s not working.
Wiring
The components need to be wired up as follow. Note that I this is my first “real” hardware project so it might not be the most optimized way.
For the power source, I’ve spliced an unused USB cable to get 5V and GND out of it and plug the other half into an old portable battery.
Software
The software I’ve written for the module can be found on GitHub. It is written in Arduino and the following packages are required:
One thing I’ve learned during the development is that it’s extremely annoying to have to disconnect the ESP01 from the sensor and connect it to the computer to flash an update then connect the sensor back again to test. Thus, one of the first thing you should do is implement over-the-air (OTA) firmware update so you can easily upload a new firmware via Wi-Fi. That greatly sped up my development speed.
If you would like to replicate this project, it’s enough to check out the GitHub repository, compile it in Arduino SDK and upload the firmware.
Usage
If everything is working correctly, you should be able to see the current PM2.5 value on the LCD screen.
You should also be able to see a Wi-Fi network named “ESP-XXXXX” with no password. (If you don’t, it means that you’ve configured Wi-Fi previously. Set “Erase Flash” to “Sketch + Wi-Fi Settings” in Arduino IDE and re-flash to restore the settings to default.) After connecting to the network, access 192.168.4.1 in a web browser and you should be able to see a screen similar to the following:
The “PM 2.5 History” menu can be used to retrieve up to the last 24 hours of history since last boot.
The “Wi-Fi setup” page can be used to connect the ESP01 to your home Wi-Fi network so you can access the information without connecting to a separate network. The “SoftAP setup” page can be used to configure the wireless hotspot to secure it.
Finally, the “Firmware Update” page can be used to upload a new firmware. To generate the file for uploading, use Sketch -> Export Compiled Binary from Arduino IDE.
Conclusion
Now we have a fully functional and portable PM2.5 sensor that can even record the history. Future improvements can include:
- Periodically uploading the result to a server to store it long-term.
- Store the history on the flash memory to not lose it on reboot.
- Soldering the connections on a prototype board to improve reliability.