
Quick answer: An ESP32 air quality monitor uses a laser-scattering particle sensor (PMS5003 or SPS30) to measure PM2.5 and PM10, a true NDIR sensor (Sensirion SCD41) for CO2, and an SHT40 for temperature and humidity. The ESP32 corrects the readings for humidity, converts PM2.5 to the US AQI and sends the data over Wi-Fi. If you put several of these nodes around Dhaka, you get a live colour-coded map of the city’s air.
🌐 এই লেখাটি বাংলায় পড়ুন: বাংলা সংস্করণ →
This is a reference design for a final-year or M.Sc. project: physics, verified circuit, pin-by-pin wiring, BDT bill of materials, calibration, AQI maths and the firmware architecture, with about half of the code. Sensor figures come from the datasheets. Results are described as expected behaviour, not as a lab report.
Table of Contents
Why an IoT air quality monitoring system matters in Dhaka (2026)
In IQAir’s 2024 World Air Quality Report, Bangladesh ranked 2nd most polluted country and Dhaka 3rd most polluted city, with an annual average PM2.5 of 78 µg/m³. The WHO guideline is 5 µg/m³. The 2025 edition, published in March 2026, again placed Bangladesh second at 66.1 µg/m³. A World Bank study estimated 78,145–88,229 deaths in Bangladesh in 2019 from air pollution, and an economic cost of 3.9–4.4% of GDP.
The Department of Environment (DoE) runs Continuous Air Monitoring Stations (CAMS). These are expensive reference instruments, so the network is sparse and cannot show street-level differences, for example between a Mirpur intersection and a quiet lane nearby. That gap is why low-cost sensor networks are such an active research area. For students, it combines embedded hardware, sensor physics, statistics and a web map.
How a low-cost air quality monitor works
Laser scattering PM2.5 sensors
Inside a PMS5003 or SPS30, a fan pulls air through a laser beam. Each particle scatters a short flash of light onto a photodiode, and the pulse height depends on particle size. The sensor sorts pulses into size bins (the PMS5003 reports counts above 0.3, 0.5, 1.0, 2.5, 5 and 10 µm per 0.1 L), then converts counts to mass (µg/m³) by assuming a shape and density. The PM2.5 value is therefore an estimate that shifts with aerosol type, so it needs calibration against a reference.
PM2.5 humidity correction: why fog and monsoon make it read high
Sulfate, nitrate and some organic particles absorb water. At high relative humidity they swell and are counted as bigger and heavier. Reference monitors dry the sample; low-cost sensors don’t. Two published corrections:
- EPA US-wide PurpleAir correction (Barkjohn et al., 2021):
PM2.5 = 0.524 × PA_cf1 − 0.0862 × RH + 5.75. It uses the CF=1 output of Plantower sensors and cut the RMSE from 8 to 3 µg/m³ in US data. The coefficients come from US aerosol, so use the form of the equation and fit your own coefficients for Dhaka. - κ-Köhler correction (Crilley et al., 2020): divide the raw mass by
C = 1 + (κ/1.65)/(−1 + 1/aw), where aw = RH/100. The authors say data above about 60% RH needs correction, and they report a global κ of 0.33 as a fallback when no reference is available.
True NDIR CO2 (SCD41), not MQ-135 “CO2”
The SCD41 is a photoacoustic NDIR sensor: it measures absorption in the 4.26 µm CO2 infrared band, so it responds to CO2 specifically. Per the Sensirion SCD41 page and datasheet, the specified range is 400–5,000 ppm, with ±(50 ppm + 2.5% of reading) accuracy between 400 and 1,000 ppm. The MQ-135 is a heated metal-oxide sensor that reacts to many gases (alcohol, ammonia, smoke), so its “CO2 ppm” is a curve-fit guess. One caveat: outdoor CO2 varies by only tens of ppm, close to the SCD41’s accuracy, so outdoors it is mainly a traffic and combustion indicator. Indoors (classrooms, factory floors) it is very useful.
PMS5003 vs SPS30: which PM2.5 sensor for ESP32?
| Parameter | Plantower PMS5003 / PMS7003 | Sensirion SPS30 |
|---|---|---|
| Supply | 5.0 V (4.5–5.5 V), fan needs 5 V | 5.0 V ±10% |
| Logic level | 3.3 V TTL UART (direct to ESP32) | Accepts 3.3 V or 5 V, outputs ~3.3 V |
| Interface | UART 9600 8N1, 32-byte frame | UART 115200 (SHDLC) or I2C 0x69 (SEL to GND) |
| Active current | ≤100 mA | 45–65 mA (80 mA at fan start) |
| PM2.5 precision | ±10 µg/m³ (0–100), ±10% (100–500) | ±10 µg/m³ (0–100), ±10% (100–1000) |
| Extras | Sleep via SET pin, passive mode | Auto fan-cleaning weekly, >10-year MTTF |
Our pick for a student network is the PMS5003: cheap, well documented, and the same family as PurpleAir, so the EPA correction form fits. Choose the SPS30 for a premium or M.Sc. version. Neither needs a level shifter with an ESP32; both need a solid 5 V supply. Values are from the PMS5003 data manual and the Sensirion SPS30 datasheet.
System block diagram

