ATmega328P - Pure Tone Detection

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to detect pure tone (sine wave) frequencies using an Arduino Uno and an Electret Microphone.

 

Can you tell me if this frequency measurement library would be a good way to go about it and how

I would connect an electret mic to this circuit?

 

 

 

Last Edited: Fri. Feb 2, 2018 - 01:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The microphone you selected has an analog output wich is meant to put directly to the ADC of your uC.

 

On the sparkun site there is also a tutorial with software on how to use this microphone with an uC:

https://learn.sparkfun.com/tutor...

And that also links to the instructables website for more projects.

 

You might also want to look into FFT (Fast Fourier Transform).

It's quite a complicted piece of math, but there are a lot of libraries and examples on the 'web.

Inclusive examples for AVR's.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to tell more about your requirements, and the various possible input signals.

 

The FFT, (or DFT), can tell you the frequencies and their relative energies within the signal sample.

It is, as noted, computationally intense, but runs well on a PC.

 

If you only need to detect the presence or absence of a known frequency within the input signal, you can certainly use the FFT (DFT), or you can use the Goertzel Algorithm to just check for that specific frequency.

 

For real world applications, you can sometimes filter the sin wave, then clip the sin wave and convert it into a square wave.

With a square wave on can skip the fancy FFT (DFT) or Goertzel algo approaches, and just measure the successive periods of the signal in the time domain, which is fast and easy.

Whether or not this works depends upon the nature of the input signal and what you are actually trying to do.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As all of the links are Arduino-related, I will move this thread into the Arduino forum.

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:

You need to tell more about your requirements, and the various possible input signals.

 

The FFT, (or DFT), can tell you the frequencies and their relative energies within the signal sample.

It is, as noted, computationally intense, but runs well on a PC.

 

If you only need to detect the presence or absence of a known frequency within the input signal, you can certainly use the FFT (DFT), or you can use the Goertzel Algorithm to just check for that specific frequency.

 

For real world applications, you can sometimes filter the sin wave, then clip the sin wave and convert it into a square wave.

With a square wave on can skip the fancy FFT (DFT) or Goertzel algo approaches, and just measure the successive periods of the signal in the time domain, which is fast and easy.

Whether or not this works depends upon the nature of the input signal and what you are actually trying to do.

 

JC

 

Thanks JC.  I’m working on an audio (pure tone) controlled robot.  Based on the (predefined) pure tone/sine wave frequency that is detected, a corresponding command will be issued to the robot to carry out a particular operation.  The robot will need to be able to detect five different pure tones/specific frequencies (from an online tone generator) each relating to a specific command/operation.  E.g. 12, 14, 16, 18, and 20 KHz.  I've read that it's a good idea to use frequencies that are high enough to measure one cycle in the least possible time, yet distinct enough to measure with very low probability of mistaking one frequency for another.   And that frequencies that are multiples of each other should be avoided in order to prevent aliasing errors.  

 

I have heard of FFT (DFT), FHT and Goertzel algorithms but have not used them before.  Any pointers to projects/code that I could learn from would be greatly appreciated.

Last Edited: Sat. Feb 3, 2018 - 01:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

The microphone you selected has an analog output wich is meant to put directly to the ADC of your uC.

 

On the sparkun site there is also a tutorial with software on how to use this microphone with an uC:

https://learn.sparkfun.com/tutor...

And that also links to the instructables website for more projects.

 

You might also want to look into FFT (Fast Fourier Transform).

It's quite a complicted piece of math, but there are a lot of libraries and examples on the 'web.

Inclusive examples for AVR's.

 

Thanks Paulvdh, appreciate the advice.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Steve.

 

If you are not constrained by project definitions (eg a school project), I would suggest that you consider/investigate the use of DTMF encde/decode operations. These are the tones that modern telephones generate.

 

A cheap (and operational) decoder is available for pennies.

 

https://www.ebay.com.au/itm/MT88...

 

I have recently used the same chip in a commercial product that interfaced to an iPad via its earphone jack to control leds.

 

Cheers,

 

Ross

 

ps... and you can test it using a free DTMF generator software available from ...

 

http://www.dtmfdial.com/

 

 

Ross McKenzie ValuSoft Melbourne Australia

Last Edited: Fri. Feb 2, 2018 - 02:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The challenge with detecting tones is you have noise. Just looking at the pulse widths will most likely give you bad results. With a microphone you probably need automatic gain control (agc) and bandpass filtering. You then measure the energies of the frequencies of interest.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

Hi Steve.

 

If you are not constrained by project definitions (eg a school project), I would suggest that you consider/investigate the use of DTMF encde/decode operations. These are the tones that modern telephones generate.

 

A cheap (and operational) decoder is available for pennies.

 

https://www.ebay.com.au/itm/MT88...

 

I have recently used the same chip in a commercial product that interfaced to an iPad via its earphone jack to control leds.

 

Cheers,

 

Ross

 

ps... and you can test it using a free DTMF generator software available from ...

 

http://www.dtmfdial.com/

 

 

 

Thanks very much Ross.  I'm free to use whatever hardware/software I want - time, however, is not on my side (my own fault).

If I don't find a solution to this problem in the very near future i.e. a week or so, I'm in trouble.

 

Would the DTMF approach be difficult to implement?  I basically need the Arduino to command a motor

controller in 5 different ways (forward, reverse, left, right, stop), each depending on detection of a specific

