Shop OBEX P1 Docs P2 Docs Learn Events
How would I go about making a simple Guitar tuner using BS2? — Parallax Forums

How would I go about making a simple Guitar tuner using BS2?

K2KK2K Posts: 1
edited 2008-11-08 04:32 in BASIC Stamp
I am a·musician and I thought it would be fun, and educational,·to make a simple Guitar tuner using the BS2. Doesn't have to be anything speacial, but I do want it to be as accurate as possible. If anyone could give me any information on how to do this, it would be much appreciated. Thank You.
«1

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-10 13:18
    The hardest part will be conditioning the signal so that you can get a clean square wave into the BASIC Stamp. After that you could use PULSIN (twice) to measure the high and low portions of the signal and from there derive frequency (F = 1 / T).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Piper984Piper984 Posts: 74
    edited 2005-10-10 18:43
    Hi Jon,

    When you state 'conditioning the signal', can you describe what that means? Or rather, how would one go about conditioning the incoming audio signal?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-10 23:32
    What you want to do is amplify it up to 5 volts (peak) and square it off -- use a chip with a Schmitt trigger input for this. I have three guitars but rely on my trusty Korg tuner and haven't spent the time to build a Stamp-based version.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • edited 2005-10-11 00:19
    Sounds like a challenge!

    Guitar signal are full of harmonics and overtones.· Look for an amplifier that gracefully recovers from hitting the rails- lots of overdrive circuits contain op-amps that are good at this.· Typically they are duals, where you would probably want single ended.

    · The high E-string fundamental will create a "1 ms" pulse, and the BS2 has 2us resolution, so you should be in good shape there.· In fact, I'd recommend damping the fundamental and playing the 1st harmonic (lightly touching the string at the twelfth fret).· This is a dramatically cleaner signal than a plucked fundamental (fewer surviving overtones).

    ····· E6····· 82.41
    ····· A5···· 110.00
    ····· D4···· 146.83
    ····· G3···· 196.00
    ····· B2···· 246.94
    ····· E1···· 329.63
    ·
  • Piper984Piper984 Posts: 74
    edited 2005-10-11 17:19
    Jon: "...haven't spent the time to build a Stamp-based version." Sounds like a Nuts and Volts article idea, eh? [noparse]:)[/noparse]

    Chris: Thanks for the data. I'll see how it works out for me tonight.

    Regards!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-11 19:00
    No, not a good idea for Nuts & Volts.· Nice try though, but this is not the first to attempt to get me to do a design under the guise of, "Oh, that would be great for N&V." tongue.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • knightofoldcodeknightofoldcode Posts: 233
    edited 2005-10-12 06:58
    Jon Williams (Parallax) said...
    No, not a good idea for Nuts & Volts. Nice try though, but this is not the first to attempt to get me to do a design under the guise of, "Oh, that would be great for N&V." tongue.gif


    rotflmao!

    I thought it was a good guise! [noparse]:)[/noparse]

    Knight.
  • steve_bsteve_b Posts: 1,563
    edited 2005-10-12 12:52
    Although the 12th fret harmonic is cleaner, it doesn't always represent the tuning of the string.

    If your intonation is set properly then YES, the note on the open string and at the same strings 12th fret should be the same....however, going from different environments (temperatures and humidity) the wood bends and twists so that this isn't always the same.

    I've a new guitar that I had in a non A/C'd apartment and had real trouble with humidity....was readjusting it so much, that I accidentally tightened the truss rod too much and pressed in to the wood (shoulda loosened)...anyhow, the moral is, if you don't know how to do it...pay $20 and have the gee-tar restringed and adjusted by a shop!
    Chris Design Dude said...
    Sounds like a challenge!

    Guitar signal are full of harmonics and overtones.· Look for an amplifier that gracefully recovers from hitting the rails- lots of overdrive circuits contain op-amps that are good at this.· Typically they are duals, where you would probably want single ended.

    · The high E-string fundamental will create a "1 ms" pulse, and the BS2 has 2us resolution, so you should be in good shape there.· In fact, I'd recommend damping the fundamental and playing the 1st harmonic (lightly touching the string at the twelfth fret).· This is a dramatically cleaner signal than a plucked fundamental (fewer surviving overtones).

    ····· E6····· 82.41
    ····· A5···· 110.00
    ····· D4···· 146.83
    ····· G3···· 196.00
    ····· B2···· 246.94
    ····· E1···· 329.63
    ·
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 02:45
    Hi Jon et all,

    Just to see what would happen, I wired a microphone to an LM324 OpAmp, and then into pin 0 of a BS2 to see if I could get any data into the stamp via the count command.· But no luck.· Have you ever done this type of circuit before?· Any advice?· I understand this will not be an accurate pitch detection method, but I'd just like to get started with some type of feedback and work up from there.

    Also: I'd be interested to know if you've ever used an IC to do Freq. to voltage and interfaced this to a BS2?· I have an application where I'd like to monitor the pitch from 4 different sources.· I have coded PC based pitch detection, but I've never done DSP programming and I'm hoping to be able to use a BS2 for·this project.

    Thanks!
  • ccevtccevt Posts: 10
    edited 2005-11-09 03:43
    K2K and the assembled masses,

    I am no musician and don't know anything about the normal guitar tuning procedure, but may I suggest a different approach to the problem based on a few things I do know something about.

    A music teacher uses a "pitch pipe" to establish a singers pitch. A piano tuner uses a tuning fork as a frequency standard and tunes for an audible beat note between the key (really a string) and the tuning fork. A HAM radio operator uses the same idea of "zero beating" one signal against another to precisely match the frequency. In that case, one can often visually "zero beat" by watching the S-meter make slow undulations until the needle holds still when the frequencies match. Anyway, it would seem one could use the "freqout" command on a BS2 or BS2E to generate the desired reference frequencies (one at a time) and using the 2 stage RC low pass filter (shown in the freqout help description) feeding either a speaker, or preferrably, an audio amplifier with speaker, a audible beat could be heard as the string was tuned. In other words, the stamp generates an audible reference frequency which the human ear compares with the sound of the simultaneously plucked string. The only problem I see with this is the minimum frequency increment appears to be 1 Hz steps and some of the earlier exampes indicate frequencies in between which would mean errors of up to .5 Hz on some notes, and that does not include error due to time base accuracy. Of course, who knows what the spec'd accuracy of that 25 year old tuning fork is that has been kicking around for most of a career?

    Back to my cave,

    Horace
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 16:12
    Hello Horace,

    If I understood you right: Sure, you could use a BS2 to produce audio tones at a certain Hertz and then tune by ear.· Actually, you could just use a 555 timer (no BS2 needed) for that approach.·

    However, what I'm really interested is pitch detection methods... that's my interest in this topic.· The most sophisticated pitch detection we all have access to is our ears, but what could be done with a Basic Stamp? Any thoughts?

    ·
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2005-11-09 16:19

    Horace,

    I think you are on the right track. If I were constructing this circuit, I would probably create 6 independent band-pass filters for the 6 notes indicated.
    This is to reject any unwanted frequencies that we are not focusing on. Each filter would then have an active region of about +/-10% or so (however
    accurate you want your instrument). From here, the output of the band-pass filters would be relatively easy to condition (convert to a square wave)
    for the Stamp to interpret. You could use either an RC filter or LC filter to accomplish your band-pass, although unless you are into winding your own
    coils, the RC approach might work out better for you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-09 16:21
    The process is pretty simple -- once you have a signal connected to the BS2 that it can see. Since we know that Frequency = 1 / Period, you can use PULSIN to measure the high and low poritons on the incoming wave, then the frequency of the wave can be determined.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 16:27
    So Jon, using a 324 opamp to condition the signal from a microphone... Should I have the output of one opamp section drive a second section of the opamp IC, and then into the BS2? What's the best way to know if the signal is squared off 'enough'?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-09 16:34
    Sorry, I'm not analog guy by any stretch -- perhaps my colleague Beau will chime in; he's got a great library of useful circuits in his head.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BeanBean Posts: 8,129
    edited 2005-11-09 17:15
    To "Square off" a signal you usually use an op-amp with very high gain. When the op-amp ampilifies the signal it will "hit the rail" of the power supply. So you end up with the square wave that goes from 0 to 5 volts.

    The problem is that a guitar string has harmonics, so the E6 string has mostly 82.41 Hz, but also has some higher frequencies mixed in. Using harmonics (lightly touching the string at a certain place when it is plucked) will give you a purer signal (less other frequencies) that's why they sound like a "bell".

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Those that would give up freedom for security will have neither.
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2005-11-09 21:06
    Ok, so here is the "skinney"....

    Bean is right, to square off the signal you just amplify the signal to the point where
    you have "clipping" of both the top and bottom halves of the wave.

    Now, as far as picking off the frequency range so you don't have a lot of garbage when
    you amplify the signal, you need either a LC filter (better) or an RC filter (good).

    Think of the LC filter like tuning in a radio channel, only instead of MHz or kHz we are dealing
    with a few hertz (Hz). The goal is to create a LC circuit that resonates at the frequency we
    are looking for.

    Here is a good reference on-line calculator:
    www.deephaven.co.uk/lc.html

    Here is a table based on the values for E6 to E1
    
              Hz      C      L
    E6     82.41    2200uF    1695uH
    A5    110.00    2200uF     951uH
    D4    146.00    2200uF     540uH
    G3    196.00    2200uF     300uH
    B2    246.94    1000uF     415uH
    E1    329.63    1000uF     233uH
    
    Input >---o----[noparse][[/noparse]Coil]----o---> Output
              | +          + |
            [noparse][[/noparse]Cap]          [noparse][[/noparse]Cap]
              |              |  
      GND >---o--------------o---> GND
    
    
    



    What? afraid of coils? Here is an RC approach....

    Good reference material.... Note (Fig 1 is all you need)
    www.qrp.pops.net/filter1.htm

    Here with an RC approach, the goal is to create a "notch"
    filter by combining a High Pass filter with a Low Pass filter.


    Here is a table based on the values for E6 to E1
    
              Hz      C        R        Frequency Range
    E6     82.41    .1uF    19.313K     67-110
    A5    110.00    .1uF    14.468K     90-150
    D4    146.00    .1uF    10.901K        124-178 
    G3    196.00    .1uF     8.120K        171-245
    B2    246.94    .1uF     6.470K        178-324
    E1    329.63    .1uF     4.837K        276-398
    
    
          High Pass Filter       Low Pass Filter
    
    Input >---o----[noparse][[/noparse]Cap]-->......>---o----[noparse][[/noparse]Res]--> Output
              |                      |
            [noparse][[/noparse]Res]                  [noparse][[/noparse]Cap]
              |                      |
      GND >---o----------->......>---o-----------> GND
    
    




    The BIG difference here is that with a LC filter we are really
    not filtering, we are "tuning" and when we hit on the resonate
    frequency of the LC circuit, the output can increase dramatically!!!
    Where as with an RC filter the signal level is attenuated by the
    input frequency, and is never as "sharp" of a response than a LC
    filter.

    For example with note A5.  A frequency of 110 Hz with 100mV input
    will cause the output to peak at 175V !!!
    
    The table below shows the output by varying the frequency and keeping
    the input voltage at a constant 100mV.  You will see that the output
    signal is very sharp when 110 Hz is present.
    
    114Hz --    1.25V
    113Hz --    2.00V
    112Hz --    2.75V
    111Hz --    5.50V
    110Hz -- 175.00V
    109Hz --    5.50V
    108Hz --    2.75V
    107Hz --    2.00V
    106Hz --    1.25V
    
    
    



    If you really want to play with coils, here is an Excel file that I put together.
    (Note: change the extension from .TXT to .XLS)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 11/9/2005 9:14:23 PM GMT
  • steve_bsteve_b Posts: 1,563
    edited 2005-11-09 21:07
    You also get something called intonations....honestly don't know the name of it...but it's a way to know if the bridge saddle to the head nut corresponds with the middle being at the 12th fret. Your string can be out of tune but the tone (harmonic/intonation) doesn't always reflect that.

    At least that's the case on my tuner. I can be a little off on an open string and yet when I hit the intonation at the 12th fret my tuner will show that I'm dead on....of course, does that mean my intonation is off? lol maybe!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 21:48
    Beau,

    Thanks for the data. Great stuff!! I'd like to try this out... What would the circuit look like to interface a mic element to P0 of my BS2? Bean (above) said to use an opamp to square off the signal. I understand the filtering, but a little stuck with the mic->BS2 circuit. I am currently not seeing data coming in from the mic element (using LM324 opamp) and I am hoping one of you kind BS2 gurus would post a description of the circuit (exluding the filtering, as that can be applied later) to get the BS2 to read the mic input via the pulsin or count commands.

    Regards!!
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2005-11-09 22:00
    What kind of microphone are you using?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • BeanBean Posts: 8,129
    edited 2005-11-09 22:25
    Intonation problem are commonly caused by the action (string to fret distance) being too high. When you push the string down to the fret, you are really stretching it slightly giving a higher pitch.

    Since I usually play open chords I set my intonation so that it is "right-on" when the string is held at the third fret.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Those that would give up freedom for security will have neither.
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2005-11-09 22:30
    Just an FYI:

    Looking through my November 2005 issue of "Nuts and Volts" on page 56 there is an article
    going over "...analog filters built with discrete components..."

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 22:48
    Beau: Just a cheap little mic like the kind that comes with a computer/sound card.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-11-09 22:58
    Piper, you are likely missing the bias circuitry for the microphone.

    Heres a page that discusses it: http://www.epanorama.net/circuits/microphone_powering.html

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Piper984Piper984 Posts: 74
    edited 2005-11-09 23:30
    Ok Paul, thanks for the link. That help! I get data returned now via pulsin and count commands. Now back to what Jon was talking about: F=1/T. So far, the numbers coming back don't seem to jive with the sine wave tones I'm playing. Maybe the LM324 isn't enough preamp to square the signal? I'll see if using multiple stages helps. The max output of the opamp in my circuit is 3.5V, as I'm powering it w/ the 5V from my BOE.

    Regards!!
  • steve_bsteve_b Posts: 1,563
    edited 2005-11-10 02:05
    I was always taught to set intonation at 12th fret harmonized.

    But ya, the action on my electric is way different than on my acoustic....high/low action. I've also got a bit of a twist on my electrics neck, so the bottom isn't the same as the top!


    Bean (Hitt Consulting) said...
    Intonation problem are commonly caused by the action (string to fret distance) being too high. When you push the string down to
    the fret, you are really stretching it slightly giving a higher pitch.

    Since I usually play open chords I set my intonation so that it is "right-on" when the string is held at the third fret.

    Bean.
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."
  • Piper984Piper984 Posts: 74
    edited 2005-11-10 13:58
    Hi Jon:

    After rereading you posts, you mention to call pulsin twice. I am not clear what you meant by this. Last night I mucked about with using pulsin to detect the frequency of a few sine waves, and although I got data back that seemed to correlate with the pitch of the sine wave, I didn't see Freq=1/Time and perhaps I am not using pulsin as you prescribed? Or perhaps my signal is not 'conditioned' as it should be?

    Thanks for any direction you can give.
  • Piper984Piper984 Posts: 74
    edited 2006-02-27 01:29
    Hello Stampers,

    I am getting back to this project, and am stuck on the OpAmp -> BS2 interface circuit.· From what Jon posted earlier, I think that if do something like:

    PULSIN 0,1,var1 
    PULSIN 0,0,var2 
    


    I can use var1+var2 to determine the frequency of tones being picked up from a mic.· I have tried a few circuits to interface the mic to an opamp, and then to a pin on a BS2.· But I have not had very good luck doing this at all.·(I get either noise or 0's). ·Has anyone else actually done this type of circuit before?· Would you be willing to post a schematic?· A BS2 should be able to 'hear' 440Hz pulses?

    See attached snips of a circuit I have been trying this weekend.· The OmpAmp is OPA2340PA. I am not sure of the source of the original (googled), or I'd post the source site/author.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-02-27 04:33
    Piper,

    The OPA2340 is a good choice, rail to rail i/o and plenty of bandwidth. I don't like the circuit though. for the "gain block" in particular. The rest of it looks okay. I would suggest tying the top of R5 to VG (virtual ground) instead of Vcc. Also leave out the capacitor C6 and tie the left end of R6 directly to VG. Actually, you can leave C6. The most important change is R5 to VG.

    It should work okay so long as the fundemental amplitude is greater than the summed amplitude of the harmonics and other ambient noise.

    If you have access to an oscilloscope, that can show yoou so much of what is going on.

    Another fun kind of circuit for this would be a PLL tone decoder. It is in the class of tuned or filter circuits that Beau mentioned, that would reject the harmonics and ambient noise. Devices like the LM567 are no harder to use than an op amp. You would feed your audio in, and the LO (local oscillator) output is a clean square wave that tracks the input frequency over a +/- 10% range. That you would feed into the Stamp for COUNT or PULSIN. The tone decoder also gives you a flag to show you that the tone is strong enough for a lock. There are tricks you can play with the stamp pins to set the PLL for different tones, by switching in different LO capacitors.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Piper984Piper984 Posts: 74
    edited 2006-02-27 14:33
    Thanks Tracy! I'll make the changes you recommend and see what happens. I'll also get a few LM567 on my next Digikey order.
Sign In or Register to comment.