Example Code for Arduino
Last revision 2026/01/14
The article discusses integrating an Arduino UNO with a PM2.5 Air Quality Sensor, providing example code for measuring particle concentrations in standard and atmospheric environments, along with wiring diagrams and setup instructions.
Hardware Preparation
- 1 x Arduino UNO control board
- 1 x PM2.5 Air Quality Sensor
- Several Dupont lines
Software Preparation
- Arduino IDE, click to download Arduino IDE
- For how to install the library file, click the link
- Install the Arduino library DFRobot_AirQualitySensor.
Wiring Diagram

Function list
/*!
* @brief Get the concentration of the specified particle type
* @param type:PARTICLE_PM1_0_STANDARD Particle concentration of PM1.0 in standard particle
PARTICLE_PM2_5_STANDARD Particle concentration of PM2.5 in standard particle
PARTICLE_PM10_STANDARD Particle concentration of PM10 in standard particle
PARTICLE_PM1_0_ATMOSPHERE Particulate matter concentration of PM1.0 in atmospheric environment
PARTICLE_PM2_5_ATMOSPHERE Particulate matter concentration of PM2.5 in atmospheric environment
PARTICLE_PM10_ATMOSPHERE Particulate matter concentration of PM10 in atmospheric environment
* @return concentration(ug/m3)
*/
uint16_t gainParticleConcentration_ugm3(uint8_t type);
/*!
* @brief Get the number of particles per 0.1 liter of air
* @param type:PARTICLENUM_0_3_UM_EVERY0_1L_AIR
PARTICLENUM_0_5_UM_EVERY0_1L_AIR
PARTICLENUM_1_0_UM_EVERY0_1L_AIR
PARTICLENUM_2_5_UM_EVERY0_1L_AIR
PARTICLENUM_5_0_UM_EVERY0_1L_AIR
PARTICLENUM_10_UM_EVERY0_1L_AIR
* @return number
*/
uint16_t gainParticleNum_Every0_1L(uint8_t type);
/*!
* @brief Writes data to the specified register of the sensor
* @param NULL
* @return No return value
*/
uint8_t gainVersion();
Sample Code
1. Sample code-standard particle concentration
Function description: Get the standard particle concentration of PM2.5, PM1.0, PM10.
#include "DFRobot_AirQualitySensor.h"
#define I2C_ADDRESS 0x19
DFRobot_AirQualitySensor particle(&Wire ,I2C_ADDRESS);
void setup() {
Serial.begin(115200);
/**
Sensor initialization is used to initialize IIC, which is determined by the communication mode used at this time.
*/
while(!particle.begin())
{
Serial.println("NO Deivces !");
delay(1000);
}
Serial.println("sensor begin success!");
delay(1000);
/**
Get sensor version number
*/
uint8_t version = particle.gainVersion();
Serial.print("version is : ");
Serial.println(version);
delay(1000);
}
void loop() {
/**
*@brief : Get concentration of PM1.0 PM2.5 PM10
*@param :PARTICLE_PM1_0_STANDARD Standard particle
PARTICLE_PM2_5_STANDARD Standard particle
PARTICLE_PM10_STANDARD Standard particle
PARTICLE_PM1_0_ATMOSPHERE In atmospheric environment
PARTICLE_PM2_5_ATMOSPHERE In atmospheric environment
PARTICLE_PM10_ATMOSPHERE In atmospheric environment
*/
uint16_t PM2_5 = particle.gainParticleConcentration_ugm3(PARTICLE_PM2_5_STANDARD);
uint16_t PM1_0 = particle.gainParticleConcentration_ugm3(PARTICLE_PM1_0_STANDARD);
uint16_t PM10 = particle.gainParticleConcentration_ugm3(PARTICLE_PM10_STANDARD);
Serial.print("PM2.5 concentration:");
Serial.print(PM2_5);
Serial.println(" ug/m3");
Serial.print("PM1.0 concentration:");
Serial.print(PM1_0);
Serial.println(" ug/m3");
Serial.print("PM10 concentration:");
Serial.print(PM10);
Serial.println(" ug/m3");
Serial.println();
delay(1000);
}
Result
In the state of standard particulate matter, the concentration of PM2.5, PM1.0, PM10 read is: 45ug/m3, 31ug/m3, 49ug/m3.

