Read counts on pin for a particle sensor


I have an Electric IMP that read counts from a sensor on pin1 by the command The small code that covers this is:

count <- hardware.pin1;
local time = 60 // time to log pulses for
count.configure(PULSE_COUNTER, time);
local numPulses = 0;

function bigLoop() { 
    counter = counter +1;
    local numPulses =;

Is there something similar for the photon I could use so I could move the application to the photon?

you can use an interrupt to count pulses:

volatile int myCount = 0;

void setup() 
  attachInterrupt(D2, count, RISING);

void loop() 


void count()

read about attachInterrupt() here

If I’m not mistaken up till now you’d need to add this line before the attachInterrupt() line

  pinMode(D2, INPUT_PULLDOWN);

And I’d avoid any of these pins wherever possible

And maybe use a different name than count since this is a commonly used name for counter variables (but this is just personal taste ;-))

well the Example Code should also be looked at a close as you scrutinize my posts:

Again, the example code uses D2 :wink:

it wasn’t etched into stone, just a variable name that meant something.

Sorry, if you took my post as scrutinizing yours - it wasn’t meant that way - and "yes!" the example would need looking at too, but I think my point is valid for the a button (which usually means floating pin when not pressed), while the example does not explicitly talk about a button.
I’d rather gather from the use of CHANGE that there is a interrupt source in use that does effectivly provide two defined states for HIGH and LOW, which a button does (usually) not.
(But on second read I’ve seen that the OP uses a sensor rather than a button)

As for the pin selection, the example dates back to the Particle Core where such limitations did not exist (but others) and I didn’t say you can’t use these pins, but where possible avoid them.

And despite the fact that my post was marked as reply to you ( @BulldogLowell ), I presset the “Reply” button for the thread and actually wanted to provide helping pointers for the OP and definetly not get a you.

Sorry if you misunderstood me that way!

1 Like

I use the word in its pure sense, no emotion… I didn’t intend to impugn your comments!

“Scrutiny” and words like “accountability” and “consequences” seem to somehow gotten a negative wrap… probably because of their excessive use in the corporate world!! :smile:

In any case, the OP should be on the right track!!!


Sorry to Hijack this thread, but its on topic :slight_smile:

My code compiles and works well without this line when attaching interrupts? I have an external pull down is that why? or is there more to it?

yeah… you can use the internal pull-ups/pull-downs or you can use an external resistor (as you did) pulling up to 3V3 or down to ground.

in Arduinoland, you would usually set the pin to INPUT though… but the example in the Particle docs doesn’t, so I now want to check to see if the pinMode is set in the attachInterrupt() function.


There is an open GitHub issue that deals with that.
It suggests to auto-set the pinMode() according the trigger edge.

1 Like

Thanks for the help! The following now works fine for counting the pulses i receive from the sensor. There are some other stuff there as well but one should get the idea if someone else needs something similar.

volatile int myParticle = 0;

void setup()
attachInterrupt(D5, particle, RISING);

void loop()
// If it’s been POST_RATE ms (default 30 seconds), try to post again.
if (millis() - lastPost > POST_RATE)
lastPost = millis();
myParticle = 0; // Resets particle counter

void particle()