Pages: [1] 2 3 ... 5   Go Down
Author Topic: Read a freqency from an analog pin  (Read 3160 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 48995
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26934
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26183
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26934
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-razz
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 48995
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can make for me an example code with this library?
When is our homework due?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't need the full code. I want only a line , wich read frequency...
Please help.  smiley-cry
Logged

Pages: [1] 2 3 ... 5   Go Up
Jump to: