// dust sensor pin lineup
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
int pin = 8; // connect dust sensor pin 2 to Arduino ping 8
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
float pm25val = 0;
float pm25coef = 0.00207916725464941;
// assumes count per 0.1 cubic ft
// Note PDS42NS output is 0.01 cubic ft
void setup() {
Serial.begin(9600);
pinMode(8,INPUT);
starttime = millis();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// check if returns are valid, if they are NaN (not a number) then something went wrong!
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using PPD42 spec sheet curve
Serial.print(lowpulseoccupancy);
Serial.print(" ; ");
Serial.print(ratio);
Serial.print(" ; ");
Serial.print(concentration);
Serial.println("pcs/0.01cft ");
// PM2.5 calc
pm25val = pm25coef * concentration * 10; // 10 to transform 0.01 cf to 0.1 ft
Serial.print("PM25 value: ");
Serial.print(pm25val);
Serial.println("ug/m3");
lowpulseoccupancy = 0;
starttime = millis();
}
}