Guitar Tuner With An Arduino: by EE421ACDC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

instructables

Guitar Tuner With an Arduino

by EE421ACDC

Pitch is everywhere, but what exactly is it? This website will document the steps to create your very own Arduino
guitar tuner.

Guitar Tuner With an Arduino: Page 1


Step 1: Lets Talk Theory, "Understanding Pitch"

Pitch, which is often thought of as how high or low a 27.50, B0 = 30.87 all in HZ. We define a pitch as any
sound is, can be defined as the human perception of multiple of the zero frequency i.e. A*N where N is any
frequency. It is clear that humans can not detect real number greater than 0. That means that 55.00
frequency with the naked ear, however, our natural HZ, 110 HZ, 220 Hz, ... are all considered musically
idea of pitch can get us most of the way there. A. The same goes for the remaining G through B.
Pitches are denoted by the seven letters Using these repeating domains allows us to convert
A,B,C,D,E,F,G and A which is one octave above the any input frequency (in Hertz) to it's corresponding
initial A with a frequency of 2A. If we look at the keys pitch.
of a piano we see a repetition of those same musical
notes. Each set of eight notes is known as an octave We were not given this natural ability to perfectly
and each note can be flat (lower) or sharp (higher) detect pitch or frequency, but with technology and our
than the ideal pitch frequency. Lets take a look at understanding of signals, this can be done. This
rather small frequencies between 50 and 0. demonstration will outline how AC/DC achieved a
means to detect the pitch of a guitar string through
Starting with C the frequencies are, C0 = 16.35, D0 = frequency analysis.
18.35, E0 = 20.60, F0 = 21.83, G0 = 24.50 A0 =

Guitar Tuner With an Arduino: Page 2


Step 2: Lets Talk More Theory, "Frequency Retrieval From Circuit"

This image indicates the frequency layout for any note the guitar signal might produce. These ranges allow the
user to tell if the note being played is sharp or flat, and this is displayed in a later video.

Guitar Tuner With an Arduino: Page 3


Step 3: Building the Amplifying Circuit

How exactly do we get frequency from a raw input (x1) toggle switch (amazon)
sine wave? We'll get into how the Arduino handles
the conversion later, for now, lets build the input (x2) 9V batteries and snaps to power the amplifier
circuit. The input is taken directly from the audio jack
which intern receives the signal through the audio (x1) 2.1mm DC barrel jack (male, center +)
cable from the inductive pickups located on the guitar.
We need to collect the signal and then amplify it, after (x1) lots of wire/jumpers
that the raw voltage value is inputted into the board
as an analog input to be processed. (x1) bread board/ prototyping board

1) Lets begin by building the op-amp circuit. Take the NOTE: The Op Amp circuit takes the sound waves
time to gather all the components listed below. and increases the amplitude, so that the Arduino can
read it easily. The waveform without it would not be in
(1x) 10uF Capacitor (amazon) the detectable range of the Arduino of 0 to 5V. It then
sends the signal into the Arduino to be sampled,
(1x) 100nF Capacitor (amazon) which then samples the frequency of the sound
waves, and records it on the serial port so the user
(x1) TL082CP Op Amp (amazon) can see.

(x1) 1/4 inch Mono Audio Jack (amazon)

(x1) 220 ohm resistor (radioshack)

(x1) 22k ohm resistor (radioshack)

(x3) 100k ohm resistor (radioshack)

Guitar Tuner With an Arduino: Page 4


Step 4: Design and Construction of LCD Circuit

We need some way to out put the processed data more wires...
from the arduino, for this Guide we will be using the
following schematic to output with the 16x2 LCD and that's it. This is pretty simple, just connect the
display. For this you will use the same arduino wires in the same way that they are shown in the
connected to the op-amp circuit as well as; schematic.

(x1) 10 kOhm potentiometer for the LCD back-light NOTE: We used a 10 kOhm potentiometer as a
variable resistor to adjust the gain of the op amp. The
(x1) large value resistor (for power to the LCD) we gain is the amplitude of the output voltage divided by
used 22 kOhm. the amplitude of the input voltage.

Step 5: Full Pitch Detection Circuit

This video shows the full circuit put together, walks through the schematic and parts list, and goes over important
parts of the code.

//www.youtube.com/embed/UI_mEBMcYQY

Guitar Tuner With an Arduino: Page 5


Step 6: Lets Code This Thing!

Guitar Tuner With an Arduino: Page 6


