Hello everyone, I'm am currently trying to make a acoustic guitar tuner as a project and I am running into some problems. Currently using a Arduino Uno, swapping between two sound sensors to check if there is any difference and a lCD display with I2C which i will remove for simplicity for now. These are my sound sensors:first one is and second one.
My problem is that when i struck any of the string the frequencies are either way higher than the ones that should be either because of the mic quality or environmental noises (or both).
My Arduino code: Arduino G-tuner - Pastebin.com
#include <arduinoFFT.h>
#define SAMPLES 128
#define SAMPLING_FREQUENCY 22050 // Adjust this based on your microphone's characteristics
arduinoFFT FFT = arduinoFFT();
unsigned int samplingPeriod;
unsigned long microSeconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
// Define the frequency range for the guitar strings
const double MIN_FREQUENCY = 40.0; // Adjust based on your guitar's lowest string
const double MAX_FREQUENCY = 1000.0; // Adjust based on the highest string
void setup() {
Serial.begin(115200);
samplingPeriod = round(1000000.0 / SAMPLING_FREQUENCY);
}
void loop() {
// Read microphone input
for (int i = 0; i < SAMPLES; i++) {
microSeconds = micros();
vReal[i] = analogRead(0) - 512.0; // Center the signal around 0
vImag[i] = 0;
while (micros() < (microSeconds + samplingPeriod)) {
// Wait for the desired sampling period
}
}
// Perform FFT
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 (dominant note)
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
// Check if the detected peak frequency is within the desired range
if (peak >= MIN_FREQUENCY && peak <= MAX_FREQUENCY) {
// Print detected peak frequency to Serial Monitor
Serial.print("Detected Guitar String Frequency: ");
Serial.print(peak);
Serial.println(" Hz");
}
//delay(1000); // Add a delay of 1 second (adjust as needed)
}
I'm also thinking of adding a pick(vibration sensor) if that may help to cancel any environmental noises.
Any help towards the code or new components is welcome.
My apologies for the code.