Arduino frequency measure

I'm trying to measure a frequency from a external source with an arduino UNO, the signal is almost a square wave with an amplitude of 6v (i have to reduce this a bit, to fit arduino input specs, i don't know which is the best way to do this without modifying the signal shape) and a frequency between 20 and 220 hz more or less, this should be easy to measure as i'm not trying to measure high frequency signal (so i don't think i need any kind of divider or so).

After dealing with electronics to get a more stable signal, I have tried all frequency counters approaches i've found on the internet (including freq counter libraries) but i hadn't found anything that works, most of them show an accurate freq measure at certain freqs, but as i vary input signal the measure starts going more and more inaccurate.

Another think is that if increases the freq linearly but arduino don't register this, sometimes even decreases freq print while im increasing the freq.

I know that a 6 volt peak would lead to inaccurate results, and maybe is only this why it isn't working, i can't solve this right know, but i'd like to get advice how to get this working.

I have think different ways of doing this: -using a rising edge interrupt and a timer to calculate the period, maybe changing interrupt after rising edge to get duty cycle. -using an interrupt to sample every ISR the input and determine the rising edge and falling edge, i'm going to try this method but i don't know how to do it -doing operations in the ISR (may slow a bit the ISR, leading to measurement errors) -filling an array (buffer) with measures and processing it in the main loop. -using a library, i'd like to avoid this because i want to have full control of the code executing.

Can anyone help me with this? sorry about me english,

Edit: if possible would be good to get a fast measure, as the freq may vary along time, and if possible allow the possibility of measure more shapes than square. But first of all i want to measure the freq accurately,

Between 20Hz and 200Hz ? That is very slow.

This one should be good: https://www.pjrc.com/teensy/td_libs_FreqMeasure.html Give it a try, I'm curious if that is okay for you.

I have been thinking about measuring the period for low frequencies myself, but I never tried to write code for it. You can have a look at the "Projects" section at http://www.avrfreaks.net/ Sign up; Log in; click on "Projects" in the bar; type the word 'frequency' without enter; and see what appears on the right.

Do you know how noisy your signal is, with spikes or drops ? I assume that you did also connect the GND of the signal ? You can bring the 6V down with two resistors. For example signal to 4k7 to pin 8. And a resistor of 22k from pin 8 to GND. http://en.wikipedia.org/wiki/Voltage_divider

Well, an important experience for a newbie: IT WORKS!
Imported the library, copied the code- after a minor modification, I was able to confirm the european (swedish) grid frequency of 50 Hz.
I had to declare float frequency prior to the loop- that’s all.

#include <FreqMeasure.h>
void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum=0;
int count=0;
[b]float frequency;[/b]

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

Hadn't worked for me, but neither any of the libraries that can be found on the internet, at this point i started checking the signal, is almost a square wave.

Im designing a circuit to get the most clean signal as possible but the libraries are supposed to show an accurate reading of the frequency, which is true at certain freqs but not at all (which doesn't make sense to me is the range of frequencies im measuring, should be easy to read that range with any method (counting or measuring) but i get huge error with every program i try.

Hmm, may be, I found a frequency hard- and software worked fine together... will come back after more testing.

Clearly you are doing something wrong, but as you are not posting any code all we can do is guess. We also need to see a schematic of how you have wired it up as from what you say you only have a hazy knowledge of electronics.

For example you say:-

I know that a 6 volt peak would lead to inaccurate results,

It does no such thing. What it does do however is damage your Arduino.