pure tone/frequency.

Last Edited: Fri. Feb 2, 2018 - 07:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Given your time constraints, i’d say the dtmf approach is most likely to be quicker and more likely to work.

Google arduino dtmf

Last Edited: Fri. Feb 2, 2018 - 08:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Given your time constraints, i’d say the dtmf approach is most likely to be quicker and more likely to work. Google arduino dtmf

 

Thanks Kartman, I've actually been googling it there and it looks doable, plenty of projects and code examples out there.

I just need to get my hands on the MT8870 asap :)  Also, thanks for your advice regarding agc and bandpass filtering.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The easyest to implement is probably your original idea of just measuring the frequency.

It is also the most unreliable because of other sounds being picked up & noise from the robot itself.

 

DTMF (Dual Tone Multi Frequency) works by checking if very specific frequencies are present in the audio and ignoring everything else.

It's so robust that it's used worldwide in (probably) all phone systems.

According to Wikipedia DTMF is in use in commercial products since 1963

Back then microcontrollers did not exist yet and the tone generation and detection was done with filters and analog electronics.

Later miniaturisation resulted  "DTMF decoder" chips which could be easily wired to a microcontroller.

And then again years later the microcontrollers became powerfull enough to handle the DTMF decoding in software.

 

Github is always a nice place to start for sourcecode:

https://github.com/search?q=dtmf...

 

For the "Sender' you can of course use any phone which can generate these tones loud enough to be picked up by your robot.

Maybe put an amplifer & speaker on an old analog phone...

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

The easyest to implement is probably your original idea of just measuring the frequency.

It is also the most unreliable because of other sounds being picked up & noise from the robot itself.

 

DTMF (Dual Tone Multi Frequency) works by checking if very specific frequencies are present in the audio and ignoring everything else.

It's so robust that it's used worldwide in (probably) all phone systems.

According to Wikipedia DTMF is in use in commercial products since 1963

Back then microcontrollers did not exist yet and the tone generation and detection was done with filters and analog electronics.

Later miniaturisation resulted  "DTMF decoder" chips which could be easily wired to a microcontroller.

And then again years later the microcontrollers became powerfull enough to handle the DTMF decoding in software.

 

Github is always a nice place to start for sourcecode:

https://github.com/search?q=dtmf...

 

For the "Sender' you can of course use any phone which can generate these tones loud enough to be picked up by your robot.

Maybe put an amplifer & speaker on an old analog phone...

 

Brilliant, thanks a lot Paulvdh.  I'm definitely going with DTMF, I've just ordered an MT8870 which I hope to receive within the next week :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not clear to me what you really want.

 

Do you want a audio remote where you push 5 different keys that then play one of 5 tones , or do you want to whistle the tones ?

 

And I don't see why not do it in SW. (both send and receiver)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

stevesy wrote:
If I don't find a solution to this problem in the very near future i.e. a week or so, I'm in trouble.
stevesy wrote:
I've just ordered an MT8870 which I hope to receive within the next week :)
sparrow2 wrote:
It's not clear to me what you really want.
ditto.

Have Fun.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Fri. Feb 2, 2018 - 10:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

stevesy wrote:

 E.g. 12, 14, 16, 18, and 20 KHz. 

 

If you must use these frequencies DTMF isn't going to help, DTMF uses frequencies that are an order of magnitude lower in frequency.  If you are allowed to use any frequency you like then by all means go for DTMF.

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:

It's not clear to me what you really want.

 

Do you want a audio remote where you push 5 different keys that then play one of 5 tones , or do you want to whistle the tones ?

 

And I don't see why not do it in SW. (both send and receiver)

 

I want a tone controlled robot.  I had planned to use an online tone generator for pure tones instead of whistling (I can't whistle).

As I mentioned I'm happy to go with a phone/DTMF to control the robot, I'm under pressure timewise.

Last Edited: Fri. Feb 2, 2018 - 11:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David Bowie was under pressure, but unfortunately he ran out of time as did Freddie.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

digitalDan wrote:

stevesy wrote:

 E.g. 12, 14, 16, 18, and 20 KHz. 

 

If you must use these frequencies DTMF isn't going to help, DTMF uses frequencies that are an order of magnitude lower in frequency.  If you are allowed to use any frequency you like then by all means go for DTMF.

 

Yes digitalDan I can use whatever frequencies I like.  I just included those ones as an example of frequencies I might use, if I were using the electret mic.  Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

stevesy wrote:
If I don't find a solution to this problem in the very near future i.e. a week or so, I'm in trouble.
stevesy wrote:
I've just ordered an MT8870 which I hope to receive within the next week :)
sparrow2 wrote:
It's not clear to me what you really want.
ditto.

Have Fun.

 

Apologies for my lack of clarity or any confusion caused.  I'm very grateful to you all for your help with this, cheers.

Last Edited: Sat. Feb 3, 2018 - 01:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Steve,

 

Have just returned to see that my suggestion in post#7 has been taken up.

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

Steve,

 

Have just returned to see that my suggestion in post#7 has been taken up.

 

Cheers,

 

Ross

 

 

Yes, can't thank you enough Ross.  I ordered the MT8870 on ebay.ie (I'm based in Ireland). 

It's being shipped from England so shouldn't take too long to arrive :)

Last Edited: Sat. Feb 3, 2018 - 01:47 PM