Having problems on Using sensor to read frequnecy of sound signal

Recently, i have started a arduino project related to sound detected function. Firstly, i have search online code and tested with a grove sound sensor. However, After testing, i realized that the sensor can't detect frequency form sound signal``,What sensor should i use for my project?

This is the code i have found online.

/*
File/Sketch Name: AudioFrequencyDetector

Version No.: v1.0 Created 12 December, 2019

Original Author: Clyde A. Lettsome, PhD, PE, MEM

Description: This code/sketch makes displays the approximate frequency of the loudest sound detected by a sound detection module. For this project, the analog output from the
sound module detector sends the analog audio signal detected to A0 of the Arduino Uno. The analog signal is sampled and quantized (digitized). A Fast Fourier Transform (FFT) is
then performed on the digitized data. The FFT converts the digital data from the approximate discrete-time domain result. The maximum frequency of the approximate discrete-time
domain result is then determined and displayed via the Arduino IDE Serial Monitor.

Note: The arduinoFFT.h library needs to be added to the Arduino IDE before compiling and uploading this script/sketch to an Arduino.

License: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) version 3, or any later
version of your choice, as published by the Free Software Foundation.

Notes: Copyright (c) 2019 by C. A. Lettsome Services, LLC
For more information visit My Weekend Project: Audio Frequency Detector w/ Arduino|ClydeLettsome.com

*/

#include "arduinoFFT.h"

#define SAMPLES 128 //SAMPLES-pt FFT. Must be a base 2 number. Max 128 for Arduino Uno.
#define SAMPLING_FREQUENCY 2048 //Ts = Based on Nyquist, must be 2 times the highest expected frequency.

arduinoFFT FFT = arduinoFFT();

unsigned int samplingPeriod;
unsigned long microSeconds;

double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values

void setup()
{
Serial.begin(115200); //Baud rate for the Serial Monitor
samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
}

void loop()
{
/Sample SAMPLES times/
for(int i=0; i<SAMPLES; i++)
{
microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script.

    vReal[i] = analogRead(0); //Reads the value from analog pin  0 (A0), quantize it and save it as a real term.
    vImag[i] = 0; //Makes  imaginary term 0 always

    /*remaining wait time between samples if  necessary*/
    while(micros() < (microSeconds + samplingPeriod))
    {
      //do nothing
    }
}

/*Perform FFT on samples*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal,  vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);

/*Find peak frequency and print peak*/
double peak = FFT.MajorPeak(vReal,  SAMPLES, SAMPLING_FREQUENCY);
Serial.println(peak);     //Print out the most  dominant frequency.

/*Script stops here. Hardware reset required.*/
while (1); //do one time

}

@foxychan416 welcome to the forum.

Yes you can as the code you found illustrates. However, you will probably have to amplify the analogue signal from the sound sensor (not the digital one) to make the signal large enough to give a decent reading. You should aim for a 5V peak to peak value.

You might want to look at this How to get the best out of this forum before you proceed any further. It tells you how to post code here, which you are not doing correctly.

I have also moved you post to a much better forum category.

Can you post a link to the grove sound sensor you are using please?

Apart from that what is the question you want answering?

Which Arduino are you using?

Are you sure you have it wired correctly?

Pin 1 (yel)- Microphone output --> A0
Pin 2 -
Pin 3 (red)- VCC --> 5V
Pin 4 (black) - GND --> GND

i have wired it correctly

Which Arduino are you using?

Arduino UNO R3

Arduino UNO R3

Have you tried the test code on the sensor wiki website?

@foxychan416

Thanks for the link. I can say that this sensor can't detect audio at all only the envelop of the audio. It is like a peak detector, or envelope detector, see:-

In order to use the FFT routine then you will have to get a different sound sensor, one that outputs the actual waveform. Like one of these:-

Thankyou so much

What happens if the sound contains mixed frequencies?
The result may have peaks but those will vary.
Then perhaps FFT to take that apart? I only know about adding waves and not much about that.

The most sensitive sound sensor I know of is a piezo disk on a thin, stiff surface with lots of area, and an amplifier.

If you use the ADC with 1.1V Reference, 1 volt is all you need to detect near full range.

Before you buy another module, why don't you try the test code.

The sensor is given by others, before doing the project,I have tried test code from grove websites and asked my tutor, and he told me the sensor can absolutely detect frequency from sound signal, as the result, i misuderstand that the function of the sensor

ok, thankyou

The sensor is given by others, before doing the project,I have tried test code from grove websites and asked my tutor, and he told me the sensor can absolutely detect frequency from sound signal, as the result, i misuderstand the function of the sensor

Did it work?

It works and println the value. However, i realized the value may related to the loudness of sound , not the frequency now. Thankyou for your help.

Well yes that program takes an average of the signal, so it will just be the sound level but it does prove that everything is connected correctly and it works.

How were you testing it with the FFT program?
Were you providing a constant sound input?
The FFT program only runs for a very very short time.

Hey Jim, can FFT be used to separate mixed frequencies?

Nice clean sine waves are not always the norm.

I have chips used for 7-bar equalizer displays that must use bypass filters, but they give volume for 7 frequency bands which is what I bought them for. I got 10 for the price of 4 at single piece price when I did buy.