Go Down

Topic: Auto Guitar Tuner (Read 150 times) previous topic - next topic

rfarnes

Apr 24, 2019, 09:33 am Last Edit: Apr 24, 2019, 09:36 am by rfarnes
I am totally new to arduino coding, and I already know that this project is over my head, but I am stuck with it as it is for a class.
For the project, a guitar note is selected which the correct frequency on the aruidno,  a MAX4466 microphone picks up the sound from the guitar string when strumming the guitar note, it send this input to the arudino to tell it if the note is to high or too low for the selected note, then a motor will turn the tuning nut until the note is tuned and a LED light will come on to tell me the note is in the correct frequency range.
My questions are:
Does the MAX4466 microphone pick up the frequency of the guitar or just the sound without the frequency?
If it is just the sound, what do I need to tell the arudino for it to figure out the frequency of this sound so it can tell the motor what to do?
Currently, the code I created, the microphone can't pick up the guitar notes coming in and the lights will only come on by having my phone right up to the microphone.
Any advice is appreciated, my teacher and teacher assistants are not very helpful at this point.
Below is what I  have for just the frequency and light portion of the code:
// Note frequencies
// NOTE_E2 82.41
// NOTE_A3 220
// NOTE_D3 146.83
// NOTE_G3 196
// NOTE_B3 246.94
// NOTE_E4 329.63
///Turns on the LED according to sound level recorded by the sound or microphone sensor.

int led1 = 2;

int led2 = 3;

int led3 = 4;

int led4 = 5;

int led5 = 6;

int led6 = 7;

int sensorPin = A0; // input pin for the sensor

int sensorval = 0; // variable for the value coming from the sensor

// the setup routine runs once when you press reset:

void setup() { // initialize the digital pin as an output.

pinMode(led1,OUTPUT); //Note E2 green1 right most side

pinMode(led2,OUTPUT); //Note D3 white

pinMode(led3,OUTPUT); //Note G3 blue

pinMode(led4,OUTPUT); //Note A3 yellow

pinMode(led5,OUTPUT); //Note B3 red

pinMode(led6,OUTPUT); //Note E4 green2 left most side

pinMode(sensorPin, INPUT); // initialize sensor as input

Serial.begin(9600); // initialize serial communication with computer, Sets the ata rate in bits per second for serial data transmission }

}
void loop() {
sensorval = analogRead(sensorPin); // read the value from the sensor , baseline is 343

Serial.println(sensorval); // send it to the computer's serial port screen

if ((sensorval < 70) && (sensorval < 100)) { digitalWrite(led1, HIGH); Serial.println(sensorval); Serial.println("green1"); } else { digitalWrite(led1,LOW); }//Note E2 82.41

if ((sensorval < 140) && (sensorval < 150)) { digitalWrite(led2, HIGH);Serial.println(sensorval); Serial.println("white"); }else { digitalWrite(led2,LOW); }//Note D3

if ((sensorval < 190) && (sensorval < 200)) { digitalWrite(led3, HIGH);Serial.println(sensorval); Serial.println("blue"); } else { digitalWrite(led3,LOW); }//Note G3

if ((sensorval < 215) && (sensorval < 225)) { digitalWrite(led4, HIGH); Serial.println(sensorval); Serial.println("yellow");} else{ digitalWrite(led4,LOW); }//Note A3

if ((sensorval < 245) && (sensorval < 255)) { digitalWrite(led5, HIGH); Serial.println(sensorval); Serial.println("red");} else { digitalWrite(led5,LOW); }//Note B3

if ((sensorval < 320) && (sensorval < 335)) { digitalWrite(led6, HIGH);Serial.println(sensorval); Serial.println("green2"); }else { digitalWrite(led6,LOW); }// Note E4

delay(100); }

slipstick

The MAX4466 is not a microphone, it is a preamplifier IC. I guess you have a board from somewhere containing a an electret microphone and a MAX4466 preamp. A link to that would be good but I am pretty confident that the output will just be the amplitude of the sound taking no account at all of the frequency.

Fortunately you are not the first person to try to make a guitar tuner. If you search for "Arduino guitar tuner" you'll find details of lots of similar projects.

