< All Topics
Print

Measuring Particulate Matter Using a PMS5003 Sensor & Arduino UNO

Tutorial Aim:

The goal of this tutorial is to measure & display the number of suspended particles in the air (particle concentration) using a PMS5003 air quality sensor with an Arduino UNO.

Requirements:

This tutorial makes use of the Arduino IDE 2.3

  • Arduino IDE
  • Arduino (Compatible) UNO R3 with USB cable
  • PM2.5 Air Quality Sensor Module – PMS5003
  • 4 MM Jumper Wires

PMS5003 Pin Layout:

Please note that the pins for a PMS5003 are numbered right to left as seen in the image below. In this tutorial we’ll be ignoring PMS5003 pins 3 & 6.

 

PinFunctionDescriptionRemarksArduino Pin
1VCCSupply voltage 5V4.5 – 5.5V5V
2GNDGround GND 
3SETHIGH or SUSPENDED – work mode
LOW – sleep mode
3.3V logicN/A
4RXDUART/TTL data recieve3.3V logic2
5TXDUART/TTL data transmit3.3V logic3
6ResetLOW to reset3.3V logicN/A
7NCNot connected N/A
8NCNot connected N/A

Setup:

Refer to the image below for wiring.

  • Connect the current sensor’s RX pin to pin 2 & TX pin to pin 3 on the Arduino UNO using the FM jumper wires. 
  • Open Arduino IDE
  • Plug in the Arduino UNO to the computer

Code Walk Through:

Libraries:

To access & communicate with the sensor we include the SoftwareSerial library, so that we can use Serial communication with the digital pins. For information about the SoftwareSerial library, please visit the Arduino doc page.

The struct pms5003 is designed to contain all the sensor outputs, as well as the checksum so that we can check if the data was successfully read.

Particulate Matter Sensor Code:

Entire tutorial code for your reference. Please note that the code is available in the download section.

#include <SoftwareSerial.h>

// RX pin: 2, TX pin: 3
SoftwareSerial pms(2, 3);

/* Struct containing all the sensor outputs */
struct pms5003 {
  uint16_t framelen;
  uint16_t pm10, pm25, pm100;
  uint16_t pm10_env, pm25_env, pm100_env;
  uint16_t p_03um, p_05um, p_10um, p_25um, p_50um, p_100um;
  uint16_t unused;
  uint16_t checksum;
};
struct pms5003 data;

/* Reading in the data from the pms serial */
boolean readPMS(Stream *s) {
  if (! s->available()) { return false; }
 
  // Waiting for the ‘0x42’ start byte
  while(s->peek() != 0x42) { s->read(); }
 
  // Reading all 32 bytes
  if (s->available() < 32) { return false; }
   
  uint8_t buffer[32];    
  uint16_t sum = 0;
  s->readBytes(buffer, 32);
 
  // Setting up the checksum
  for (uint8_t i=0; i<30; i++) { sum += buffer[i]; }
 
  // Configuring the buffer so that it works across all platforms
  // as the data comes in endian’d
  uint16_t buffer_u16[15];
  for (uint8_t i=0; i<15; i++) {
    buffer_u16[i] = buffer[2 + i*2 + 1];
    buffer_u16[i] += (buffer[2 + i*2] << 8);
  }
 
  // Copying the data into the struct
  memcpy((void *)&data, (void *)buffer_u16, 30);
 
  // Checking if the data was successfully read
  if (sum != data.checksum) {
    Serial.println(“Checksum failure”);
    return false;
  }
  return true;
}

/* Initialising the serial & pms serial with a 9600 baud rate */
void setup() {
  Serial.begin(9600);
  pms.begin(9600);
}
 
/* Gathering & displaying the sensor data */
void loop() {
  // Checking if the data was successfully read
  if (readPMS(&pms)) {
    Serial.println(“—————————————–“);
    Serial.print(“Particles > 0.3um:”);
    Serial.println(data.p_03um);
    Serial.print(“Particles > 0.5um:”);
    Serial.println(data.p_05um);
    Serial.print(“Particles > 1.0um:”);
    Serial.println(data.p_10um);
    Serial.print(“Particles > 2.5um:”);
    Serial.println(data.p_25um);
    Serial.print(“Particles > 5.0um:”);
    Serial.println(data.p_50um);
    Serial.print(“Particles > 10.0 um:”);
    Serial.println(data.p_100um);
  }
}

Output:

After uploading the code, launch the serial monitor by pressing Shift+M. The output from the sensor will be displayed and the values will settle after approximately 1 minute. If you move the sensor into a dustier environment, you will notice these values will change.

Downloadable Content:

Please find this tutorial’s code on our GitHub page.

Credits:

  • Maker Community
  • The Arduino Community
  • The STEM Community
Table of Contents