This Arduino project displays the approximate frequency of the loudest sound detected by a sound detection module.
Devices and components
Arduino Uno Rev3
Full Size Solderless Breadboard
Jumper wires (generic)
USB-A to B cable
DEVMO High Sensitivity Sound Detection Module for Arduino
Materials and tools
Computer
Project description
AudioFrequencyDetectorV1-0.ino
arduino
This code/sketch displays the approximate frequency of the loudest sound detected by a sound detection module.
1/*
2 File/Sketch Name: AudioFrequencyDetector
3
4 Version No.: v1.0 Created 12 December, 2019
5
6 Original Author: Clyde A. Lettsome, PhD, PE, MEM
7
8 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
9 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
10 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
11 domain result is then determined and displayed via the Arduino IDE Serial Monitor.
12
13 Note: The arduinoFFT.h library needs to be added to the Arduino IDE before compiling and uploading this script/sketch to an Arduino.
14
15 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
16 version of your choice, as published by the Free Software Foundation.
17
18 Notes: Copyright (c) 2019 by C. A. Lettsome Services, LLC
19 For more information visit https://clydelettsome.com/blog/2019/12/18/my-weekend-project-audio-frequency-detector-using-an-arduino/
20
21*/
22
23#include "arduinoFFT.h"
24
25#define SAMPLES 128 //SAMPLES-pt FFT. Must be a base 2 number. Max 128 for Arduino Uno.
26#define SAMPLING_FREQUENCY 2048 //Ts = Based on Nyquist, must be 2 times the highest expected frequency.
27
28arduinoFFT FFT = arduinoFFT();
29
30unsigned int samplingPeriod;
31unsigned long microSeconds;
32
33double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
34double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values
35
36void setup()
37{
38 Serial.begin(115200); //Baud rate for the Serial Monitor
39 samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
40}
41
42void loop()
43{
44 /*Sample SAMPLES times*/
45 for(int i=0; i<SAMPLES; i++)
46 {
47 microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script.
48
49 vReal[i] = analogRead(0); //Reads the value from analog pin 0 (A0), quantize it and save it as a real term.
50 vImag[i] = 0; //Makes imaginary term 0 always
51
52 /*remaining wait time between samples if necessary*/
53 while(micros() < (microSeconds + samplingPeriod))
54 {
55 //do nothing
56 }
57 }
58
59 /*Perform FFT on samples*/
60 FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
61 FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
62 FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
63
64 /*Find peak frequency and print peak*/
65 double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
66 Serial.println(peak); //Print out the most dominant frequency.
67
68 /*Script stops here. Hardware reset required.*/
69 while (1); //do one time
70}
71
Downloadable files
Audio frequency detector
This Arduino project displays the approximate frequency of the loudest sound detected by a sound detection module.
Audio frequency detector
Audio frequency detector
This Arduino project displays the approximate frequency of the loudest sound detected by a sound detection module.
Audio frequency detector
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.