user45654: Here is my code to find the frequency of an input rectangular wave at pin 8 of the Arduino Uno board.
const uint16_t freqPin = PB0;
const uint16_t load = 0;
unsigned long int count=0;
unsigned int value=0;

void setup()
{
Serial.begin(9600);
DDRB = 0x00;
TCCR1A = 0;
TCCR1B = TCCR1B & B11111000 | B00000111;
TCNT1 = load;
TIMSK1 |= (1<<ICIE1);
TIMSK1 |= (1<<TOIE1);
sei();
}

void loop()
{
}

ISR(TIMER1_OVF_vect){

  count++;
}
ISR(TIMER1_CAPT_vect){
  value = TCNT1;
  TCNT1 = load;
  Serial.println(count);
  Serial.println(value);
}

The output is
0  
0  
0  
0  
0  

Why the output is showing zeroes only. why is this happening ? Can someone explain?
Thanks in advance.

user43025: There are a few ways to measure the frequency of the incoming signal.  Here are ideas based on the assumption that's all you want, rather than to use specific pins of the Arduino:

Use timer interrupts and digitalRead(8); (or faster (PINB & _BV(0))) to sample when the signal is high or low.  Transfer the data off Arduino, e.g. to a PC, then do a Fourier transform (Matlab, Mathematica, Excel, whatever you like).  Alternatively, calculate the frequency on-board.  This way it is possible to measure frequencies up to several tens of kHz, with a precision of few Hz.
Use attachInterrupt() on either digital pins 2 or 3.  Count the number of rising edges of the square wave within a given time period.  You can use the timer if you need accuracy, but the method will not be as good as Fourier transform.


45654: <p>Here is my code to find the frequency of an input rectangular wave at pin 8 of the Arduino Uno board.</p>

<pre><code>const uint16_t freqPin = PB0;
const uint16_t load = 0;
unsigned long int count=0;
unsigned int value=0;

void setup()
{
Serial.begin(9600);
DDRB = 0x00;
TCCR1A = 0;
TCCR1B = TCCR1B &amp; B11111000 | B00000111;
TCNT1 = load;
TIMSK1 |= (1&lt;&lt;ICIE1);
TIMSK1 |= (1&lt;&lt;TOIE1);
sei();
}

void loop()
{
}

ISR(TIMER1_OVF_vect){

  count++;
}
ISR(TIMER1_CAPT_vect){
  value = TCNT1;
  TCNT1 = load;
  Serial.println(count);
  Serial.println(value);
}
</code></pre>

<p>The output is</p>

<pre><code>0  
0  
0  
0  
0  
</code></pre>

<p>Why the output is showing zeroes only. why is this happening ? Can someone explain?<br>
Thanks in advance.</p>
 43025: <p>There are a few ways to measure the frequency of the incoming signal.  Here are ideas based on the assumption that's all you want, rather than to use specific pins of the Arduino:</p>

<ul>
<li>Use timer interrupts and <code>digitalRead(8);</code> (or faster <code>(PINB &amp; _BV(0))</code>) to sample when the signal is high or low.  Transfer the data off Arduino, e.g. to a PC, then do a Fourier transform (Matlab, Mathematica, Excel, whatever you like).  Alternatively, calculate the frequency on-board.  This way it is possible to measure frequencies up to several tens of kHz, with a precision of few Hz.</li>
<li>Use <code>attachInterrupt()</code> on either digital pins 2 or 3.  Count the number of rising edges of the square wave within a given time period.  You can use the timer if you need accuracy, but the method will not be as good as Fourier transform.</li>
</ul>
