Dust PM2.5 air monitor

PMS5003 particle sensor plus BME280: PM1 / PM2.5 / PM10, temperature and humidity on a web page.

Climate & heating Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 1–2 h

Particles, humidity and the sensor’s fan

The PMS5003 counts particles with a laser and a small fan. The fan has a limited life, so for a permanent installation let it run for 30 seconds every ten minutes: warm up, read a few values, switch off with the SET pin. Its power is 5 V, its serial logic is 3.3 V.

Reading the numbers correctly

At relative humidity over about 85 % droplets grow and the sensor over-reports PM2.5. That is the reason for the BME280 in this project: mark readings taken in humid air or discard them. For reference, the WHO guideline for the daily average of PM2.5 is 15 µg/m³; spikes during cooking are normal and last a few minutes.

  • Put the intake away from the kitchen, and the outlet away from the intake, or the sensor measures its own exhaust.
  • Two sensors, one indoors and one outdoors, tell you when it is smart to open the window.
  • Publish the average of several readings, not each single one.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • PMS5003 particulate sensor
  • BME280 I2C module (3.3V)
  • Dupont jumper wires (M-F, M-M)

Wiring

Module Module pin ESP32 pin Note
PMS5003 VCC / GND 5V / GND
PMS5003 TX UART GPIO16 (RX2)
PMS5003 RX UART GPIO17 (TX2) optional

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WebServer.h>

WebServer server(80);
String buildJson();   // implemented in the project below

String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }

const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";

void startWeb() {
  server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
  server.on("/json", []() { server.send(200, "application/json", buildJson()); });
  server.begin();
}
int pm1 = -1, pm25 = -1, pm10 = -1;
bool readPMS() {
  static uint8_t b[32]; int n = 0; uint32_t t0 = millis();
  while (millis() - t0 < 2000) {
    if (!Serial2.available()) continue;
    uint8_t c = Serial2.read();
    if (n == 0 && c != 0x42) continue;
    if (n == 1 && c != 0x4D) { n = 0; continue; }
    b[n++] = c;
    if (n == 32) {
      uint16_t sum = 0; for (int i = 0; i < 30; i++) sum += b[i];
      if (sum != (uint16_t)(b[30] << 8 | b[31])) return false;
      pm1 = b[10] << 8 | b[11]; pm25 = b[12] << 8 | b[13]; pm10 = b[14] << 8 | b[15];
      return true;
    }
  }
  return false;
}
String buildJson() { return String("{"PM1":") + pm1 + ","PM2_5":" + pm25 + ","PM10":" + pm10 + "}"; }
void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
  connectWiFi();
  startWeb();
}
void loop() {
  server.handleClient();
  static uint32_t t = 0;
  if (millis() - t > 3000) { t = millis(); readPMS(); }
}

Build tips

  • Keep the intake free and mount the sensor vertically away from the ESP32.
  • Run the fan only when measuring to extend sensor life (set pin) if you need long lifetime.

What you need

Buy the parts kit · Estimated total: $23.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
PMS5003 particulate sensor × 1
$15.00
Buy at AliExpress Buy at Amazon.com
BME280 I2C module (3.3V) × 1
$3.00
Buy at AliExpress Buy at Amazon.com
Dupont jumper wires (M-F, M-M) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Other countries: order these parts on AliExpress with the links below.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

Temperature & humidity station

BME280 plus an OLED screen: temperature, humidity and pressure on the display, a web page and Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1 h
Pick parts / kit

Network cabinet temperature watch

Two probes at the top and the bottom of a closet with the router, switch and NAS, and an alarm before the equipment overheats in summer.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit

CO2 monitor

MH-Z19B CO2 meter with an OLED and a ventilation alert, handy for bedrooms and offices.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.