Frequency detector with Sound sensor

Hello all,

I am planning to use sound detector board with MIC to detect frequency or measure frequency and dB.
I have arduino mega 2560 and uno .

not sure where to start with

https://learn.sparkfun.com/tutorials/sound-detector-hookup-guide

Thank you

Are you trying to make a "fun display" or accurate measurements? i.e. Most people have failed trying to make a guitar tuner.

You can make a crude SPL meter, but it has to be calibrated (using an already-calibrated SPL meter) and it's not going to be accurate enough for "serious" loudness measurements.

But lots of people make really nice "spectrum analyzer effects". (You can find examples on YouTube.)

There are FFT and FHT libraries for the Arduino that will give you the amplitudes of multiple frequency bands., and that's the basis of a spectrum analyzer effect.

With that particular sound detector use the "audio" output. The "envelope" only gives you loudness information (no frequency information) and the "gate" just tells you if you are louder or quieter than some threshold.

P.S.
Start with simple loudness before playing around with frequency analysis.

You might want to take a look at my [u]World's Simplest Lighting Effect[/u] which flashes an LED (or lamp) on when the loudness is above average and off when it's below average. It should work with your sound sensor. It doesn't attempt to calculate dB or do any frequency analysis but it's a starting point and it might give you some ideas.

P.P.S.
In case you don't know this already, all real sounds contain multiple-simultaneous frequencies. Only a pure sine wave is a single frequency.

DVDdoug:
Are you trying to make a "fun display" or accurate measurements? i.e. Most people have failed trying to make a guitar tuner.

You can make a crude SPL meter, but it has to be calibrated (using an already-calibrated SPL meter) and it's not going to be accurate enough for "serious" loudness measurements.

But lots of people make really nice "spectrum analyzer effects". (You can find examples on YouTube.)

There are FFT and FHT libraries for the Arduino that will give you the amplitudes of multiple frequency bands., and that's the basis of a spectrum analyzer effect.

With that particular sound detector use the "audio" output. The "envelope" only gives you loudness information (no frequency information) and the "gate" just tells you if you are louder or quieter than some threshold.

P.S.
Start with simple loudness before playing around with frequency analysis.

You might want to take a look at my [u]World's Simplest Lighting Effect[/u] which flashes an LED (or lamp) on when the loudness is above average and off when it's below average. It should work with your sound sensor. It doesn't attempt to calculate dB or do any frequency analysis but it's a starting point and it might give you some ideas.

P.P.S.
In case you don't know this already, all real sounds contain multiple-simultaneous frequencies. Only a pure sine wave is a single frequency.

Thank you for your comment.

Measuring the frequency of signals like square waves, sin waves and triangle & saw tooth waves is quite simple. But real sounds like a piano or guitar are very tricky.

This is because real sounds are complex wave shapes and it is hard to pick out when one cycle ends and the next starts, something relatively easy with a mathematically defined wave shape. What makes it even harder is the fact that real sounds change their harmonic contend ( wave shape ) as the sound continues.

Detection the loudness in dBs is not just a matter of taking the peak signal either.

Detecting a sound or differentiating a sound, for example a babies cry from an air conditioner unit requires machine learning or AI techniques and is several steps up the ladder of knowledge than beginner.

Grumpy_Mike:
Measuring the frequency of signals like square waves, sin waves and triangle & saw tooth waves is quite simple. But real sounds like a piano or guitar are very tricky.

This is because real sounds are complex wave shapes and it is hard to pick out when one cycle ends and the next starts, something relatively easy with a mathematically defined wave shape. What makes it even harder is the fact that real sounds change their harmonic contend ( wave shape ) as the sound continues.

Detection the loudness in dBs is not just a matter of taking the peak signal either.

Detecting a sound or differentiating a sound, for example a babies cry from an air conditioner unit requires machine learning or AI techniques and is several steps up the ladder of knowledge than beginner.

Thank you
Grumpy_Mike

Actually this is my plan , measuring frequency with square and Sin wave.

Could I ask you where can I find relevant information about this ?
thanks

There is a lot of unnecessary stuff like the LCD that can be left out and replace with a print to the serial monitor.
https://create.arduino.cc/projecthub/ambhatt/frequency-and-duty-cycle-measurement-using-arduino-1e4896

Grumpy_Mike:
There is a lot of unnecessary stuff like the LCD that can be left out and replace with a print to the serial monitor.
Frequency and Duty Cycle Measurement Using Arduino | Arduino Project Hub

