Jump to content

Analog Input Frequency Detector (Arduino/Port)

Recommended Posts

Hi everyone,


I am looking to write/port code to determine real-time frequency of an input signal (audio signal no greater than 4kHz for my needs) on my MSP430 F5529. After looking over interrupts/timers I feel I have a better sense of how this will tie into this project. But I am still at a bit of a loss on the coding portion when it comes to having interrupts occur at my desired frequency.


High level abstract of my desired algorithm is as follows (although I am open to any ideas):


A ) Enters Loop


B ) Input analog signal (audio tones) crosses some set threshold (not zero due to noise/ripple even at silence) and increments a counter


C ) After X amount of time: interrupt occurs calling a function that divides the previous counter from B by X and prints this value to the serial monitor


D ) Counter from step B is reset and begins back at step A


Questions about the algorithm above


In step B I am looking for an audio signal to cross a given threshold value, is it better to sense for a rising signal in my code or just disregard half of the counter values since the signal will be sinusoidal and pass the threshold twice? Also I'm not sure I will be able to set up a direct if(input_signal == 2.xx) (where 2.xx is an threshold value in volts), so how do I sense for some window for instance: if (input_signal < 2.xx + k) && (input_signal < 2.xx - k) (where k is some value that helps determine the approximation near the threshold) without increment multiple times during a single pass? I think my last question really just relates to not fully having wrapped my head around how the main loop & interrupts will play together.


In step C I will be dividing the counter (incremented value) by the time at which my interrupt occurs and finally reset my counter. This is handled by having a global variable set for the counter, correct?


Arduino example w/ code


An example of what I am looking to accomplish can be seen HERE, any help on porting this code or how to go about this for energia/ccs would be greatly appreciated.

Link to post
Share on other sites

Hi @@StevenSegal


It looks like you can do something similar with CounterLib. 


I did a real quick and dirty setup using an Adafruit MAX4466 electret microphone breakout for the analog input instead of the circuit referred to in your link.  I hooked the output from the amplifier into pin 18 (P2.2) on the F5529 and also to my oscilloscope.  I used the Few_Kilohertz_Counter that chicken included in the examples with his library and TimerA2.  Then I blew a note on my grandson's harmonica into the microphone.  This is what the oscilloscope showed:




It is showing about 1.3 kHz with a regular (well kind of regular) signal.  The mean is about 1.5 V and it is varying 0.5 V around it. The breakout has adjustable gain but I didn't bother adjusting it.


At the same time I saw this on the serial monitor coming from the F5529:




You can see that it is showing about 1.3 kHz also.


This may or may not be good enough for what you are trying to do but I suspect the Arduino code you linked to would give the same quality results.  The Arduino sketch could be ported to the F5529 but all code that refers to registers would have to be rewritten.

Link to post
Share on other sites

Rather than using ADC, have you considered using the comparator?  (I believe this device has one).  You would have to see if it does what you want (e.g. can you set threshold voltage close enough to what you want to do).  

If it did, you might be able to just set up the timer and comparator, go to sleep until you got a comparator interrupt, 

then record the crossing, reset the comparator, and go back to sleep.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...