# Read a freqency from an analog pin

Hi! I want to read some frequnces, but i do not know how i can do it. For example: if freq=20 hertz, blink a LED. Any oppinions?

I want to read some frequnces, but i do not know how i can do it. For example: if freq=20 hertz, blink a LED. Any oppinions?

The frequency of a PWM pin is a constant.

Sorry, an analog pin.I want to read a frquency.

florin: Sorry, an analog pin.I want to read a frquency.

Difficult and time consuming to try and measure the frequency of a analog signal with a analog input pin. Better to run the signal to a comparator op-amp circuit and measure the frequency as a digital on and off signal. What is the waveform of your signal (sinewave, square wave, random audio) ? What is it's voltage range min to max, AC or DC?

Perhaps if you told us what you are trying to accomplish in the sketch rather then what method you think will work best, we can better try and help you.

Lefty

Digital signal, or analog? Best bet is to get it digital - then can use the Pulsein() commands to measure the high time, measure the low time, add the two together for the period. 1/period = frequency. Or using the rising edge to create a PCINT, measure the time from PCINT to PCINT, do the same math.

I have a microphone conected to A0. If frequency is less than 1 HZ , a led is HIGH.If frequency is bigger than 1 HZ, another led is HIGH.

florin: I have a microphone conected to A0. If frequency is less than 1 HZ , a led is HIGH.If frequency is bigger than 1 HZ, another led is HIGH.

But a microphone generates rather random audio sounds comprised of many mixed frequencies and their harmonics, not a single frequency unless you have it pointing at a signal generating a single pure frequency through a speaker. Also audio is a AC voltages and an arduino analog input pin can only measure DC positive voltage values, negative voltages if large enough can damage the input pin. I think you need to rethink your application approach.

Lefty

I do not want to work exactly.If sound is low, blink a led. If sound is high, blink another led.

1Hz is an extremely low frequency to detect with any microphone you are likely able to afford.

You need to detect high & low volume then? That is easier. Run your signal thru an amplifier, and low pass filter, make it more like a DC level, then measure the level with analog pin. That's easily doable.

OK.I want to use a library, or a function wich can read a frequency. After reading i want to separe in 2 frequences with if conditional. That is all. :P

I don't want to read volume. I have found this : http://tushev.org/articles/electronics/43-measuring-frequency-with-arduino Can make for me an example code with this library? Thanks in advanced.

I think this should be possible if there is a single frequency or a dominant frequency, especially at low frequencies. At higher frequencies, processor speed could be an issue... And, frequency measurement can just be "flaky", even with a proper frequency counter. Often you have to manually adjust a threshold control to get a good-stable reading.

Normally, you'd measure the time betwen zero-crossings (i.e When the waveform goes from positive to negative or vice-versa). Since the Arduino can't accept negative input, you'd have to bias the input (i.e. 2.5V) and use the effective zero-crossing (bias-crossing) instead of the actual zero crossing. You need to allow for noise, so you'd have to look for values slightly above and below the zero-crossing (perhaps calibrating to a percentage of the peak), and you'd have to ignore low signal levels.

You should probably "isolate" your time-measurement from your frequency calculation and other code. In other words, run a tight time-measurement loop (perhaps several times to take an average) and then break the loop and process your data.

If you are analyzing complex sounds with more than one frequency component (just about anything other than a test-tone,) you'll need to look into [u]FFT/DFT[/u] (quite a bit more mathematically complex).

Can make for me an example code with this library?

When is our homework due?

[quite] I want only a line , wich read frequency…[/quote]

Try this:

``````  freq=freq_read(pin); //read frequency on pin
``````

You are welcome.

``````1.long getFrequency(int pin) {
2.#define SAMPLES 4096
3.long freq = 0;
4.for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
5.return freq / SAMPLES;
6.}
``````

Is right there on the page you linked to. Surely you can figure out how to call this function.

Isn’t this simple enough?

``````long getFrequency(int pin) {
#define SAMPLES 4096
long freq = 0;
for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
return freq / SAMPLES;
}
``````

I write the code below:

``````#include <FreqCounter.h>

double frequency;

void setup() {
pinMode(5,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(15,OUTPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);

}

void loop() {

long getFrequency(A0); {
#define SAMPLES 4096
long freq = 0;
for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(A0, HIGH, 250000);
frequency = freq/SAMPLES;
if (frequency <1) { digitalWrite(5, HIGH);
digitalWrite(11,HIGH);}
if (frequency >=1) { digitalWrite(12, HIGH);
digitalWrite(15,HIGH);}

}

}
``````

No errors, but circuit doesen’t work. I have mistakes in the code?

``````if (frequency <1) { digitalWrite(5, HIGH);
``````

digitalWrite(11,HIGH);}
if (frequency >=1) { digitalWrite(12, HIGH);
digitalWrite(15,HIGH);}

So how do those LEDs ever turn off then? You are turning them on, but never off.