I tried code and mic
sadly it didn't work

I tried code and mic
sadly it didn't work

And how did you connect your microphone to pin 7? The digital output of that device you have is only an envelope not a frequency.

Actually this is my plan, measuring frequency with square and Sin wave.

You can't do that with a microphone, unless you condition the audio signal into a square wave and apply it to a digital pin.

I managed to run something with this code

I connect Audio output pin from microphone to A5 (ardunio uno)

Please let me know what you thinking

const int pulsePin = A5;                 // Signal In


int pulseHigh;                           // Pulse High Time in uS
int pulseLow;                            // Pulse Low time in uS
float pulseTotal;                        // Total of high & low times
float frequency;                         // Calculated Frequency
//---------------------------------------------------------------
void setup()
{
    Serial.begin(9600);
    pinMode(pulsePin,INPUT);              //Set pin 8 for Input
  
}


//---------------------------------------------------------------
void loop()
{
    pulseHigh = pulseIn(pulsePin,HIGH);   //get the high
    pulseLow = pulseIn(pulsePin,LOW);     //get the low
    
    pulseTotal = pulseHigh + pulseLow;    // add em up
    frequency=1000000/pulseTotal;         // convert to Hz with MATH!


      Serial.print ("Frequency: ");
      Serial.print ((unsigned long) frequency);
      Serial.println (" Hz.");
    delay(1000);                           //take a break
}

What sort of results do you get?

The problem with what you are doing is that you are feeding an analogue input into a digital pin. This means that the triggering is not going to be at a consistent voltage. What you need to do is to feed the audio signal into a voltage comparator before you feed it into a digital pin to do the time measurements. This will ensure you always trigger at the same point or threshold voltage.

There is still a problem with this because some audio signals might have noise on them or harmonics in the waveform that cross the threshold voltage many times rapidly within the cycle you are trying to measure. You counteract this by introducing some hysteresis into the comparator. That is the threshold for a signal going up is different to a signal going down. This sounds complex but is only a case of putting a resistor or two between input and output.

https://www.daycounter.com/Calculators/Comparator-Hysteresis-Calculator.phtml

There is a comparator built into the Arduino, it is described here:-
https://www.gammon.com.au/forum/?id=11916
But that triggers an interrupt and so the software involved in making it work is a bit more complex.

Actually this is my plan , measuring frequency with square and Sin wave.

Obviously, those have to be electronically generated and it you can get-to the electrical signal you'll probably get better results than picking-up sound waves with a microphone. There is usually acoustic noise and speakers are not very good at generating square waves (and the air doesn't "hold" the flat parts of the wave).

And... you probably know the frequency already! :wink:

The linked thread is an implementation of a frequency detector on an Arduino Uno that works pretty well (see post #42). This implementation uses an autocorrelation approach which is robust against signals with harmonic content. I've tested it with a nominal pure sine and square waves as well as a variety of musical instruments (piano, recorder, guitar).

Those sound detector boards you've linked don't have much analog gain on the microphone so you need a pretty loud signal to get a usable signal.

Hi,

summary

Equipment I have

  • SparkFun Sound Detector ( SEN-12642 )
  • Arduino Mega256 and uno

Requirement

  • Detect 3.6khz frequency from buzzer which is pure wave (sine or square )
  • Remove unwanted noise
  • Remove human noise

Thank you

Equipment I have

Is not enough.

Requirement

Is unfeasible.

samanshams:
Equipment I have

  • SparkFun Sound Detector ( SEN-12642 )
  • Arduino Mega256 and uno

Requirement

  • Detect 3.6khz frequency from buzzer which is pure wave (sine or square )
  • Remove unwanted noise
  • Remove human noise

I posted some sample code (post #15) in this thread that detects a 1 kHz tone and is moderately robust against other noise in the environment. The approach was to use a narrow band digital filter centered on the tone of interest and compare the signal power out of that filter with the full band power, declaring the tone of interest active when it's power exceeds 50% of the wide band power.

As written this sketch sampled audio at 2.5 kHz on an Arduino Uno, so it wouldn't easily be adapted to detect 3.4 kHz tone, but the technique may be relevant to your application. There are optimizations that could be done to this code, but it probably isn't going to be fast enough on 8-bit AVR like the Uno. I've run essentially this same code on a STM32 (32 bit / 70 MHz / ARM processor) at a 40 kHz sample rate if you'd consider moving to a higher performance processor.