Counting pulses from a sensor

Hey guys!

If this has been addressed already then please indicate me, if possible, the thread. :slight_smile:

I have this sensor that can send up to 25000 pulses per minute and I wanted to know: can the Arduino Uno can read these many pulses?

I'm counting the pulses by doing:

pulse = pulseIn(pin, LOW);

if(pulse != 0){
c++;
}

'pulse' being unsigned long.

Now, the number of pulses is used to measure something. This measure I can do it roughly with another mechanical method which until now it's being more close to the truth than the results whit the Arduino. Hence my question.

In the present case I should be measuring something close to 4500 in 1 min and I'm only getting 1900...

And also, is there a better way to count the pulses?

Thanks so much for the help! :smiley:

pulseIn is for measuring the length of pulses, you would probably be better off just using an interrupt pin (pins 2 or 3) and attachInterrupt().

I think your test might just be rejecting very short pulses for no reason, try losing the if.

Thanks, I'll look into that and see if I can understand what you mean...I haven't used interruptions yet.

Another I approach I just thought is to get the number of pulses from the pulse duration.

For the same situation (where re result should be aprox. 4500 pulses) I'm getting from pulseIn a duration of 3000microsec each pulse. So in 60s I would have 60000ms / 3ms = 20000 pulses! Doesn't seem right either...

25000 pulses per minute is only 420 pulses per second (one every 2.4ms or so).
You should be easily be able to count this.
Be aware that if you're using serial output to report results, unless you are using interrupts, you will probably miss pulses during serial prints.

Ok, I don't get this...I'm only being able to get the pulse values from pulseIn on pin 13... In any other pin I get 0. It doesn't make sense since I'm using a variable to set the pin, so changing it there, changes everywhere else.

I was trying to connect the sensor to pin 2 in order to make the interruption. Something like:

int pin = 2;
volatile int c = 0;

void setup()
{
pinMode(pin, INPUT);
attachInterrupt(0, count, LOW);
Serial.begin(9600);
}

void loop(){

Serial.println(c);

}

void count(){
c++;
}

I get nothing from the sensor on pin 2...only on pin 13....don't get it

Update:

So I was using the PNP output from the sensor, and thinking that the behavior above could be due to the built-in LED on pin 13, I used the NPN output instead and it does work on other pins now.

So running the instructions for the interrupt above, in 60s I'm getting 4 300 000 pulses (!)...far too much...

Am I doing something wrong?

Hi,

Do you need a pullup resistor (maybe 1000 ohms) to +5 from that sensor??

Sounds like you're counting noise....

I'm using a 10kOhm pullup resistor.

Don't you want FALLING, not LOW?

OK so trying FALLING very quickly I got 6300 pulses/min. Since the 4500 were from a not very accurate method, I'm guessing this could be it :slight_smile: I'll have to verify this later.

Thanks!

Hey guys, correct me if I'm wrong.

As much as I wanted to get rid of the pulseIn function, apparently I need it to maintain some kind of synchronism between the readings of the Arduino and the sensor.

If there is no pulseIn I keep reading HIGHs and LOWs when I should be getting only HIGHs. What I understand from this is that the Arduino is reading LOWs in between pulses. And this cannot be with what I want.

Does this make sense? I don't have a choice do I?

After you changed LOW to FALLING, you have been counting pulses. You get exactly one FALLING interrupt/pulse, so you are not counting both HIGHs and LOWs if that's what you were thinking of. No need to bring pulseIn back.

:slight_smile:
Hey..
I have already worked on this project..
Here is my project code, its perfect counter and can do your job just edit it little bit according to your need..

// counter
int count = 0;
int previousValue = 0;
int currentValue = 0;
void setup()
{
   pinMode(13, OUTPUT);
   pinMode(4, INPUT);
}
void loop()
{
   currentValue = digitalRead(4);
   if (previousValue != currentValue)
   {if(currentValue == HIGH)
         ++count;}
   if(count == 5000)
     {count = 0;
     digitalWrite(13, HIGH);
     delay(1000)
     digitalWrite(13, LOW);}
previousValue = currentValue; 
}