Projects / Guitar tuner

Guitar tuner

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP3217.3 mASSD1306

    Wire the OLED

    Connect I2C and check it shows text with a simple test sketch.

  2. ESP32

    Wire the microphone

    Connect the MAX9814 to GPIO34.

  3. ESP32

    Upload the sketch

    Open the serial monitor at 115200.

  4. ESP32

    Test with a tone

    Play 110 Hz from a phone tone app near the microphone and see A2.

  5. ESP32

    Detune the tone

    Play 115 Hz and check the cents reading rises.

  6. ESP32

    Try a guitar

    Pluck the open A string and watch the needle.

  7. ESP32

    Tune

    Tighten or loosen until the needle sits in the centre.

  8. ESP32

    Try the other strings

    Lower strings read more steadily than the high E.

In short

How do you build a guitar tuner?

A microphone and an ESP32 find the pitch of a plucked string and show how sharp or flat it is on an OLED. You need ESP32 DevKit V1, MAX9814 microphone with AGC, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MAX9814 VDDESP32 3V3Power
MAX9814 GNDESP32 GNDGround
MAX9814 OUTESP32 GPIO34Audio, an input-only ADC pin
OLED VCCESP32 3V3Power
OLED GNDESP32 GNDGround
OLED SDAESP32 GPIO21I2C data
OLED SCLESP32 GPIO22I2C clock

Code

The MAX9814 has automatic gain control and a DC bias, which the code removes by subtracting the mean. Autocorrelation compares the signal with a delayed copy of itself; the first strong peak sits at the period of the note. Sampling at 8 kHz limits the resolution, so the code interpolates around the peak, which should get within roughly 5 to 10 cents. Check it against a phone tone app before trusting it on a real guitar. The upper strings are harder because the period is only about 24 samples. Use the standard six-string tuning E A D G B E. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int MIC = 34;
const int N = 1024;
const int FS = 8000;                    // samples per second
const int MINLAG = 14, MAXLAG = 112;    // about 570 Hz down to 71 Hz
int16_t buf[N];
float ac[MAXLAG + 2];

const char* names[6] = {"E2", "A2", "D3", "G3", "B3", "E4"};
const float freqs[6] = {82.41, 110.00, 146.83, 196.00, 246.94, 329.63};

float detectPitch() {
  unsigned long next = micros();
  long sum = 0;
  for (int i = 0; i < N; i++) {
    while ((long)(micros() - next) < 0) {}
    next += 1000000UL / FS;
    buf[i] = analogRead(MIC);
    sum += buf[i];
  }
  int mean = sum / N;
  long level = 0;
  for (int i = 0; i < N; i++) { buf[i] -= mean; level += abs(buf[i]); }
  if (level / N < 30) return 0;         // too quiet

  float best = 0;
  for (int lag = MINLAG - 1; lag <= MAXLAG + 1; lag++) {
    long s = 0;
    for (int i = 0; i < N - lag; i++) s += (long)buf[i] * buf[i + lag];
    ac[lag] = (float)s;
    if (lag >= MINLAG && ac[lag] > best) best = ac[lag];
  }
  // First clear peak: smallest lag that is a local maximum above 90 percent of the best
  for (int lag = MINLAG; lag <= MAXLAG; lag++) {
    if (ac[lag] >= 0.9f * best && ac[lag] >= ac[lag - 1] && ac[lag] >= ac[lag + 1]) {
      float a = ac[lag - 1], b = ac[lag], c = ac[lag + 1];
      float shift = 0.5f * (a - c) / (a - 2 * b + c);   // parabolic interpolation
      return FS / (lag + shift);
    }
  }
  return 0;
}

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(WHITE);
  analogReadResolution(12);
}

void loop() {
  float f = detectPitch();
  oled.clearDisplay();
  if (f > 0) {
    int idx = 0; float bestDiff = 1e9;
    for (int i = 0; i < 6; i++) {
      float d = fabs(log(f / freqs[i]));
      if (d < bestDiff) { bestDiff = d; idx = i; }
    }
    float cents = 1200.0f * log(f / freqs[idx]) / log(2.0f);
    oled.setTextSize(3); oled.setCursor(0, 0); oled.print(names[idx]);
    oled.setTextSize(1); oled.setCursor(70, 4); oled.print(f, 1); oled.print(" Hz");
    oled.setCursor(70, 16); oled.print(cents, 0); oled.print(" cents");
    int x = 64 + constrain((int)cents, -50, 50);        // needle
    oled.drawRect(14, 40, 100, 14, WHITE);
    oled.drawLine(64, 36, 64, 58, WHITE);
    oled.fillRect(x - 2, 42, 4, 10, WHITE);
    Serial.println(f);
  } else {
    oled.setTextSize(1); oled.setCursor(0, 20); oled.print("Pluck a string");
  }
  oled.display();
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

90.97
108.39
185.53
145.16
296.65
295.37

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
110 Hz toneA2 with a few cents of error
115 Hz toneA2 about 80 cents sharp
SilencePluck a string message
Plucked guitar stringCorrect note name, needle moves as you tune

If it does not work

Always says pluck a string

Move the microphone closer or lower the quiet threshold.

Wrong octave

Pluck harder and check the first-peak search; the string may be drowned by a harmonic.

Jumpy readings

Wait for the note to settle and try the lower strings first.

OLED blank

Check the I2C address 0x3C and the SDA and SCL pins.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the guitar tuner?

ESP32 DevKit V1, MAX9814 microphone with AGC, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the guitar tuner take to build?

About 180 minutes for a intermediate maker, in 8 steps.

How do I know the guitar tuner works?

110 Hz tone: A2 with a few cents of error. 115 Hz tone: A2 about 80 cents sharp. Silence: Pluck a string message.

Always says pluck a string: what should I check?

Move the microphone closer or lower the quiet threshold.

Wrong octave: what should I check?

Pluck harder and check the first-peak search; the string may be drowned by a harmonic.

Jumpy readings: what should I check?

Wait for the note to settle and try the lower strings first.

OLED blank: what should I check?

Check the I2C address 0x3C and the SDA and SCL pins.