0% found this document useful (0 votes)
150 views2 pages

Prana Air PM Sensor Arduino Code Og

This code is reading data from a PMS5003 air quality sensor over a serial connection. It defines a data structure to store the sensor readings, which include PM2.5, PM10 and larger particle counts. The code initializes the serial connection, reads the data in a loop if available, and prints the readings to the serial monitor.

Uploaded by

smokeiiot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views2 pages

Prana Air PM Sensor Arduino Code Og

This code is reading data from a PMS5003 air quality sensor over a serial connection. It defines a data structure to store the sensor readings, which include PM2.5, PM10 and larger particle counts. The code initializes the serial connection, reads the data in a loop if available, and prints the readings to the serial monitor.

Uploaded by

smokeiiot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <SoftwareSerial.

h>
SoftwareSerial pmsSerial(2,3);

void setup() {
// our debugging output
[Link](115200);

// sensor baud rate is 9600


[Link](9600);
}

struct pms5003data {
uint16_t framelen;
uint16_t pm10_standard, pm25_standard, pm100_standard;
uint16_t pm10_env, pm25_env, pm100_env;
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um,
particles_100um;
uint16_t unused;
uint16_t checksum;
};

struct pms5003data data;

void loop() {
if (readPMSdata(&pmsSerial)) {
// reading data was successful!
[Link]();
[Link]("---------------------------------------");
[Link]("Concentration Units (standard)");
[Link]("PM 1.0: "); [Link](data.pm10_standard);
[Link]("\t\tPM 2.5: "); [Link](data.pm25_standard);
[Link]("\t\tPM 10: "); [Link](data.pm100_standard);
[Link]("---------------------------------------");
[Link]("Concentration Units (environmental)");
[Link]("PM 1.0: "); [Link](data.pm10_env);
[Link]("\t\tPM 2.5: "); [Link](data.pm25_env);
[Link]("\t\tPM 10: "); [Link](data.pm100_env);
[Link]("---------------------------------------");
[Link]("Particles > 0.3um / 0.1L air:"); [Link](data.particles_03um);
[Link]("Particles > 0.5um / 0.1L air:"); [Link](data.particles_05um);
[Link]("Particles > 1.0um / 0.1L air:"); [Link](data.particles_10um);
[Link]("Particles > 2.5um / 0.1L air:"); [Link](data.particles_25um);
[Link]("Particles > 5.0um / 0.1L air:"); [Link](data.particles_50um);
[Link]("Particles > 10.0 um / 0.1L air:"); [Link](data.particles_100um);
[Link]("---------------------------------------");
}
}

boolean readPMSdata(Stream *s) {


if (! s->available()) {
return false;
}

// Read a byte at a time until we get to the special '0x42' start-byte


if (s->peek() != 0x42) {
s->read();
return false;
}

// Now read all 32 bytes


if (s->available() < 32) {
return false;
}

uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);

// get checksum ready


for (uint8_t i=0; i<30; i++) {
sum += buffer[i];
}

/* debugging
for (uint8_t i=2; i<32; i++) {
[Link]("0x"); [Link](buffer[i], HEX); [Link](", ");
}
[Link]();
*/

// The data comes in endian'd, this solves it so it works on all platforms


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);
}

// put it into a nice struct :)


memcpy((void *)&data, (void *)buffer_u16, 30);

if (sum != [Link]) {
[Link]("Checksum failure");
return false;
}
// success!
return true;
}

You might also like