This code/sketch displays the approximate frequency as well as the musical note played on an electronic keyboard or piano app. For this project, the analog output from the sound module detector is sent to the A0 analog input of the Arduino Uno. The analog signal is sampled and quantized (digitized). Autocorrelation, weighting and tuning code is used to find fundamental frequency using the first 3 periods. The approximate fundamental frequency is then compared to frequencies in octaves 3, 4, and 5 range to determine the closest musical note frequency. Finally the guessed note for the closest frequency is printed to the screen.
The Chaos of a Raw Audio Wave
Unlike a clean digital signal, an analog sound wave from a musical instrument is a chaotic mess of overlapping frequencies, harmonics, and background noise.
- A KY-038 Sound Sensor or MAX4466 Electret Microphone is wired to the Arduino's Analog
A0pin. - The Arduino must sample the raw voltage incredibly fast (e.g., 20,000 times a second) utilizing direct ADC register manipulation, filling a massive RAM array with the data.
- If you plot that data, it looks like a crazy, complex waveform.
The FFT Algorithmic Decoding
The core of the note detection lies in a powerful algorithm like the Fast Fourier Transform (FFT). This project uses autocorrelation, but the principle is similar to FFT analysis.
- It takes the massive, messy squiggly line from the microphone.
- It mathematically dissects the single line into its constituent frequencies.
- The Peak Detection Protocol: The code sweeps through the analyzed data to find the fundamental frequency.
- If the detected frequency is
439.8 Hz, the Arduino's logic knows it's fundamentally an A4 note! - It instantly updates the display, printing the identified note.
Audio Analysis Needs
- Arduino Uno/Nano (The autocorrelation and FFT math pushes the ATmega processor to its computational limit).
- A Proper Analog Microphone (Cheap digital sound sensors only output a High/Low signal and cannot be used for frequency analysis. You must capture a full analog waveform!).
- A display, such as a 0.96" SSD1306 I2C OLED Screen, to cleanly output the detected musical note.
For more information about this project please visit this link: More Information