Go Down

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

#### Leviticus6432

##### Nov 03, 2011, 07:57 pm
What is the most simple code for counting pulses per second? thanks

#### Grumpy_Mike

#1
##### Nov 03, 2011, 08:10 pm
Use the pulse in function to measure the high and low periods. Add them together and take the reciprocal.

#### Leviticus6432

#2
##### Nov 03, 2011, 08:39 pm
like this? PPS = 1/(pulseIn(1,HIGH) + pusleIn(1,LOW))

#### Grumpy_Mike

#3
##### Nov 03, 2011, 10:23 pm
Yes give it a try.

#### WizenedEE

#4
##### Nov 04, 2011, 12:54 am
pulseIn() returns a value in microseconds. Since using unsigned longs means no fractions, the value of that equation will always be zero.

#### Techone

#5
##### Nov 04, 2011, 12:59 am
@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

#6
##### Nov 04, 2011, 09:07 am
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.

#### Leviticus6432

#7
##### Nov 22, 2011, 02:37 am
What would the code be to count pulses per second? thanks

#### PeterH

#8
##### Nov 22, 2011, 03:03 am
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.

#### Leviticus6432

#9
##### Nov 22, 2011, 05:08 am
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

#10
##### Nov 22, 2011, 07:19 am
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!

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

#### PaulS

#11
##### Nov 22, 2011, 11:44 am
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.

#### Leviticus6432

#12
##### Nov 23, 2011, 09:47 pm
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()
{
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

#### Leviticus6432

#13
##### Nov 23, 2011, 09:50 pm
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

#14
##### Nov 23, 2011, 11:41 pm
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

Please enter a valid email to subscribe