I’m working on integrating the Grove dust sensor(the Shinyei PPD42NS) with the Photon to measure air pollution, but the sensor is returning sporadic and incorrect values(when calibrated to an Arduino running very similar source code with the same sensor).
Here is the code in question:
void loop() {
duration = pulseIn(PPD_PIN, LOW); // Take readings from dust sensor
lowpulseoccupancy = lowpulseoccupancy+duration;
Serial.println("Duration: " + String(duration));
if ((millis()-starttime) > sampletime_ms) // Compute the concentration every 30 seconds
{
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 spec sheet curve
Serial.println("Ratio: " + String(ratio));
Serial.println("Concentration: " + String(concentration));
lowpulseoccupancy = 0;
// Reset starttime to now
starttime = millis();
}
}
While troubleshooting I found that the PulseIn function on the Photon was timing out and returning 0 for most readings. I see that the PulseIn function returns 0 when it times-out(takes longer than 3 seconds to return), so I assume that this is what is happening. However, according to the datasheet for the PPD42NS (http://www.seeedstudio.com/wiki/images/4/4c/Grove_-_Dust_sensor.pdf), the pulse-width is 10ms-90ms, so I’m not sure why the time-out would be triggered.
Any ideas for what the problem could be?