Go Down

Topic: counting pulses per second (Read 4485 times) previous topic - next topic

What is the most simple code for counting pulses per second? thanks

Grumpy_Mike

Use the pulse in function to measure the high and low periods. Add them together and take the reciprocal.

like this? PPS = 1/(pulseIn(1,HIGH) + pusleIn(1,LOW))

Grumpy_Mike


WizenedEE

pulseIn() returns a value in microseconds. Since using unsigned longs means no fractions, the value of that equation will always be zero.

Techone

@Leviticus6432

I did a small code to count pulses. Check-it out at http://arduino.cc/forum/index.php/topic,75571.0.html one of my posts.

Just trying to help.

Grumpy_Mike

Quote
pulseIn() returns a value in microseconds. Since using unsigned longs means no fractions, the value of that equation will always be zero.

So cast them as floats.

What would the code be to count pulses per second? thanks

PeterH

You could have some logic to increment a pulse counter each time a pulse occurs, and some logic to determine when a second had elapsed and save the counter somewhere where you are holding 'current pulses per second', then reset the counter to zero.
I only provide help via the forum - please do not contact me for private consultancy.

well it is for a speed sensor in a car and I really need it to be a little more accuate than checking the speed every second...

Nick Gammon

How accurate?

The suggested solution doesn't "check the speed every second". It counts pulses, during a second. Which is what you asked for in the subject line and the first post.

You could count pulses, and check every half second how many you got (and double the number to estimate the number per second).

You could check every quarter of a second.

But if you recall your calculus you will hit a limit. You can't check over a zero period of time. And the shorter the period the less accurate the result will be.

For example, if you counted pulses over a microsecond, you might get one, or no pulses. This is hardly going to give you an accurate result.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

You could, each time a pulse occurs, record the time, and compare this pulse's time with the previous pulse's time, to get an instantaneous speed reading (for the second and subsequent pulses).

There a number of ways to skin this particular cat. Pick one, and try to write some code to implement it. Post what you have, and describe why you chose that method, and what issues you are having, if you have issues.

This isn't the "do my homework for me" hotline. We've all done our own homework in the (distant) past. Your turn to do the same.

Quote

byte freqpin = 2;

unsigned long timePeriod = 0;
int frequency  = 0;
unsigned long ton = 0;
unsigned long toff = 0;
int lastin = 0;
int in = 0;
int count = 0;

void setup()

  pinMode(freqpin,INPUT);
  Serial.begin(9600);
  digitalWrite (freqpin,HIGH);
}

void loop()
{
  in = digitalRead(freqpin);
  ton = pulseIn(freqpin,HIGH);
  toff = pulseIn(freqpin,LOW);
  timePeriod = ton + toff; 
  if (in != lastin)
  {
    if (in == LOW)
    {
      count = (count + 1);
    }
  }
 
  if (count > 2)
  {
    frequency =(1000*count/timePeriod);
    timePeriod = 0;
    count = 0;
    Serial.println (frequency);
  }
lastin = in;


}






I was thinking something like that... It checks the hz every time the sample of 2 pulses has been recoded... at really low speeds it is not that accurate but should in theory work well for any speed over say 3 miles per hour.. But it doesn't work at all? what am I doing wrong   PS: the speed sensor pulses something like 1.7 times per mph   Thanks

By the way a very small amount was from Techone, I was reading through his post and started trying to make it work for me... My code is basically completely different than his now tho, only two or three lines and a few varible names remain the same. Just trying to give credit where credit is due lol

PaulS

Code: [Select]
  ton = pulseIn(freqpin,HIGH);
  toff = pulseIn(freqpin,LOW);

This is not how to detect pulses from the sensor. It blocks until the signal transitions from HIGH to LOW and back HIGH, then records the time that LOW to HIGH took. Then, it waits for the signal to go from LOW to HIGH and then back to LOW. Polling the pin, and detecting when a transition occurs yourself, and, of course, noting the time, will produce more accurate results.


Go Up