Skip to content

Sensors & Signal Processing

Build an Arduino guitar tuner: smooth, gate, time 20 waves

An Arduino guitar tuner listens to a string through a microphone, works out its frequency, and says whether it's flat, in tune or sharp. The hard part isn't the counting but the clean-up: remove the microphone's middle, smooth away the overtones, gate out the hum between plucks, and time 20 waves instead of one. For the A string the target is 110 Hz, and within 1 Hz counts as in tune.

The overtones problem

A plucked string rings at its note and at whole-number multiples of it, the overtones: 220 Hz, 330 Hz and up. They add extra crossings, so counting raw crossings of the A string reads far too high, 150 Hz or more. A low-pass with alpha 0.1 at 4000 samples a second, a corner near 67 Hz, shrinks the overtones more than the note and leaves one clean crossing per wave.

The chain

  1. The middle: mid follows the reading by 0.01, and x = reading - mid. A microphone's middle is never exactly 512.
  2. Calm the overtones: smooth follows x by 0.1.
  3. Loudness: size = fabs(smooth), with a fast attack and a × 0.999 release, into env.
  4. Only while it rings: while env is over 60, count rising crossings of smooth past +10, re-armed below −10. Note the sample number n at the first; at the 21st, hz = 20 * 4000.0 / (n - first).

Why 20 waves

At 4000 samples a second, one 110 Hz wave lasts about 36 samples. Timing is always about one sample out, so one wave is only good to about 3 Hz, wider than the whole in-tune window. Twenty waves take about 727 samples, and the same one-sample error is now about 0.15 Hz.

hz = 20 × 4000 ÷ 727 ≈ 110.0 Hz

Live experiment

Test the tuner on three plucks

Three plucks of the A string, flat at 104 Hz, in tune at 110 and sharp at 116.5, with mains hum between them. Count raw crossings, time one wave, leave out the gate, or run the whole chain, and see which passes all four checks.

Try it yourself: free lesson

“Project: Guitar tuner” is the project that ends Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

The gate

Between plucks the microphone hears only the 50 Hz mains hum. Smoothed, it reads about 16, so a gate at 60 keeps the tuner from timing it and reporting the hum as a note.

Reading it out

Within 1 Hz of 110, show IN TUNE; below, FLAT; above, SHARP. The test plucks at 104 and 116.5 Hz are each about a semitone out. For the other strings, change the target: E 82.4, D 146.8, G 196, B 246.9 and high E 329.6 Hz.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Project: Guitar tuner” is the project that ends Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3Why time 20 waves and not one?
    Show the answer

    Answer: One wave is only 36 samples, so being one sample out is 3 Hz out; over 20 waves it's 0.15 Hz. The error is one sample whatever you time, so time a long stretch and the error shrinks in proportion.

    • Why not “The overtones need it”? The smoothing filter deals with the overtones. Timing more waves deals with the sample error.
    • Why not “One wave is too quiet”? Loudness doesn't change how precisely a wave can be timed. The one-sample error does.
    • Why not “It uses less memory”? Timing 20 waves needs the same few variables as timing one. It's about precision, not memory.
  2. Question 2 of 3reading − slow is…
    Show the answer

    Answer: A high-pass filter: the fast part. slow is the low-pass; taking it away leaves the fast part.

    • Why not “The slow part”? slow itself is the slow part. reading − slow is everything slow leaves out.
    • Why not “Always 0”? It's 0 only when the reading isn't changing. Any quick change shows up in it.
    • Why not “A low-pass filter”? slow is the low-pass. Taking it away from the reading leaves the opposite: the fast part.
  3. Question 3 of 3A transmitter's third harmonic sits at…
    Show the answer

    Answer: Three times its frequency. Harmonics are whole-number multiples.

    • Why not “The same frequency”? The fundamental is the first harmonic. The third is three times higher.
    • Why not “Nine times”? Nine times would be the ninth harmonic. The third is three times the frequency.
    • Why not “A third of its frequency”? Harmonics are whole-number multiples of the fundamental, so they sit above it, never below.
Keep practising: free lesson

“Project: Guitar tuner” is the project that ends Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.

Questions people ask

What frequency is each guitar string?

In standard tuning, from the thickest: E 82.4 Hz, A 110 Hz, D 146.8 Hz, G 196 Hz, B 246.9 Hz and the high E 329.6 Hz. Each is the note's fundamental; its overtones sit at whole-number multiples of it.

How accurate does a guitar tuner need to be?

Within a few cents, hundredths of a semitone. At 110 Hz one cent is about 0.06 Hz, so a 1 Hz window is roughly ±16 cents: fine for a first tuner, and timing more waves tightens it.

What sample rate does an Arduino guitar tuner need?

Several times the highest note it measures. 4000 samples a second covers a guitar's open strings, up to 330 Hz, with room for the smoothing filter. analogRead on an Uno can manage roughly 9000 samples a second.

Every topic