BTW your if statements don't make any sense. If sensorval is less than 70 it will always be less than 100 too. It will also be less than all the other values in all the other if statements.

Steve

rfarnes

Sorry, yes the MAX4466 is connected to a electrec microphone- here is a link of what I am using https://www.adafruit.com/product/1063. I have researched old auto guitar tuner projects, but from most of them, I can't tell how they are getting their frequency data from the guitar to the aurdino or how the aruidno is processing the frequency.
Also, I think I see my mistake in my code, does this make sense now? This is the last part of the code where there was a mistake. I want to have an LED turn on when the sensorval is in between a certain range of htz. Fo example, for the first one, if the frequency input from the guitar was 84, then green1 led would turn on.
Thank you.

if ((70 < sensorval) && (sensorval < 100)) { digitalWrite(led1, HIGH); Serial.println(sensorval); Serial.println("green1"); } else { digitalWrite(led1,LOW); }//Note E2 82

if ((200< sensorval) && (sensorval < 230)) { digitalWrite(led2, HIGH);Serial.println(sensorval); Serial.println("white"); }else { digitalWrite(led2,LOW); }//Note A2 110

if ((140 < sensorval) && (sensorval < 150)) { digitalWrite(led3, HIGH);Serial.println(sensorval); Serial.println("blue"); } else { digitalWrite(led3,LOW); }//Note D3 146

if ((186 < sensorval) && (sensorval < 200)) { digitalWrite(led4, HIGH); Serial.println(sensorval); Serial.println("yellow");} else{ digitalWrite(led4,LOW); }//Note G3 196

if ((239 < sensorval) && (sensorval < 255)) { digitalWrite(led5, HIGH); Serial.println(sensorval); Serial.println("red");} else { digitalWrite(led5,LOW); }//Note B3 246

if ((320 < sensorval) && (sensorval < 335)) { digitalWrite(led6, HIGH);Serial.println(sensorval); Serial.println("green2"); }else { digitalWrite(led6,LOW); }// Note E4 329

delay(100); }

DVDdoug

The microphone signal contains the complete sound/signal which includes amplitude & frequency and if you connect a power amplifier & speakers (or powered speakers) you'd hear it.

sensorval is ONLY amplitude.   And, it's the instantaneous amplitude representing one point in time "sampled" from a sound wave.   The wave is continuously varying.  It has a positive and negative* peak and it passes-through zero twice per cycle.  Your "raw readings" will "look random" within a range, depending on the amplitude.

The Audacity website has a little introduction to how audio is digitized (sampled).   But it doesn't get into the concepts of frequency or pitch.

To find the frequency, you can use FFT, FHT, or autocorrelation.    From what I've read, autocorrelation should work best, but it looks like most people who try to make guitar tuners with the Arduino aren't very successful.  :(

Real world sounds contain multiple frequencies.   It's these harmonics & overtones that make a guitar sound different from a trumpet, or what makes two singers sound different when they are singing the same song and the same notes.   ...And, you wouldn't "strum" the guitar, you would pluck one string at a time.




* The Arduino can't read negative voltages so the output of the microphone-board is biased at half the supply voltage so silence should read about 512 on the 10-bit ADC.   You can subtract-out the bias in software if you wish.

Grumpy_Mike

Quote
Does the MAX4466 microphone pick up the frequency of the guitar or just the sound without the frequency?
It picks up samples of the waveform of the sound. It doesn't identify any frequency component. That is the hard part, in fact he very hard part. This is because a sound from a string is a mixture of harmonics and that mix is constantly changing so identifying the actual note is not easy.

There are many examples of people's attempts at this sort of project, non I have seen have been entirely successful. Forget the motor part you will just snap strings. If you get the frequency identification working at 90% you will be doing well.

MrMark

In the thread linked below another poster and I collaborated on an autocorrelation-based pitch detection algorithm which should be sufficient for guitar tuning.  This was an enhancement to some work done earlier on this forum and elsewhere as one can see by following links in that thread:

https://forum.arduino.cc/index.php?topic=540969.0

Go Up