Measure Frequency with KY-038

Hello.
i have arduino and module sound KY-038, and I want to measure frequency of the sound with the sensor. i already tried with the FFT, but it always give me error.
do you have another method to get the frequency?
Thank you

THIS ONE should work, it uses the same code and connections as yours.

Hi,
What sounds are you sampling?

Have you tried a single tone?

Tom.. :smiley: :+1: :coffee: :australia:

Hello,
i used it before, and it work. now i want to measure the frequency of the sound with FFT. i followed this project and i failed.

https://projecthub.arduino.cc/lbf20012001/audio-frequency-detector-d300e3

Hi, @farhan562

Please post your code?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi tom.
thanks want to help.
im new to using arduino. so, I use arduino uno R3 and module sound KY-038. and then, I connect A0, G, plus pin from the sensor to A0, GND, and 5V pin from Arduino.
for the code, I use it from this link

https://projecthub.arduino.cc/lbf20012001/audio-frequency-detector-d300e3

here's the code

/*
 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 https://clydelettsome.com/blog/2019/12/18/my-weekend-project-audio-frequency-detector-using-an-arduino/

*/

#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
}

and i already install the FFT library
WhatsApp Image 2024-06-27 at 00.24.48

when i tried to compile, there is an error appear
the error in line 28: Compilation error: 'arduinoFFT' does not name a type; did you mean 'ArduinoFFT'?

i try to change the word arduinoFFT to ArduinoFFT, but still appears error.

could you help me?

KY-038 can only measure loudness. Frequency is not within its scope.

Here you will find more information. Is there a library to measure sound frequency using the ky-038 sensor?

Follow this tutorial, it works.

It will plot the FFT on the serial monitor.
If you are using an Uno, you need to set the number of samples to 128 or less
For low frequency signals, set the sampling frequency to less than 8000, it will provide bettor resolution.

I believe you are correct, but there is also a software error. The code doesn't know what's connected. The code should run but give incorrect results.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.