Go Down

### Topic: Read a freqency from an analog pin (Read 7218 times)previous topic - next topic

#### florin

##### Nov 13, 2012, 08:36 pm
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?

#### PaulS

#1
##### Nov 13, 2012, 08:37 pm
Quote
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.

#### florin

#2
##### Nov 13, 2012, 08:38 pm
Sorry, an analog pin.I want to read a frquency.

#### retrolefty

#3
##### Nov 13, 2012, 08:48 pm

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

#4
##### Nov 13, 2012, 08:50 pm
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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### florin

#5
##### Nov 13, 2012, 08:53 pm
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.

#### retrolefty

#6
##### Nov 13, 2012, 09:01 pm

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

#### florin

#7
##### Nov 13, 2012, 09:08 pm
I do not want to work exactly.If sound is low, blink a led. If sound is high, blink another led.

#### AWOL

#8
##### Nov 13, 2012, 09:09 pm
1Hz is an extremely low frequency to detect with any microphone you are likely able to afford.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#9
##### Nov 13, 2012, 09:13 pm
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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### florin

#10
##### Nov 13, 2012, 09:14 pm
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.

#### florin

#11
##### Nov 13, 2012, 09:17 pm
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.

#### DVDdoug

#12
##### Nov 13, 2012, 09:19 pm
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 FFT/DFT (quite a bit more mathematically complex).

#### PaulS

#13
##### Nov 13, 2012, 09:22 pm
Quote
Can make for me an example code with this library?

When is our homework due?

#### florin

#14
##### Nov 13, 2012, 10:06 pm
I don't need the full code. I want only a line , wich read frequency...

Go Up

Please enter a valid email to subscribe