Each node samples, corrects, computes the AQI, shows it on an OLED and RGB LED, and publishes JSON over MQTT. On the server, Mosquitto receives it, Telegraf (or a Python bridge) writes to InfluxDB, Grafana draws trends, and a Leaflet page shows each node as a coloured circle on a Dhaka map. For a quick demo, ThingSpeak can replace the server side.
ESP32 air quality monitor circuit diagram

The board is an ESP32-DevKitC (ESP32-WROOM-32E). Pin names come from Espressif’s DevKitC user guide. GPIO16/17 are reserved on WROVER boards, so use a WROOM. Sensor pins follow the PMS5003 manual and Sensirion SCD4x/SHT4x datasheets.
| From | To (ESP32-DevKitC header) | Notes |
|---|---|---|
| DC jack +5 V → F1 1.1 A PTC → D1 SS34 | +5V rail → 5V (J2-19) | D2 SMBJ5.0A TVS + C1 470 µF to GND. Don’t power from USB and the 5V pin at the same time. |
| GND | GND (J2-14, J3-1) | Star ground near the DC jack |
| U2 AP2112K-3.3: VIN(1), EN(3) ← +5V; GND(2); VOUT(5) | 3V3_S sensor rail | 1 µF in, 1 µF + 10 µF out. A separate quiet rail for the SCD41 (30 mV ripple limit) |
| PMS5003 PIN1 VCC / PIN2 GND | +5V / GND | C6 100 µF at the connector (fan inrush) |
| PMS5003 PIN5 TX | IO16 (J3-12) = UART2 RX | 3.3 V TTL, direct |
| PMS5003 PIN4 RX | IO17 (J3-11) = UART2 TX | 3.3 V TTL, direct |
| PMS5003 PIN3 SET | IO4 (J3-13) | LOW = sleep (internal pull-up) |
| PMS5003 PIN6 RESET | IO18 (J3-9) | Active LOW, for fault recovery |
| I2C SDA (SCD41, SHT40, OLED) | IO21 (J3-6) | R1 4.7 kΩ to 3V3_S |
| I2C SCL (SCD41, SHT40, OLED) | IO22 (J3-3) | R2 4.7 kΩ to 3V3_S |
| SCD41 VDD + VDDH / GND | 3V3_S / GND | VDDH must be tied to VDD. 10 µF + 100 nF. Address 0x62 |
| SHT40 VDD / VSS | 3V3_S / GND | 1.08–3.6 V only. 100 nF. Address 0x44 |
| SSD1306 0.96″ OLED VCC / GND | 3V3_S / GND | Address usually 0x3C (check module jumper) |
| RGB LED (common cathode) R / G / B | IO25 (J2-9) / IO26 (J2-10) / IO27 (J2-11) | Via R4 220 Ω / R5 100 Ω / R6 100 Ω |
Bill of materials (approx. BDT)
| Part | Qty | Approx. cost (BDT) |
|---|---|---|
| ESP32-DevKitC (WROOM-32E) or equivalent | 1 | 650–900 |
| Plantower PMS5003 (with cable) | 1 | 2,200–3,200 |
| Sensirion SCD41 module (SCD40 cheaper, 400–2,000 ppm) | 1 | 4,500–6,500 |
| SHT40 breakout | 1 | 450–750 |
| 0.96″ SSD1306 I2C OLED | 1 | 300–450 |
| AP2112K-3.3, SS34, SMBJ5.0A, PTC fuse, caps, resistors, RGB LED | 1 set | 200–350 |
| 5 V 2 A adapter + DC jack | 1 | 300–450 |
| IP65 box + radiation shield (plates / DIY saucer stack) | 1 | 600–1,200 |
| Custom PCB (share of a 5-pc JLCPCB order) | 1 | 300–500 |
| Total per node | ≈ 9,500–14,300 |
Prices are approx., Nov 2026, local market (Patuatuli, Stadium Market and online shops in Dhaka). They vary with the exchange rate. Clone and relabelled sensor modules are common, so read how to spot fake electronic components before you buy.
How to calculate AQI from PM2.5 (2024 EPA breakpoints)
The US EPA revised the PM2.5 AQI breakpoints effective 6 May 2024. The “Good” ceiling dropped from 12.0 to 9.0 µg/m³, and the two Hazardous bands were merged (EPA fact sheet; 40 CFR Part 58 Appendix G). Many tutorials still use the old table.
| PM2.5 (µg/m³, 24-h) | AQI | Category | LED colour |
|---|---|---|---|
| 0.0–9.0 | 0–50 | Good | Green |
| 9.1–35.4 | 51–100 | Moderate | Yellow |
| 35.5–55.4 | 101–150 | Unhealthy for Sensitive Groups | Orange |
| 55.5–125.4 | 151–200 | Unhealthy | Red |
| 125.5–225.4 | 201–300 | Very Unhealthy | Purple |
| 225.5–325.4 (AQI 500 at 325.4) | 301–500 | Hazardous | Maroon |
First truncate the concentration to one decimal place, then interpolate linearly: AQI = (I_hi − I_lo)/(C_hi − C_lo) × (C − C_lo) + I_lo, rounded to an integer. The AQI is defined on a 24-hour average. For live display, AirNow uses the NowCast: a weighted average of the last 12 hourly values with weight w = c_min/c_max (floored at 0.5), valid when 2 of the last 3 hours exist. When pollution changes fast, recent hours dominate.
Bangladesh DoE AQI: the DoE (real-time AQI from CAMS since Nov 2023) uses the same category names, but sets its own PM2.5 breakpoints, which critics say need revision; the national PM2.5 standard became 35 µg/m³ in 2022. Show the US AQI for international comparison. If you also show a DoE value, take its breakpoints from the current DoE publication and label which index you display.
Step-by-step build guide
Step 1: Build the power stage and PCB
Build the input chain (PTC fuse, Schottky, TVS, bulk capacitor) and check the 3.3 V sensor rail before fitting any sensor. Keep the SCD41 and SHT40 away from the ESP32 and LDO, which are heat sources. Our KiCad 10 to JLCPCB guide shows how to order the board.
Step 2: Wire the PMS5003 and confirm frames
Connect TX→IO16 and RX→IO17. In default active mode it sends a 32-byte frame starting 0x42 0x4D about every 2.3 s. Print the raw bytes, verify the 16-bit checksum, and discard the first 30 s after wake-up, as the manual advises.
Step 3: Bring up the I2C sensors
An I2C scan should show 0x3C (OLED), 0x44 (SHT40) and 0x62 (SCD41). A missing device usually means missing pull-ups or swapped SDA/SCL. Set SCD41 altitude or ambient pressure; tune its temperature offset only after the box is sealed.
Step 4: Enclosure and radiation shield

