Example Code for Arduino-Software Serial
Last revision 2025/12/16
This article guides you through setting up Arduino Software Serial with a PM2.5 sensor, offering detailed hardware and software preparation, wiring diagrams, and sample code to monitor air quality effectively.
Hardware Preparation
| Name | Model/SKU | Quantity | Purchase Link |
|---|---|---|---|
| PM2.5 Laser Dust Sensor | SEN0177 (1272) | 1 | Link |
| Arduino Board | - | 1 | - |
Wiring Note:
- The RX pin on the sensor connects to pin 11 on the Arduino
- The TX pin on the sensor connects to pin 10 on the Arduino
Software Preparation
- Development Tool: Arduino IDE
- Version Requirement: ArduinoIDE 1.6.x or above (includes
SoftwareSeriallibrary by default) - Download Link: Arduino IDE
Wiring Diagram

Other Preparation Work
- SET: SET = 1, the module works in continuous sampling mode, it will upload the sample data after the end of each sampling. (The sampling response time is 1S); SET = 0, the module enters a low-power standby mode.
- RESET: Leave it empty is OK.
Sample Code
NOTE: The following codes are for using Software Serial.
/*!
* @file SEN0177.ino
* @brief Abstract: Read value of PM1,PM2.5 and PM10 of air quality
* @n The RX pin on the sensor connects to pin 11 on the Arduino
* @n The TX pin on the sensor connects to pin 10 on the Arduino
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author Zuyang @ HUST
* @version V3.0
* @date 2016-03-25
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
SoftwareSerial PMSerial(10, 11); // RX, TX
void setup()
{
PMSerial.begin(9600);
PMSerial.setTimeout(1500);
Serial.begin(9600);
}
void loop()
{
if(PMSerial.find(0x42)){
delay(100);
PMSerial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}
Result
Please wait 30s for the data.

Was this article helpful?
