กลับหน้าหลัก
views
How to Read PM2.5 Dust Sensor PMS5003 G5 with ESP32
Last updated on

How to Read PM2.5 Dust Sensor PMS5003 G5 with ESP32


How to Read PM2.5 Dust Sensor PMS5003 G5 with ESP32

This tutorial walks through using ESP32 to read PM2.5 dust levels with the PMS5003 G5 laser dust sensor. We cover wiring, library installation, code upload, and reading results on the Serial Monitor.

Required Components

  • ESP32 NodeMCU ESP-WROOM-32 Wi-Fi + Bluetooth Dual Core with CP2102
  • PMS5003 G5 Laser Dust Sensor — reads PM1.0 / PM2.5 / PM10
  • Breadboard 170 tie points, white
  • Micro USB Cable Type B to USB 2.0 Type A, 1m
  • Power Adapter Micro USB 5V 2A
  • Jumper Wires M-M, M-F, F-F, 20cm each, 40 pieces each type
All components laid out on a workbench with labels for each part

Wiring ESP32 to PMS5003 G5

ESP32PMS5003 G5
5VVCC
GNDGND
D21TXD
D22RXD

Key rule: TXD on the sensor connects to the pin we define as RX in code (D22), and RXD on the sensor connects to the TX pin (D21). Swapping these results in corrupted data.

Clear wiring diagram showing connections between ESP32 and PMS5003 on a breadboard, labeled with pin names

Installing the SoftwareSerial Library for ESP32

ESP32 has two Hardware Serial ports; the primary one is already used for USB and Serial Monitor. SoftwareSerial lets us create a virtual serial port on any GPIO pins to talk to the sensor.

  1. Download Esp32-SoftwareSerial-master.zip
  2. Extract and place the folder in Documents/Arduino/libraries/
  3. Restart the Arduino IDE

If you are not comfortable installing libraries in Arduino IDE, check the guide on How to Install Arduino Libraries first.

Library folder structure after extraction, confirming correct placement in the libraries directory

Code Skeleton for Reading PMS5003 with ESP32

#include <SoftwareSerial.h>

// Define SoftwareSerial pins
// Sensor TX -> ESP32 D22 (our RX)
// Sensor RX -> ESP32 D21 (our TX)
#define PMS5003_RX 22
#define PMS5003_TX 21

SoftwareSerial pmsSerial(PMS5003_RX, PMS5003_TX);

void setup() {
  Serial.begin(115200);
  pmsSerial.begin(9600);  // PMS5003 uses 9600 baud
}

void loop() {
  // Wait for data from sensor
  if (pmsSerial.available()) {
    int rawData = pmsSerial.read();
    
    // PMS5003 sends data in 32-byte binary frames
    // Refer to the datasheet for frame structure
    // Write a parse function to extract PM1, PM2.5, PM10 values
    Serial.print("Raw: ");
    Serial.println(rawData, HEX);
  }
}

The code above is a skeleton that gets ESP32 receiving raw data from the PMS5003 via SoftwareSerial. To extract the PM values, you need to implement frame parsing based on the PMS5003 datasheet protocol. The sensor communicates in binary frames, so you must read a complete 32-byte frame before decoding PM1, PM2.5, and PM10 values.

How to Upload Code to ESP32

  1. Open Serial Monitor first, then click Sketch → Upload
  2. During upload, if it hangs at ... Connecting ..., hold the BOOT button on the board for a moment
  3. Wait for Hard resetting via RTS pin... to confirm successful upload

[image: Arduino IDE upload progress window showing successful completion with Hard resetting message]

Reading Results on the Serial Monitor

Set the Serial Monitor Baud Rate to 115200. You should see three key measurements:

  • PM1.0 — particles smaller than 1 micron
  • PM2.5 — particles smaller than 2.5 microns
  • PM10 — particles smaller than 10 microns

[image: Serial Monitor window displaying PM1, PM2.5, and PM10 values clearly boxed for visibility]

Testing the Sensor with Soldering Smoke

A simple functional test: burn solder to produce smoke and let it drift near the sensor. PM values will spike immediately and gradually return to baseline as the smoke disperses. A working sensor should show a clear, dramatic change.

[image: Before and after comparison of PM values during and after smoke exposure test]

Quick Checklist

  1. Wire 5V→VCC, GND→GND, D21→TXD, D22→RXD
  2. Place Esp32-SoftwareSerial in the libraries folder
  3. Upload code (hold BOOT if upload stalls)
  4. Open Serial Monitor at 115200 to view PM1 / PM2.5 / PM10
  5. Confirm functionality by testing with solder smoke if needed

Reference Video

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น