PM2.5 Air Quality Sensor With Arduino Mega 2560

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
nathanngu
 
Posts: 2
Joined: Fri Nov 09, 2018 11:10 pm

PM2.5 Air Quality Sensor With Arduino Mega 2560

Post by nathanngu »

I don't know how to format the pm2.5 sensor code given to be applied to an Arduino Mega 2560.

The code works perfectly fine on an Arduino UNO, but I'd like to use the particulate sensor in conjunction with other sensors to build an electronic nose.

The problem is, is that these different sensors have different baud rates, so I need to use a Mega because they can support multiple baud rates.


I hooked it up the same as you would with the uno, but it nothing ever shows up on the serial monitor.

Whenever I run the code it just never seems to have data available. The debugging code doesn't work because there is not data to be buggy to begin with.

How should I reformat the code to work with a Mega?

Thanks,
Nathan

User avatar
oesterle
 
Posts: 806
Joined: Tue Sep 17, 2013 11:32 pm

Re: PM2.5 Air Quality Sensor With Arduino Mega 2560

Post by oesterle »

Hi, Nathan!

Please post a picture of your wiring (800x600 is best). Also, please post your code.

(In the forum editor, you’ll find code, and image buttons for adding these.)

Cheers,

Eric

User avatar
nathanngu
 
Posts: 2
Joined: Fri Nov 09, 2018 11:10 pm

Re: PM2.5 Air Quality Sensor With Arduino Mega 2560

Post by nathanngu »

I connected:
VCC --> 5v pin
GND --> GND pin
TXD --> Serial pin 2


I just copy/pasted the code from the AdaFruit website

#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);

void setup() {
// our debugging output
Serial.begin(115200);

// sensor baud rate is 9600
pmsSerial.begin(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!
Serial.print("Particles > 2.5um / 0.1L air:"); Serial.println(data.particles_25um);
Serial.println("---------------------------------------");
}
}

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


for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer, HEX); Serial.print(", ");
}
Serial.println();


// 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 = buffer[2 + i*2 + 1];
buffer_u16 += (buffer[2 + i*2] << 8);
}

// put it into a nice struct :)
memcpy((void *)&data, (void *)buffer_u16, 30);

if (sum != data.checksum) {
Serial.println("Checksum failure");
return false;
}
// success!
return true;

sorry, couldn't figure out picture/code formatting

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”