//Free for use and modification //Written by: Noah St. // initialize the library with the numbers of the interface
Pierre and Ethan Gibson //Credit for frequency pins //Pin 12 = RS //Pin 11 = E //Pin 5 = DB4 //Pin 4
detection code to: = DB5 //Pin 3 = DB6 //Pin 2 = DB7 LiquidCrystal
//https://github.com/akellyirl/Arduino-Guitar-Tuner // lcd(12, 11, 5, 4, 3, 2);
include the library code: #include
void setup() { Serial.begin(115200);

analogReference(EXTERNAL); // Connect to 3.3V


#define LENGTH 512 analogRead(A0);

byte rawData[LENGTH]; int count; char noteName; //string output = 0; // set up the LCD’s number of
columns and rows: lcd.begin(16, 2);
// Sample Frequency in kHz const float sample_freq = lcd.setCursor(0,0); lcd.print(” EE421: Signals”);
8919; lcd.setCursor(0,1); lcd.print(” Guitar Tuner”);
delay(3000); lcd.clear(); lcd.setCursor(2,0);
int len = sizeof(rawData); int i,k; long sum, sum_old; lcd.print(“[“); lcd.setCursor(13,0); lcd.print(“]”); // Print
int thresh = 0; float freq_per = 0; byte pd_state = 0; a message to the LCD.

//Base 0 octave frequencies //float freq = 415; // dont count = 0; } float freq; void loop() { if (count <
neet this float Ffreq; float Note; LENGTH) { count++; rawData[count] =
analogRead(A0)>>2; } else { sum = 0; pd_state = 0;
char testput; String out = “b—-[[ ]]—-#”; int int period = 0; for(i=0; i < len; i++) { // Autocorrelation
octave_counter; float C = 16.35; float D = 18.35; float sum_old = sum; sum = 0; for(k=0; k < len-i; k++) sum
E = 20.60; float F = 21.83; float G = 24.50; float A = += (rawData[k]-128)*(rawData[k+i]-128)/256; //
27.50; float B = 30.87; Serial.println(sum);

// Peak Detect State Machine if (pd_state == 2 && float getFfreq(float freq){ octave_counter++; if(freq >
(sum-sum_old) <=0) { period = i; pd_state = 3; } if B){ return getFfreq(freq/2);} else return freq; }
(pd_state == 1 && (sum > thresh) && (sum-sum_old)
> 0) pd_state = 2; if (!i) { thresh = sum * 0.5; pd_state }
= 1; } } // for(i=0; i < len; i++) Serial.println(rawData[i]);
// Frequency identified in Hz if (thresh >100) {
freq_per = sample_freq/period;
//Serial.println(freq_per);

//Filter out frequencies that are too high to matter


if(freq_per < 400) { freq = freq_per; } else { freq = -1; }

} count = 0; Serial.println(freq); displayToLCD(freq);


delay(400); } }

void displayToLCD(float freq){ if(freq == -1) { return; }

if(freq >= 15.89){ // check if above minimum C;


octave_counter = -1; Ffreq=getFfreq(freq);

if((15.89<=Ffreq) & (Ffreq<=17.34)){ Note = C;


noteName = ‘C’; } else if((17.35<=Ffreq) &
(Ffreq<19.475)){ Note = D; noteName = ‘D’; } else
if((19.475<=Ffreq) & (Ffreq<21.215)){ Note = E;
Guitar Tuner With an Arduino: Page 7
noteName = ‘E’; } else if((21.215<=Ffreq) &
(Ffreq<23.185)){ Note = F; noteName = ‘F’; } else
if((23.185<=Ffreq) & (Ffreq<26.00)){ Note = G;
noteName = ‘G’; } else if((26.00<=Ffreq) &
(Ffreq<29.185)){ Note = A; noteName = ‘A’; } else
if((29.185<=Ffreq) & (Ffreq<31.785)){ Note = B;
noteName = ‘B’; }

float closeness0 = (Ffreq/Note); int cl1 = 0; cl1 =


int((closeness0-1)*100); // round to nearest whole
number if(Ffreq==Note){out = “b—-[[ ]]—-#”;} else
if(cl1==-1) out = “b—-[[ ]]—-#”; else if(cl1==1) out = “b
—-[[ ]]—-#”; else if(cl1==-2) out = “b—<<< >—–#”;
else if(cl1==2) out = “b—–< >>>—#”; else if(cl1==-3)
out = “b–<<<< >—–#”; else if(cl1==3) out = “b—–<
>>>>–#”; else if(cl1==-4) out = “b-<<<<< >—–#”; else
if(cl1==4) out = “b—–< >>>>>-#”; else if(cl1==-5) out
= “b<<<<<< >—–#”; else if(cl1==5) out = “b—–<
>>>>>>#”; } else{ Ffreq = -1; } // -1 one for too low //
count++;

lcd.setCursor(3,0); lcd.print(freq); lcd.setCursor(11,0);


lcd.print(“Hz”); lcd.setCursor(0,1); lcd.print(out);
lcd.setCursor(7,1); lcd.print(noteName);
lcd.setCursor(8,1); lcd.print(octave_counter); }

Step 7: Video Demonstration

This video consists of Ethan running through tuning Below is the close-up visual of the monitor of the
process with our electric guitar tuner and comparing pitch detection.
its results with a commercial guitar tuner.

//www.youtube.com/embed/DIPaQUTWZ8E
//www.youtube.com/embed/GBx_8yoomhY

Guitar Tuner With an Arduino: Page 8


Step 8: References

The construction of the LCD circuit was setup and designed from an online tutorial from the following linked web
page. This link also includes the circuit schematics for the LCD circuit.

https://www.arduino.cc/en/Tutorial/HelloWorld

The electric guitar signal amplifying circuit was developed with aid of another Instructable. The link contains the
corresponding circuit diagrams.

https://www.instructables.com/id/Arduino-Guitar-Tuner/

Additional online resources used for Arduino frequency detection:

http://www.akellyirl.com/arduino-guitar-tuner/

http://www.akellyirl.com/reliable-frequency-detection-using-dsp-techniques/

https://github.com/akellyirl/Arduino-Guitar-Tuner

Thanks for reading our Instructable, we hope you enjoy it!

Guitar Tuner With an Arduino: Page 9

You might also like