PulseIn on Photon timing out with PPD42NS dust sensor

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?

@patrick_t, how are you powering the sensor and which Photon pin are you using to read its output? How did you configure the input pin in setup()? How long is the wire from the pin to the sensor?

Do you have an oscilloscope to measure the actual output of the sensor? Either the sensor is not producing low pulses or the levels are not correct (HI=4v, LOW=0,7v).

@peekay123 I’m powering the sensor from the 3.3v pin, and I’m using digital pin 5 to read the output.

My setup function is very simple:

#define PPD_PIN 5

unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;

float ratio = 0;
float concentration = 0;

void setup() {
    pinMode(PPD_PIN, INPUT);
    starttime = millis();//get the current time;

I’m using a standard jumper wire to connect the sensor to the Photon, so 4-5 inches long. Unfortunately I do not have an oscilloscope to measure the output from the sensor, but I have confirmed that the same sensor works correctly using an Arduino Uno and a LinkIt One board.

The data sheet for that sensor says that supply voltage should be 5 volts, not 3.3. Try powering it from Vin, and see if that fixes the problem. The D5 pin is 5 volt tolerant in digital mode, so reading the sensor on that pin should be ok.

1 Like