2. Sample code-particulate matter concentration in atmospheric environment
Function description: Obtain the concentration of particulate matter in the atmospheric environment of PM2.5, PM1.0, PM10.
#include "DFRobot_AirQualitySensor.h"
#define I2C_ADDRESS 0x19
DFRobot_AirQualitySensor particle(&Wire ,I2C_ADDRESS);
void setup() {
Serial.begin(115200);
/**
Sensor initialization is used to initialize IIC, which is determined by the communication mode used at this time.
*/
while(!particle.begin())
{
Serial.println("NO Deivces !");
delay(1000);
}
Serial.println("sensor begin success!");
delay(1000);
/**
Get sensor version number
*/
uint8_t version = particle.gainVersion();
Serial.print("version is : ");
Serial.println(version);
delay(1000);
}
void loop() {
/**
*@brief : Get concentration of PM1.0
*@param :PARTICLE_PM1_0_STANDARD Standard particle
PARTICLE_PM2_5_STANDARD Standard particle
PARTICLE_PM10_STANDARD Standard particle
PARTICLE_PM1_0_ATMOSPHERE In atmospheric environment
PARTICLE_PM2_5_ATMOSPHERE In atmospheric environment
PARTICLE_PM10_ATMOSPHERE In atmospheric environment
*/
uint16_t PM2_5 = particle.gainParticleConcentration_ugm3(PARTICLE_PM2_5_ATMOSPHERE );
uint16_t PM1_0 = particle.gainParticleConcentration_ugm3(PARTICLE_PM1_0_ATMOSPHERE );
uint16_t PM10 = particle.gainParticleConcentration_ugm3(PARTICLE_PM10_ATMOSPHERE);
Serial.print("PM2.5 concentration:");
Serial.print(PM2_5);
Serial.println(" ug/m3");
Serial.print("PM1.0 concentration:");
Serial.print(PM1_0);
Serial.println(" ug/m3");
Serial.print("PM10 concentration:");
Serial.print(PM10);
Serial.println(" ug/m3");
Serial.println();
delay(1000);
}
Result
In the atmospheric environment, the particle concentration of PM2.5/PM1.0/PM10 is about: 38ug/m3, 23ug/m3, 46ug/m3.

3. Sample code-the number of particles per 0.1 liter of air
Function description: Read the number of particles above 0.3um/0.5um/1.0um/2.5um/5.0um/10um per 0.1 liter of air.
#include "DFRobot_AirQualitySensor.h"
#define I2C_ADDRESS 0x19
DFRobot_AirQualitySensor particle(&Wire ,I2C_ADDRESS);
void setup() {
Serial.begin(115200);
/**
Sensor initialization is used to initialize IIC, which is determined by the communication mode used at this time.
*/
while(!particle.begin())
{
Serial.println("NO Deivces !");
delay(1000);
}
Serial.println("sensor begin success!");
delay(1000);
/**
Get sensor version number
*/
uint8_t version = particle.gainVersion();
Serial.print("version is : ");
Serial.println(version);
delay(1000);
}
void loop() {
/**
*@brief : Get particle number of 0.3um/0.5um/1.0um/2.5um/5.0um/10um per 0.1L of air
*@param :PARTICLENUM_0_3_UM_EVERY0_1L_AIR
PARTICLENUM_0_5_UM_EVERY0_1L_AIR
PARTICLENUM_1_0_UM_EVERY0_1L_AIR
PARTICLENUM_2_5_UM_EVERY0_1L_AIR
PARTICLENUM_5_0_UM_EVERY0_1L_AIR
PARTICLENUM_10_UM_EVERY0_1L_AIR
*/
uint16_t um0_3 = particle.gainParticleNum_Every0_1L(PARTICLENUM_0_3_UM_EVERY0_1L_AIR);
uint16_t um0_5= particle.gainParticleNum_Every0_1L(PARTICLENUM_0_5_UM_EVERY0_1L_AIR);
uint16_t um1_0= particle.gainParticleNum_Every0_1L(PARTICLENUM_1_0_UM_EVERY0_1L_AIR);
uint16_t um2_5= particle.gainParticleNum_Every0_1L(PARTICLENUM_2_5_UM_EVERY0_1L_AIR);
uint16_t um5_0= particle.gainParticleNum_Every0_1L(PARTICLENUM_5_0_UM_EVERY0_1L_AIR);
uint16_t um10= particle.gainParticleNum_Every0_1L(PARTICLENUM_10_UM_EVERY0_1L_AIR);
Serial.print("The number of particles with a diameter of 0.3um per 0.1 in lift-off is: ");
Serial.println(um0_3);
Serial.print("The number of particles with a diameter of 0.5um per 0.1 in lift-off is: ");
Serial.println(um0_5);
Serial.print("The number of particles with a diameter of 1.0um per 0.1 in lift-off is: ");
Serial.println(um1_0);
Serial.print("The number of particles with a diameter of 2.5um per 0.1 in lift-off is: ");
Serial.println(um2_5);
Serial.print("The number of particles with a diameter of 5.0um per 0.1 in lift-off is: ");
Serial.println(um5_0);
Serial.print("The number of particles with a diameter of 10um per 0.1 in lift-off is: ");
Serial.println(um10);
Serial.println("");
delay(1000);
}
Result
It is read that the number of 0.3um/0.5um/1.0um/2.5um/5.0um/10um particles per 0.1 liter of air is approximately: 1615, 1356, 233, 0, 0, 0.

Was this article helpful?
