I'm using an Arduino clone (Seeeduino Lotus) together with a Shinyei PPD42NS sensor to measure air quality. The sensor reports its measurement as Low-Pulse occupancy. What would be the best way how to collect the value with Arduino? Currently I'm simply looping for some predefined duration using pulseIn:

unsigned long starttime;
register unsigned long sample_time; // ms
register unsigned long lowpulseoccupancy = 0; // us

// Wait for the signal to return to HIGH
pulseIn(DUSTPIN, LOW);
// Now start measuring
starttime = millis();
while ((sample_time = (millis() - starttime)) < DUST_SAMPLE_TIME_MS) {
    lowpulseoccupancy += pulseIn(DUSTPIN, LOW);

ratio_pct = lowpulseoccupancy / (sample_time * 10.0);

Is there a better way? In particular, this tight loop prevents Arduino from doing anything else for 30s (or a similar period).

I was thinking about using interrupts, either triggering on CHANGE, or (if that's possible) having two functions, one triggering on RISING and another on FALLING. Are there any caveats when doing so?

  • Seeed studio has some example code for gas sensors using LPO. 30s is way longer than necessary though. Interrupt could also work. The only downside is you can't use those interrupt pin for anything else. There is no way to have two kinds of interrupts on a single pin.
    – Gerben
    Mar 22, 2016 at 14:11
  • @Gerben For completeness (and not to pick nits), you could OR a couple of devices' interrupt lines (through a gate if necessary) to a single interrupt line. On an interrupt, the ISR could poll the devices which would need to be able to cooperate in some minimum way, or the interrupt lines could be latched and the latches presented on other, non-interrupt pins that the ISR could poll.
    – JRobert
    Aug 19, 2016 at 21:01

1 Answer 1


You can trigger on CHANGE, and check in your handler which case it is:

   attachInterrupt(digitalPinToInterrupt(my_pin), my_handler, CHANGE);
    void my_handler(void) {
      if(LOW==digitalRead(my_pin)) {
      else {

Another option would be to use a timer to poll your input, and implement a logic on top of this.

  • How long does it take between the change and the invocation of the handler? In particular I'm concerned if I could miss some pulses due to the delay.
    – Petr
    Nov 18, 2016 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.