It’s surprisingly easy to make your own simple air quality sensor. All you need is a cheap laser-scattering particulate matter sensor, a Wemos D1 Mini, a soldering iron, and Tasmota.
Part 1 showed how to make the simplest possible air quality sensor. Make sure you’ve seen that first, because Part 2 continues from Part 1 to add a 128×32 pixel OLED display and a mode button. We’re also going to install custom firmware to make the sensor last longer.
- Plantower PMS5003 particular matter sensor
- Wemos D1 Mini with ESP8266
- 128×32 I2C OLED
- Tactile pushbutton switch 6x6x9mm
- Firmware source code
- 3D-printed enclosure (download STLs or buy pre-printed)
- RD6006 lab power supply with WiFi (not required, but it’s cool so I’m linking to it anyway!)
- Discuss this project on Discord
3D print the case
Printing a case is totally optional, of course. You can use whatever enclosure you like. My case has been designed to be a press-fit over the PMS5003, which holds the two halves together by friction. See the link above to download the STLs if you want to print it yourself, or you can buy a case from me if you don’t have access to a printer. I’ll include a 6x6x9mm tact switch with the “Display” version of the case:
Put switch into case
The space for the tact switch is very tight.
To fit it into the slot, trim off the pins from the right side and trim the pins on the left side so they are a couple of millimeters long. Bend those leads out at a 45 degree angle, and solder a pair of jumper wires to them.
Push the switch into the cavity in the case as shown above, and guide the wires around the slot to the left and then down.
The button connects between GND and I/O pin D3:
Connect 128×32 OLED to D1 Mini
The 0.91″ 128×32 OLED module already has I2C pull-ups included, so there’s no need to add them. All we need to do is connect power and I2C:
The connections are:
- OLED GND to D1 Mini GND
- OLED VCC to D1 Mini 3.3V
- OLED SCK to D1 Mini pin D1
- OLED SDA to D1 Mini pin D2
Connect PMS5003 to D1 Mini
In the “Basic” version of this project, we only needed to connect to the 5V, GND, and Tx pins of the PMS5003. Now we also need to connect I/O pin D6 to the sensor’s Rx pin, to allow commands to be sent to the sensor:
The connections are:
- PMS5003 pin 1 (5V) to D1 Mini 5V
- PMS5003 pin 2 (GND) to D1 Mini GND
- PMS5003 pin 4 (Rx) to D1 Mini pin D6
- PMS5003 pin 5 (Tx) to D1 Mini pin D4
Close the case
This can be more tricky than it sounds! Be gentle so you don’t damage anything.
With the electronics sitting neatly in the “back” (or “right”) half of the case, make sure all the wiring is neatly tucked into the slots. It should look something like this:
Slide the other half of the case over the modules, checking that no wires are pinched between the halves. The case should close completely.
Set up Arduino IDE for ESP8266
If you don’t already have it, download and install the Arduino IDE:
By default, the Arduino IDE does not support the ESP8266 processor on the D1 Mini. To add this support, go to the following URL and follow the instructions. The easiest method is to use the Boards Manager:
The sketch uses 4 libraries.
The “PMS” library is directly embedded within the program, so you don’t need to install it. This is done to force a specific version of the library, which was forked from the official version by GitHub user SwapBap to add extra features. There’s nothing you need to do because it’s part of the project.
In the Arduino IDE, go into “Sketch -> Include Library -> Manage Libraries…“
Then search for each of these libraries, and click “Install”:
- GFX library by Adafruit
- SSD1306 library by Adafruit
- PubSubClient library by Nick O’Leary
If you need more help, check out this tutorial:
Download “AirQualitySensorD1Mini” sketch
Download the AirQualitySensorD1Mini sketch from:
The easiest way is to click the green “Clone or download” button near the top right of the page and select “Download ZIP”. Once you’ve downloaded it, extract the folder and put it in your sketchbook directory. You can find the correct location for this by looking in the Arduino IDE preferences.
Configure and compile sketch
Open the Arduino IDE, open the sketch, and have a look at the 4 tabs across the top. You can ignore the first three. Open the “config.h” tab, and modify the settings for WiFi and your MQTT broker:
Plug in the Air Quality Sensor using a USB cable.
Select “Tools -> Board:” and set it to “LOLIN(WEMOS) D1 R2 & mini”. Select the port, and upload the sketch:
After the sketch has uploaded the Air Quality Sensor will reboot, connect to your WiFi and MQTT broker, and show values on the display once it has successfully received data from the PMS5003.
Stay tuned for part 3, which will explain how the sketch works and how to read the data from it.