# Count pulses on a pin with the ATMEGA328

I have searched and I am unable to find out how to count the pulse rate on a pin. I only wish to count the pulses for one second. Does anyone know how to do this?

Did you look at the PulseIn() function?
Combine with millis() to keep track of time passing.

You do not want to see how long a pulse is , but to count the pulses for 1000 ms ?
Sounds like a frequency counter. Look at
http://interface.khm.de/index.php/lab/experiments/arduino-frequency-counter-library/

jackwp:
You do not want to see how long a pulse is , but to count the pulses for 1000 ms ?
Sounds like a frequency counter. Look at
Page has moved

What I am wanting to do is display the tempo to an LCD screen. To do that I need to count the pulses. If I can count them for one second then I can multiply them by 60 to get the tempo. Is there a better solution to this without sacrificing a 1 second delay while the microprocessor is counting?

If you calculate tempo as the number of pulses per second multiply by 60, I guess you get the number of pulses per minute.

1. How many pulses per second do you expect to happen (min - max) ?
2. so what output (tempo) do you expect to display?

If the tempo is steady, you just need the time of one pulse - so measure the time from rising edge to rising edge, then 1/time = frequency (tempo).
Or measure high pulse, measure low pulse, add together, and 1/time = frequency. No need to wait for 1 second.

Is there a better solution to this without sacrificing a 1 second delay while the microprocessor is counting?

Look at the BlinkWithoutDelay example in the IDE if you want/need to do other things whilst the counting takes place.

If the tempo is steady, you just need the time of one pulse - so measure the time from rising edge to rising edge, then 1/time = frequency (tempo).
Or measure high pulse, measure low pulse, add together, and 1/time = frequency. No need to wait for 1 second.

Thanks for your reply. That seems like a good way to find the BPM or Tempo. I have done some searching and can't find a way that I understand to measure the frequency. Do you have a link or know of a method to find the frequency on an input pin?

Something like this:
void loop(){
currentTime = millis();
if ( (currentTime - priorTIme) >= 250){ // sample 4 times/second
priorTime = priorTime + 250;

highTime = pulseIn(pinX, HIGH);
lowTime = pulseIn(pinX, LOW);
periodTime = highTime + lowTime;
frequencyBPM = 1/periodTime;
Serial.println(frequencyBPM);
}
syntax may be a little different, delclare variables as ints, floats, whatever. I can't access the online Reference from here to look it up (crashes me out of the forum), this should give you the idea.

Did you miss my questions:

1. How many pulses per second do you expect to happen (min - max) ?
2. so what output (tempo) do you expect to display?

Did you miss my questions:

1. How many pulses per second do you expect to happen (min - max) ?
2. so what output (tempo) do you expect to display?

jackwp:
Did you miss my questions:

1. How many pulses per second do you expect to happen (min - max) ?
2. so what output (tempo) do you expect to display?

I would expect no more than 3 pulses per second. But now that I think about it giving the information about the frequency, measuring how many pulses there are are in a second would not be as accurate and take too much time out of the program. I believe measuring the time for one pulse will give a more accurate result.

Right. That's why we ask questions.