Put the SHT40 and SCD41 in a louvered radiation shield so sunlight doesn’t heat them. Mount the PMS5003 with its inlet facing down behind a rain baffle, with inlet and outlet separated so it doesn’t re-breathe its exhaust; vents must be no smaller than the sensor inlet. We suggest 2–3 m height, away from generator exhausts and kitchen vents.
Step 5: Co-location calibration with linear regression
Place all nodes within a few metres of a reference monitor for 2–4 weeks, ideally a DoE CAMS station (ask permission) or a university research-grade instrument. The U.S. Embassy Dhaka monitor (listed on IQAir) is useful for regional checks, but you cannot co-locate there. Average to hourly values and fit PM_ref = a·PM_raw + b·RH + c (the Barkjohn form). Hold out one week for validation and report R², RMSE and mean bias. A calibrated “golden” node can then transfer the calibration to the others.
Step 6: Data upload and the live Leaflet map
Each node publishes JSON (ID, lat/lon, raw and corrected PM, CO2, T, RH, AQI, RSSI) to aq/dhaka/<node>. InfluxDB stores it; a small REST endpoint returns the latest point per node; a Leaflet page draws circleMarkers in AQI colours with a popup. Nodes silent for over 15 minutes turn grey.
Step 7: Deploy the network across Dhaka
Pick contrasting sites (main road, residential lane, school, park, industrial edge) and log each site’s height and surroundings. Re-check one node against the reference every few months; fans clog and sensors drift.
Firmware architecture (the whole design, explained)
Arduino-ESP32 on FreeRTOS: five tasks linked by queues, with a mutex-protected “latest record”.
| Module / task | What it does | Timing |
|---|---|---|
| pmTask (core 1) | State machine SLEEP → WAKE (30 s warm-up) → SAMPLE (10 frames, median) → SLEEP. Checksum check, stuck-value and zero-flow detection, RESET pin recovery | Every 120 s |
| envTask | SCD41 periodic measurement (CO2), SHT40 high-precision T/RH, sanity limits | 5 s |
| procTask | 1-min record → RH correction → calibration (a, b, c from NVS) → hourly bins → NowCast → AQI → LED + OLED | 60 s |
| netTask (core 0) | Wi-Fi manager, NTP time, MQTT publish with QoS 1, LittleFS ring buffer for offline data, back-fill on reconnect | 60 s + events |
| sysTask | Task watchdog, heap/RSSI health message, OTA update, remote config (interval, coefficients) | 10 min |
Data flow: raw frames → per-cycle median → 1-minute record (raw + corrected) → hourly mean (needs ≥75% valid PM cycles) → 12-hour NowCast buffer → AQI. Raw values are always uploaded beside corrected ones, so you can re-calibrate history later. Sleeping the PMS5003 for about 80 s of every 120 s cycle cuts fan running time by roughly two-thirds, which helps the fan last longer in dusty Dhaka air.
ESP32 air quality monitor code (about 50% shown)
Shown below: config, PMS5003 frame reader, SCD41 start-up, the 2024 AQI function and the task skeleton, about half of the firmware. Calibration, NowCast, fault logic, MQTT/offline storage, map server and OTA are withheld. The SCD41 calls follow Sensirion’s official arduino-i2c-scd4x library example.
// config.h - ESP32-DevKitC (WROOM-32E) pin map
#pragma once
#define PIN_PMS_RX 16 // ESP32 RX2 <- PMS5003 PIN5 TX
#define PIN_PMS_TX 17 // ESP32 TX2 -> PMS5003 PIN4 RX
#define PIN_PMS_SET 4 // LOW = sleep
#define PIN_PMS_RESET 18 // active LOW
#define PIN_SDA 21
#define PIN_SCL 22
#define PIN_LED_R 25
#define PIN_LED_G 26
#define PIN_LED_B 27
#define PMS_WARMUP_MS 30000UL // datasheet: stable data 30 s after wake
#define PMS_CYCLE_MS 120000UL
// Read one 32-byte PMS5003 frame, verify checksum, return CF=1 values
struct PmData { uint16_t pm1, pm25, pm10; };
bool readPmsFrame(HardwareSerial &s, PmData &out, uint32_t timeoutMs = 3000) {
uint8_t b[32];
uint32_t t0 = millis();
while (millis() - t0 < timeoutMs) {
if (s.available() < 1) { delay(5); continue; }
if (s.read() != 0x42) continue; // start char 1
if (s.readBytes(&b[1], 31) != 31) return false;
b[0] = 0x42;
if (b[1] != 0x4D) continue; // start char 2
uint16_t sum = 0;
for (int i = 0; i < 30; i++) sum += b[i];
if (sum != ((b[30] << 8) | b[31])) return false;
out.pm1 = (b[4] << 8) | b[5]; // Data1 PM1.0 CF=1
out.pm25 = (b[6] << 8) | b[7]; // Data2 PM2.5 CF=1
out.pm10 = (b[8] << 8) | b[9]; // Data3 PM10 CF=1
return true;
}
return false;
}
#include <Wire.h>
#include <SensirionI2cScd4x.h>
SensirionI2cScd4x scd4x;
bool initScd41() {
Wire.begin(PIN_SDA, PIN_SCL);
scd4x.begin(Wire, SCD41_I2C_ADDR_62);
scd4x.wakeUp();
scd4x.stopPeriodicMeasurement();
scd4x.reinit();
return scd4x.startPeriodicMeasurement() == 0; // new value every 5 s
}
bool readScd41(uint16_t &co2, float &t, float &rh) {
bool ready = false;
if (scd4x.getDataReadyStatus(ready) != 0 || !ready) return false;
return scd4x.readMeasurement(co2, t, rh) == 0;
}
// US AQI from PM2.5 - EPA breakpoints revised 6 May 2024
struct Bp { float cLo, cHi; int iLo, iHi; };
static const Bp PM25_BP[] = {
{0.0f, 9.0f, 0, 50}, {9.1f, 35.4f, 51, 100},
{35.5f, 55.4f, 101, 150}, {55.5f, 125.4f, 151, 200},
{125.5f, 225.4f, 201, 300}, {225.5f, 325.4f, 301, 500}};
int aqiFromPm25(float c) {
if (c < 0) return -1;
c = floorf(c * 10.0f) / 10.0f; // truncate to 0.1
for (int i = 0; i < 6; i++) {
const Bp &b = PM25_BP[i];
if (c <= b.cHi || i == 5) // >325.4 extrapolates
return lroundf((b.iHi - b.iLo) / (b.cHi - b.cLo) * (c - b.cLo) + b.iLo);
}
return -1;
}
void setup() {
Serial.begin(115200);
pinMode(PIN_PMS_SET, OUTPUT); digitalWrite(PIN_PMS_SET, HIGH);
pinMode(PIN_PMS_RESET, OUTPUT); digitalWrite(PIN_PMS_RESET, HIGH);
Serial2.begin(9600, SERIAL_8N1, PIN_PMS_RX, PIN_PMS_TX);
initScd41();
xTaskCreatePinnedToCore(pmTask, "pm", 4096, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore(envTask, "env", 4096, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore(procTask, "proc", 6144, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(netTask, "net", 8192, NULL, 1, NULL, 0);
// sysTask (watchdog, OTA, remote config) - in full firmware
}
void loop() { vTaskDelay(portMAX_DELAY); }
Want the complete production firmware? The full project includes the PMS5003 sleep/wake state machine with fault recovery, the humidity correction and co-location regression script (Python and on-device coefficients), the NowCast engine, MQTT with offline store-and-forward, InfluxDB/Grafana dashboards, the Leaflet live map server, OTA updates, alerts and data logging. It also includes KiCad files and the enclosure drawing. Message us on WhatsApp to get the full source code, circuit files and help building it.
Testing, expected accuracy and troubleshooting
Two PMS5003s side by side should agree within the datasheet consistency spec (±10 µg/m³ below 100 µg/m³). Against a reference, raw readings typically run high at high humidity. In our experience with similar sensor projects, a co-location fit is clearly better on dry winter days than in monsoon fog, so check your residuals by season. Report your own numbers honestly. For CO2, check fresh outdoor air (around 420 ppm) and a breath test (it should jump into the thousands).
| Symptom | Likely cause | Fix |
|---|---|---|
| No PMS frames | TX/RX swapped, SET held LOW, WROVER board on GPIO16/17 | Cross TX→RX, check SET HIGH, use WROOM |
| Frequent checksum errors | Long or unshielded UART cable, weak ground | Keep cable <20 cm, solid GND, read whole frame |
| PM2.5 jumps in fog/rain | Hygroscopic growth, water droplets | RH correction; flag RH >95% data |
| Brown-out resets on Wi-Fi TX | Weak adapter, no bulk cap | 2 A supply, 470 µF, short thick 5 V traces |
| I2C device missing | No pull-ups, swapped lines | 4.7 kΩ to 3V3_S, I2C scan |
| Temperature reads 3–6 °C high | Self-heating, sun on box | SHT40 in radiation shield, away from ESP32 |
| CO2 slowly drifts | ASC needs ~400 ppm weekly | Fine outdoors. Indoors use forced recalibration |
Safety and data-honesty notes
- 5 V only: use a certified adapter, with the mains socket in a weatherproof box.
- Don’t open the PMS5003; it contains a laser.
- This is not a certified regulatory or medical device. Label the map “indicative, low-cost sensor data” and give no health advice beyond standard AQI messages.
- Install safely, with the owner’s permission, never on electricity poles.
Future upgrades and real-world uses
- Solar + LTE nodes for sites without Wi-Fi (A7670/SIM7600 modules, 18650 + 10 W panel).
- Machine-learning calibration (random forest with RH, T, hour of day), a strong M.Sc. thesis; see our M.Sc. thesis topics.
- Gas add-ons: electrochemical NO2/O3/CO sensors.
- Products: classroom CO2 monitors and factory dust monitors.
- Paper: “Spatial variability of PM2.5 in Dhaka from a calibrated low-cost network”.
For more ideas, see final-year EEE project ideas and our ESP32 smart energy meter (NILM) project, which uses the same MQTT + InfluxDB stack.
Frequently asked questions
PMS5003 (budget) or SPS30 (long life, I2C) for PM2.5; an NDIR SCD40/SCD41 for CO2; an SHT40 in a radiation shield for temperature and humidity.
The datasheet consistency is ±10 µg/m³ up to 100 µg/m³ and ±10% above that. Uncorrected, they over-read at high humidity (about 40% in EPA’s US data). After correction and local calibration they are good for mapping and trends, not regulatory compliance.
Not reliably. It is a metal-oxide sensor that responds to many gases and to humidity, so its “ppm CO2” is only a curve estimate. Use an NDIR sensor.
Truncate the 24-hour average to 0.1 µg/m³, find its band in the 2024 EPA table and interpolate linearly. For live displays use the 12-hour NowCast instead.
Ask a DoE CAMS station or university lab for a short co-location. Otherwise apply a published RH correction (κ ≈ 0.33, Crilley et al.), compare with the nearest public station on regionally uniform days, and state that this is a weaker calibration.
Yes, if you go beyond a breadboard: multiple nodes, a real enclosure, humidity correction, calibration with error metrics and a live map make it an excellent EEE, ETE or CSE project.
Conclusion
A credible ESP32 air quality monitor is not hard to build. What sets it apart is honest physics and statistics: humidity-corrected laser PM, real NDIR CO2, the 2024 AQI breakpoints, co-location calibration and a map that shows how Dhaka’s air changes street by street. Build one node carefully, calibrate it, then scale to five.
Want to build a calibrated air quality network for your final-year project or your organisation? We design custom PCBs, firmware and complete working prototypes for students and companies. Message us on WhatsApp or use the contact form. You can write in Bangla or English.