having some problems reading the serial output from a pms5003 dust sensor. i always find the two start characters for the output, but then either the checksum fails, or if it passes, the numbers are typically zeros, or very near to. i have tried using a nano v3 with softwareserial, and a pro micro with a physical serial port and had the same results. i am powering everything with external power and using the appropriate line level converter. voltages all check out.
the fact that i do read the start periodically characters makes me believe my wiring is ok. manual can be found here:
Code:
/*
PMS5003 Pinout
Pin 1 from inside. Pin 8 on the outside. 8-pin connector (1.25mm raster).
Pin Function Description Remarks
1 VCC Supply voltage 5V 4.5 – 5.5V
2 GND Ground
3 SET HIGH or SUSPENDED – work mode
LOW – sleep mode 3.3V logic
4 RXD UART/TTL data recieve 3.3V logic
5 TXD UART/TTL data transmit 3.3V logic
6 Reset LOW to reset 3.3V logic
7 NC Not connected
8 NC Not connected
PMS5003 transport protocol-Active Mode
Default baud rate:9600bps Check bit:None Stop bit:1 bit
32 Bytes
Start character 1 0x42 (Fixed)
Start character 2 0x4d (Fixed)
Frame length high 8 bits Frame length=2x13+2(data+check bytes)
Frame length low 8 bits
Data1 high 8 bits …… Data1 refers to PM1.0 concentration unit μ g/m3(CF=1,standard particle)*
Data1 low 8 bits ……
Data2 high 8 bits …… Data2 refers to PM2.5 concentration unit μ g/m3(CF=1,standard particle)
Data2 low 8 bits ……
Data3 high 8 bits …… Data3 refers to PM10 concentration unit μ g/m3(CF=1,standard particle)
Data3 low 8 bits ……
Data4 high 8 bits …… Data4 refers to PM1.0 concentration unit μ g/m3(under atmospheric environment)
Data4 low 8 bits ……
Data5 high 8 bits …… Data 5 refers to PM2.5 concentration unit μ g/m3(under atmospheric environment)
Data5 low 8 bits ……
Data6 high 8 bits …… Data 6 refers to PM10 concentration unit (under atmospheric environment) μ g/m3
Data6 low 8 bits ……
*/
// Pins setup
const int SLEEP_PIN = 6; // this pin is for setting the sensor to sleep while it is not taking the data.
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG]; //serial read buffer
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
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//mySerial.setTimeout(1500);
pinMode(SLEEP_PIN, OUTPUT); // Setup Pin as output pin
digitalWrite(SLEEP_PIN, HIGH); // Let the sensor start working as default
Serial.println("Setup Complete!");
}
void loop()
{
if (mySerial.find(0x42)) { //start to read when detect 0x42
mySerial.readBytes(buf, LENG);
Serial.println("Reading Sensor...");
if (buf[0] == 0x4d) {
if (checkValue(buf, LENG)) {
/*PM01Value = (buf[9] << 8) + buf[10];
PM2_5Value = (buf[11] << 8) + buf[12];
PM10Value = (buf[13] << 8) + buf[14];*/
PM01Value = (buf[3] << 8) + buf[4];
PM2_5Value = (buf[5] << 8) + buf[6];
PM10Value = (buf[7] << 8) + buf[8];
for (int i = 3; i <= 14; i++) {
Serial.print(i); Serial.print(": "); Serial.println(buf[i], HEX);
}
Serial.print("PM 1: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM 2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM 10: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
}
else Serial.println("Read check failed");
}
else Serial.println("Init char not found");
Serial.println();
delay (3000);
}
}
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;
}
Some typical Serial Output
Reading Sensor...
Read check failed
Reading Sensor...
3: 0
4: 0
5: 0
6: 1
7: 0
8: 1
9: 0
10: 0
11: 0
12: 1
13: 0
14: 1
PM 1: 0 ug/m3
PM 2.5: 1 ug/m3
PM 10: 1 ug/m3
Reading Sensor...
Init char not found
Any thoughts?