Frequency Detection - Basic Stamp II
I built a simple Op amp circuit to amp a signal (guitar) pass it thru a Schmitt trigger and input to the Basic Stamp. I have 2 programs, both used the PULSIN instruction. None give an accurate frequency measurement. Is the Basic Stamp II able to measure frequencies?
One of the programs is Jon Williams SHOWFREQ.BS2.
Thanks,
PRPROG
One of the programs is Jon Williams SHOWFREQ.BS2.
Thanks,
PRPROG
Comments
Here are some suggestions to help you on your way:
1) Use an oscilloscope if you have one. USB scopes are convenient and the lower end ones do a decent job. For projects with signals in the audible frequency range, low end scopes are more than enough. Still, do your research. You can also download free software to use your soundcard as the hardware interface for an oscilloscope. I have never built one and YOU HAVE TO BE VERY CAUTIOUS HOW YOU MAKE IT AND HOW MUCH VOLTAGE IS SUPPLIED BY THE SIGNALS YOU USE. A soundcard is quite limited in how much applied votlage it will take before it is damaged. So your research before building one. I recommend the USB or standalone oscilloscopes.
2) Get small sections of your program working before you attempt to get the whole thing working at once. Troubleshooting is much harder with 100 lines of code compared to 10 or 15.
3) Use DEBUG statements to help you determine what kinds of numbers you are getting from different parts of the program.
4) You can also write bits of code that will do things such as light up LEDs dependent of different conditions. Later, you can comment out the code or delete it when it is not needed.
It read pin 0
These frequencies are looked up from a project I am working on called the Luna Mod Looper Basic Stamp 2 Version which can be found in the Projects section and on Instructables. The point is that the BS2 uses integer math. Any calculations have the numbers to the right of the decimal point truncated. The numbers I used were those I used in the program. If you want fairly accurate frequencies, check here: http://www.phy.mtu.edu/~suits/notefreqs.html
Here is a relevant equation: f = 1 / T, where
f = frequency and T = period (in seconds or milliseconds, etc.)
Low E, E2, f = 82 Hz, T = 12.2 ms
C5, f = 523 Hz, T = 1.91 ms
These frequencies should fall under the maximum pulse width of 131.07 ms (and greater than minimum of 2 us?). Given that, how far off are your frequencies? Putting the equations from the program into a spreadsheet would be a good idea. Then enter in a range of values for the equations. In the next column use a round down function to truncate the values. Now you have what the true values from the equation and the truncated values. Compare the spreadsheet values to the ones obtained from BS2. Make sure to take note what the expected frequencies are for the notes you are playing.
The other thing that comes to mind about inaccuracies with such a method where your frequency source is a guitar is that the peaks might be throwing off your input from the Schmitt trigger. What are the threshold values, minimum and maximum for changing from one state to the next? The last time I measured my electric guitar signal it had a lot or irregular waveforms and if I recall correctly, that was even for single notes being played. You could tell where the general maximums and minimums were but it was far from a regular sine wave. An oscilloscope would really be an asset to you.
I re-start the basic idea but instead of PULSIN I change to COUNT . Since I am using a Schmitt trigger., COUNT seems to be more accurate.
The key line in the code is:
COUNT 0,100,F
F = F * 10
I get 440 when I play note A on my keyboard.
I notice that the COUNT give me a 440 count ..but then the numbers start to decrease gradually. I think that is a normal behaviour, so the question is how do I get a sample of the Maximum value, which instruction/Command give me the Maximum value. (at first I though MAX was the command but that will limit to a certain value).
Also how do I translate this formula to PBASIC:
MidiNote = (12 * Log2 (F/440)) + 69
I intend to calculate the corresponding MIDI note by getting the frequency (F). What is the equivalent of Log2 in PBASIC?
Thanks,
PRPROG
I have a series of articles on how to calculate log2 in PBASIC, here.
Here is a link to the Resources where you can find the Nuts and Volts section: http://www.parallax.com/Resources/ResourcesHome/tabid/473/Default.aspx
Thanks a lot. But I have to admit, that I read your article on to calculate log2 in PBASIC and have no idea ho to implement it to get this ► MidiNote = (12 * Log2 (F/440)) + 69
Can you help me to create such formula in PBASIC? I appreciate your help.
Thanks,PRPROG
The MIDI code is an integer, right, with A440 = 69, and 12 integer values per octave? Then to find the code for A880:
MIDI = 12 * log2(880/440) + 69
= 12 * 1 + 69
= 81
And by the same means A55 is three octaves down:
MIDI = 12 * log2(55/440) + 69
= 12 * log2(2-3) + 69
= 12 * -3 + 69
= 33
Frequency=55 Hz is pretty low, unless you have a pipe organ!
To find the log, first you will find the octave of your F, by steps of 12. Once having the octave, the last part to get the code within the octave will be a bit of calculation and rounding off. OK so far?