Shop OBEX P1 Docs P2 Docs Learn Events
Measuring Ambient sound Levels — Parallax Forums

Measuring Ambient sound Levels

NoBo780NoBo780 Posts: 94
edited 2008-12-01 04:42 in BASIC Stamp
Is it possible to use a microphone with a R-C circuit and an RCTIME command to measure ambient sound levels easily with a BS2? I am thinking about using this in a robot.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
«1

Comments

  • JonathanJonathan Posts: 1,023
    edited 2007-10-23 16:16
    NoBo.

    I am working on a project that monitors classroom noise levels. You are going to need to amplify the sound.

    Part of it depends on what volume levels you want to measure. Take a look at op amps. A couple to llok at would be LM386, single supply and LM741, dual supply. Both of those can be had at RatShack.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-23 16:33
    Is that all I'd have to use? Just hook up a small mic to the op-amp and use the RCTIME command with the BS2? Or will I have to buy a bunch of capacitors and resistors to filter the output and build a circuit for it (other than the simple R-C)? Just for fun, I originally wanted to build a robot that serves drinks/snacks, and I got pretty close, until I realized that I would need an ARRAY of expensive sensors to make it even remotely practical cry.gif . So now I am building a simple, very versatile, interactive robot just for entertainment.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • JonathanJonathan Posts: 1,023
    edited 2007-10-23 17:00
    I haven't tried it using RCTIME, I use an ADC. I think the question is how loud a sound you want to detect, and with what granularity. If you just want to detect a handclap, easy. If you want to catch a murmur across the room, probably not.

    For me, I am intersted in the range whispering to ordinary speech volume. To do this effectivly, I used an electret mic run through 2X 741's, fed to a 12 bit ADC.

    Try an electret mike, bias it with a 10k and run it into a LM386. See if that works for ya.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-23 17:50
    Why did you have to use an ADC?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • JonathanJonathan Posts: 1,023
    edited 2007-10-23 18:20
    Because of the resolution I need. I want to distiguish between 2-3 students whispering and 5-6 students whispering. My project is a sound monitor hooked up to a traffic light. Green is sound level OK, yellow is warning and red is busted. In order to be able to reliably distinguish these condistions, I need the resolution of the ADC.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • D FaustD Faust Posts: 608
    edited 2007-10-23 18:30
    Look at this: http://forums.parallax.com/showthread.php?p=682063

    I haven't tried it yet, but I can post how it works when I get around to it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-23 19:10
    So, this ( http://img322.imageshack.us/img322/6720/stampaudioab4hs.png ) circuit could work, right (minus one side of it)? But I have never used an A/D converter before; could the output of the ADC in that circuit just be read as a decimal value? How do you interface the ADC? And could I use an LM386?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-23 20:10
    The circuit you reference is often used and you can leave out one half.

    The ADC produces an 8-bit binary value (0-255). You can easily convert it to decimal for display purposes. Internal to your program, it's just a number.

    The "Basic Analog and Digital" tutorial in the Stamps in Class series has plenty of information on these ADC including sample programs. Download a copy from the Parallax website.

    The LM386 is a medium power amplifier used to drive headphones or small speakers. It's not really suitable for this type of low level audio circuit. The LM741 is really a cheap, basic, buy anywhere type of op amp that will work well in this circuit.
  • JonathanJonathan Posts: 1,023
    edited 2007-10-23 21:30
    NoBo,

    All good questions... And I have to mention here that I have no formal training in electronics at all, so take what I say with a grain of salt. However, I did just make a very similar circuit and it is working so...

    OK, a couple of things to remember in a circuit like this. The 741 in the picture you linked is being used as a single supply amplifier. This means that with no input, the output voltage will be 2.5 volts. The maximum voltage out will be around 4V. So, you have around 1.5V of swing.

    The ADC shown is 8 bit. That means it will make a number between 0 and 255. It will read around 127 with no input. It will read about 204 at max voltage. So, you will have about 77 ADC counts in your full range.

    Actually, that isn't quite true. It will be a little less because of the voltage drop across the diode. Use a Schottkey type diode to minimize the voltage drop to about .7V.

    The level of amplification is set by the resistor between the output and the +input of the 741.

    If this is good enough resolution for your needs, great. If not, there are ways to increase the resolution. You can get an ADC with more bits. You can get an ADC that has + and - Vref inputs so you can narrow the range. You could opt for a dual power supply (-5V/gnd/+5V) and get a greater swing from the op-amp. You can cascade the output from one op-amp into another and amplify it again. A lot depends on what you want to do. Tell us more about what you want it to do.

    Jonathan



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-23 22:58
    Take in mind that, I do NOT have a comprehensive knowledge of PBASIC code, but I think I may have enough to figure out how to work with a ADC. I originally had the idea to incorporate it into my [noparse][[/noparse]simple] robot for basic measurement of ambient sound (and I stress BASIC because all it needs to know is the difference between a room with a few people, and a room filled with party animals/ relatively quiet compared to sudden, loud noise [noparse][[/noparse]maybe I'll get to voice-clap-coordinated commands...]), but I would LOVE to be able to make it have the kind of resolution as your project, but if it took you hours of work to figure out the principles of the code for that, I'm not going to venture there. Can that (your idea) be done with just intermediate knowledge? I mean, this is my first TRULY home-made robot.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • JonathanJonathan Posts: 1,023
    edited 2007-10-23 23:56
    NoBo,

    Sorry if I got it all complicated. It's not as bad as I made it sound. The code part is easy, don't worry.

    OK, lets talk just distinguishing noisy from quiet. Instead of using an ADC, another way is to use two 741's. One is used as an amplifier as we have discussed. The other is used as a comparator. There are many tutorials on comparators out there, so I'll just say that the comparator allows you to set a voltage (sound) level at which the comparator will either turn on or off, as you configure it. You can monitor the output of a comparator with a Stamp pin. In this setup, you set the volume threshold with a pot.

    Second way is to use the 741 as an amplifier, and then use RCTIME to monitor the voltage. This will allow you to set the threshold in code instead of physically with a pot.

    Trying these two ways first will allow you to see if a simple setup will do what you want, and cost less than 10-20 bucks, all parts available at ratshack. If it isn't adequate, all the parts you bought will still be used trying a more complex system.

    Go to ratshack or similar and get:

    2 X LM741 Maybe get 3 in case you burn one.
    A pack of 1/4 watt resistors of varying values
    A pack of misc. electrolytic caps
    small 10k, 50k, 100k pots
    couple of LEDs

    If you have a breadboard, wires etc, that should get you going. Mess about with amplifier and comparator circuits.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-24 01:33
    Thanks!! This'll keep me busy...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • NoBo780NoBo780 Posts: 94
    edited 2007-10-25 00:15
    Ok. I have attached an image to this post. Is the highlighted area the part to use? And the part with the arrow pointing to it; that can go right to a (larger) cap, and then to the BS2? Or will C2 be used for the RCTIME measurement? And can I connect an LED where D2 is (ADD it; not replace the existing diode)? This is seeming harder and harder to do without an ADC....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • JonathanJonathan Posts: 1,023
    edited 2007-10-25 01:16
    Nobo,

    Yup, the highlighted are is what to use. You can put an LED where you indicate, but it will drop the voltage. Try sending the signal directly to your RCTIME circuit.

    If you want an LED to indicate volume level, you can run the ouput into a comparatot circuit. You can do botyh at the same tim, meaning the output of the op-amp goes to both the RCTIME circuit and the comparator. If you really want the LED to indicate volume, not just when it hits a threshold, look at using a 741 as a current follower. This will allow you to use the LED and not effect the output of the op-amp.

    HTH,

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • KatyBriKatyBri Posts: 171
    edited 2007-10-25 02:04
    NoBo,

    Could you post your STAMP code you used with the ADC you mentioned in the thread?


    ·
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-11 04:12
    The circuit is not working!! Here's the code I'm using: [noparse][[/noparse]code]

    '{$STAMP BS2e}
    ' {$PBASIC 2.5}

    time VAR Word

    DO

    'HIGH 9
    PAUSE 3
    RCTIME 9, 3, time
    DEBUG HOME, DEC time

    LOOP

    and I'm using the area circled in red from that previous post. What am I doing wrong?? I don't have capacitors as low as the values on the schematic, so I am substituting the .047uf for different values (.1uf, .01uf, 3uf) and NONE of them work. The debug window just says "1", and the status light (the red one that blinks when the BS2e is sending a message to the debug terminal) flashes EXTREMELY fast.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-11-11 05:52
    NoBo,
    The RCTIME command does not function in the way that you are trying to use it.
    Syntax: [b]RCTIME[/b] [i]Pin, State, Variable[/i]
    Function
    Measure time while [i]Pin[/i] remains in [i]State[/i]; usually to measure the charge/discharge time of resistor/capacitor (RC) circuit.. [list][*][b][i]Pin[/i][/b] is a variable/constant/expression (0 - 15) that specifies the I/O pin to use. This pin will be placed into input mode. [*][b][i]State[/i][/b] is a variable/constant/expression (0 - 1) that specifies the desired state to measure. Once [i]Pin[/i] is not in [i]State[/i], the command ends and stores the result in [i]Variable[/i]. [*][b][i]Variable[/i][/b] is a variable (usually a word) in which the time measurement will be stored. The unit of time for [i]Variable[/i] is described below. [/list]
    
    

    By using an additional pin connected to the POWER supply of your MIC and 741, you MIGHT be able to use configuration "A" ... (untested)

    DO
    
    HIGH [b][i]POWER_PIN[/i][/b]          'Power up 741 and MIC take a sample (charge cap)  Note: this will only work if Cap get's charged above 1.4V
    PAUSE 5
    LOW [b][i]POWER_PIN[/i][/b]           'Power down 741 and MIC 
    [b][i][/i][/b]PAUSE 5
    RCTIME [i][b]RC_PIN[/b][/i], 1, time  'Time how long it takes for the Cap to discharge 
    DEBUG HOME, DEC time
    
    LOOP
    
    


    attachment.php?attachmentid=50377


    Your best results will come from using an external ADC as your·schematic indicates.


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

    IC Layout Engineer
    Parallax, Inc.
    473 x 267 - 16K
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-11 15:45
    I still can't get it to work w/o the ADC. I'll just get one from Parallax. Thanks anyways!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • D FaustD Faust Posts: 608
    edited 2007-11-11 15:50
    I have a microphone w/ two pins would it work with the same schematic?

    Here is the product page for it: http://www.projectsunlimited.com/audioproducts/movieclips/products/audioDetailPrint.asp?CATEGORY=4&PARTNUMBER=PUM-5050P-2-R&Guid={7210F803-61A1-45A8-AA08-1ADB762CD219}

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-11 21:33
    NoBo780,

    That circuit that you posted, the part circled in red, has no means to reset the voltage on the capacitor. Once a sound occurs, the capacitor will hold the voltage "forever" until a louder sound comes along, which will only increase the voltage on the capacitor. You could add a resistor in parallel with the capacitor, so that it self-discharges. Several megaohms. That will be important if you use an ADC. The circuit might have worked as it is, but that would be just an accident of having enough leakage. Another thing about that circuit. The diode is not inside the feedback loop, so the circuit will not respond to low level signals. If the diode is moved inside the feedback loop, the output could follow the sound down to low levels. (Google peak detector) Increase the circuit gain if necessary. The gain as it stands is only x2, and that does not seem like enough to me for an electret, unless the electret itself has a preamp with voltage gain.

    That circuit responds to the instantaneous peak sound level. It is possible to make circuits like this that respond to the average (integrated) sound level. What type of sound are you trying to detect?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-12 04:03
    I am trying to detect handclaps, and whether a room has a few people/a party of people in it. I may know a bit about electronic engineering, so to speak, but judging by your post shocked.gif, you may have to take it down a notch for me (terminology wise). I am not going to attempt this anymore until I get an A/D converter, because that'll make a WORLD of difference! The problem is...I am running out of free pins (and I'm already COMPLETELY out of variable space, which is like gold to me when I want to add some new features and programming) cry.gif. I can't omit ANY of the variables in my current program; they are all important. Most of them are for the movement; its movement is completely randomized. What about the capacitor values? Can I substitute? I don't have ANY electrolytic caps that are that value, and I definitely don't have any that are .47, .047, or of that value. I only have like 3uf's, 10uf's, 47, 470, etc. "The diode is not inside the feedback loop, so the circuit will not respond to low level signals. If the diode is moved inside the feedback loop, the output could follow the sound down to low levels." --> Could you elaborate on that a little? I think I get the jist of what you're saying...And how do I increase the gain?? Thanks!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-11-12 06:38
    NoBo,
    ·
    I have attached a derivative of your original schematic... (TESTED)
    ·
    You said...
    What about the capacitor values? Can I substitute? I don't have ANY electrolytic caps that are that value, and I definitely don't have any that are .47, .047, or of that value. I only have like 3uf's, 10uf's, 47, 470, etc. "The diode is not inside the feedback loop, so the circuit will not respond to low level signals. If the diode is moved inside the feedback loop, the output could follow the sound down to low levels." --> Could you elaborate on that a little? I think I get the jist of what you're saying...And how do I increase the gain?? Thanks!!
    You can substitute, try experimenting and see what values you come up with (smaller values will give you better resolution though)... and C2 and C3 are not electrolytic ... regular ceramic disc capacitors should work fine.
    ·
    The circuit that I have included uses the diode(s) in a way that will be·sensitive to low level signals.
    ·
    In your original Circuit, Gain can be calculate by dividing R4 by R9... 200k / 100k = 2
    ·
    In the circuit that I provided, Gain can be calculated by dividing R4 by the sum of R9 and R10.· Since R10 is a 100K Potentiometer, and R4 is 1 Meg, you can adjust the Gain from x10 to x1000.
    If you need more gain than that, you can replace R4 with a 10Meg resistor... Gain = x100 to x10,000
    ·
    Notice that I have added a SQUELCH adjustment... this is actually a Voltage reference that ideally should be at 2V, but because of differences in the diodes, output of the Op-Amp, and I/O pin supplying Power(not right at 5V),·adjustments are required·so that the normal noise floor from the microphone is·at the threshold level of the I/O which is about 1.4Volts.· This works out so that a 2Volt reference minus a Diode voltage drop(0.6V) places you right at about 1.4Volts·....· In other words, when the RCSOUND.BS2 program is running, you adjust the SQUELCH until you "just" see where the value fluctuates·between a "1" and·some low value·returned number.
    ·
    ·
    The·operation of the design is as follows....
    ·
    1) Make the·POWER_PIN HIGH providing power·to the Microphone, Op-Amp, and Vref portion of the circuit.
    ··· Note:
    ··········· While the Power Pin is·HIGH D3 blocks the ability for C2 to discharge
    ············During·the period that you make the pin HIGH, the circuit acts like a·Peak Detector and stores·the maximum voltage value in C2 for that period.· (wait at least 1mS for the sample duration).
    ··········· Subsequent periods will reset the "Peak Detector".
    ·
    2) After a determined·"sample" duration, make the POWER_PIN LOW and use·the RCTIME command to·detect the amount of time it takes C2 to discharge.
    ··· Note:
    ··········· Since the POWER_PIN is now·LOW,·C2 is now allowed to·discharge through D3 and R11.· Experiment with different values for R11 and C2 for different RC time constants.
    ··········· The returned value here is proportional to the detected volume level.
    ·

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

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 11/12/2007 4:47:08 PM GMT
    1281 x 659 - 227K
  • JonathanJonathan Posts: 1,023
    edited 2007-11-12 16:31
    If it is really as simple as detecting a handclap or louder, and you really only care if the sound goes above this threshold, try the idea we discussed above, using on op-amp to amplify the voltage and another as a comparator. When the sound gets above the threshold it will go high or low, depending on how you configgered it. THen all you have to do is check the state of a single pin.

    If you really want to know the volume level, keep pursuing the ADC and RCTIME circuits. You'll figger it out. Any chance of getting access to a 'scope? Parallax sells a little USB scope, and there are many other out there. Or perhaps a school might let you use one. Thenyou could see exactly what is going on with your circuit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-13 08:28
    The attached circuit diagram shows what I mean by putting the diode inside the loop. Feeback to the (-) input of the op-amp comes from the right hand side of the diode at the capacitor, not on the left side. That way, the voltage on the capacitor comes up quickly to equal the voltage at the input, not one diode drop less. Then it holds at that level until a higher peak comes along or until it is shorted to ground by a reset pulse. A Stamp pin can do a reset with (LOW pin : INPUT pin). A large value resistor could be put in parallel with the capacitor so that it would self discharge and not take up another Stamp pin.

    attachment.php?attachmentid=50411

    The peak detector is the second half of the circuit. The first section of the circuit provides the voltage gain. I don't know how much voltage your electret mic puts out, but I tested a little Knowles mic capsule I have here, and it put out around 100 millivolts peak when I clapped right next to it. The peak died out rapidly to a longer lower level, all together about two milliseconds. So a gain of 10 or 20 would be fine for that, but more gain would be necessary for the more distant sound. This really might be better with a nonlinear amplifier (compressor).

    Dual op-amps like the LM358 are more modern than the '741. The LM358 has both op-amps in one 8 pin package and it operates off a single supply of 0 and 5 volts. I think Parallax stocks the LM358. It is a "jellybean" and is available even at the Rat Shack.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
    444 x 173 - 8K
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-14 00:42
    Beau Schwabe (Parallax) said...
    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">NoBo, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com[noparse]:office:[/noparse]office" /><otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">I have attached a derivative of your original schematic... (TESTED)<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>
    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">

    You said...

    What about the capacitor values? Can I substitute? I don't have ANY electrolytic caps that are that value, and I definitely don't have any that are .47, .047, or of that value. I only have like 3uf's, 10uf's, 47, 470, etc. "The diode is not inside the feedback loop, so the circuit will not respond to low level signals. If the diode is moved inside the feedback loop, the output could follow the sound down to low levels." --> Could you elaborate on that a little? I think I get the jist of what you're saying...And how do I increase the gain?? Thanks!!<SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">
    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">
    You can substitute, try experimenting and see what values you come up with (smaller values will give you better resolution though)... and C2 and C3 are not electrolytic ... regular ceramic disc capacitors should work fine.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">The circuit that I have included uses the diode(s) in a way that will be sensitive to low level signals.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">In your original Circuit, Gain can be calculate by dividing R4 by R9... 200k / 100k = 2<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">In the circuit that I provided, Gain can be calculated by dividing R4 by the sum of R9 and R10. Since R10 is a 100K Potentiometer, and R4 is 1 Meg, you can adjust the Gain from x10 to x1000.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">If you need more gain than that, you can replace R4 with a 10Meg resistor... Gain = x100 to x10,000<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">Notice that I have added a SQUELCH adjustment... this is actually a Voltage reference that ideally should be at 2V, but because of differences in the diodes, output of the Op-Amp, and I/O pin supplying Power(not right at 5V), adjustments are required so that the normal noise floor from the microphone is at the threshold level of the I/O which is about 1.4Volts. This works out so that a 2Volt reference minus a Diode voltage drop(0.6V) places you right at about 1.4Volts .... In other words, when the RCSOUND.BS2 program is running, you adjust the SQUELCH until you "just" see where the value fluctuates between a "1" and some low value returned number.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">The operation of the design is as follows....<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">1) Make the POWER_PIN HIGH providing power to the Microphone, Op-Amp, and Vref portion of the circuit.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> Note: <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> While the Power Pin is HIGH D3 blocks the ability for C2 to discharge<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> During the period that you make the pin HIGH, the circuit acts like a Peak Detector and stores the maximum voltage value in C2 for that period. (wait at least 1mS for the sample duration).

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> Subsequent periods will reset the "Peak Detector".

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> <otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana">2) After a determined "sample" duration, make the POWER_PIN LOW and use the RCTIME command to detect the amount of time it takes C2 to discharge.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> Note:<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> Since the POWER_PIN is now LOW, C2 is now allowed to discharge through D3 and R11. Experiment with different values for R11 and C2 for different RC time constants.<otongue.gif></otongue.gif>

    <SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana"> The returned value here is proportional to the detected volume level.<otongue.gif></otongue.gif>

    <otongue.gif> </otongue.gif>

    Thank you! That was EXACTLY what I was looking for! One last thing: I have been seeing "(x)V Ref" (reference) in shcematic recently, but I never knew what it means. What does that mean? Do I just simply connect it to 2V?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-14 00:42
    wow...that reply came out nasty...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-11-14 17:32
    That's right about Vref. The exact value and the purpose of Vref depends on the circuit. In Beau's circuit, Vref is a value that comes from the "squelch" potentiometer. It should be set to around 2 volts, but as Beau stated, you can adjust the sweet spot.

    In his usual clever fashion, Beau has turned a shortcoming of the op-amp into a feature of the circuit. The LM741 op-amp is usually characterized for +/-15 volt power supplies, not for operation on a single 5 volt supply as it is here. One factor is how far the input and output voltages can swing. Outside that range the op-amp ceases to operate as an amplifier. For the LM741 on 5 volt supplies, the typical input range would be 1 volt centered on 2.5 volts. If the input is below 2 volts or above 3 volts it won't work. The output too has limits that are typically a swing of 3 volts on a 5 volt supply, so the output can't be made to go below 1 volt or above 4 volts. By setting the Vref to around 2 volts, you are finding the threshold voltage for your op-amp, and if you put it a tiny bit below the threshold, it will "squelch" unwanted noise. See, how Beau turns a drawback into a feature!?

    Furthermore, those figures for the input and output swing are "typlcal" as listed on the data sheet, so they will be a little different on every LM741, and that is why the threshold has to be adjustable. The data sheet gives both typical values and also worst case values for the swing. If your LM741 happened to be one with the worst case characteristics, the circuit won't work at all on the 5 volt supply. That very unlikely, but possible. It is a stretch to extrapolate 5 volt characteristics from a data sheet that only gives characteristics at +/- 15 volt power supplies. A true single supply (rail to rail) op-amp would not have those issues.

    The way the diode is connected at the output also provides some "squelch" (as Beau also observed). The output signal has to overcome the 0.6 volt forward drop of the diode before it starts to charge the output sampling capacitor. Sounds below a certain level won't pass that threshold.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 11/14/2007 5:38:11 PM GMT
  • SkeilSkeil Posts: 7
    edited 2007-11-14 18:05
    Heres a quick question if you would like something simple why not use one of the VU meters from the links below
    http://store.qkits.com/moreinfo.cfm/MK115
    http://store.qkits.com/moreinfo.cfm/K4304
    http://store.qkits.com/moreinfo.cfm/K4306
    on the 10 led kit you can merge every 2 leds to bring the outputs down from 10 to 5 outputs
    you then just poll those outputs using count command for each of the 5 pins
    and then do whatever logic you want to do

    basically dont reinvent the wheel
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-11-14 18:36
    Thanks Tracy!

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

    IC Layout Engineer
    Parallax, Inc.
  • NoBo780NoBo780 Posts: 94
    edited 2007-11-14 22:14
    Tracy Allen said...
    That's right about Vref. The exact value and the purpose of Vref depends on the circuit. In Beau's circuit, Vref is a value that comes from the "squelch" potentiometer. It should be set to around 2 volts, but as Beau stated, you can adjust the sweet spot.

    In his usual clever fashion, Beau has turned a shortcoming of the op-amp into a feature of the circuit. The LM741 op-amp is usually characterized for +/-15 volt power supplies, not for operation on a single 5 volt supply as it is here. One factor is how far the input and output voltages can swing. Outside that range the op-amp ceases to operate as an amplifier. For the LM741 on 5 volt supplies, the typical input range would be 1 volt centered on 2.5 volts. If the input is below 2 volts or above 3 volts it won't work. The output too has limits that are typically a swing of 3 volts on a 5 volt supply, so the output can't be made to go below 1 volt or above 4 volts. By setting the Vref to around 2 volts, you are finding the threshold voltage for your op-amp, and if you put it a tiny bit below the threshold, it will "squelch" unwanted noise. See, how Beau turns a drawback into a feature!?

    Furthermore, those figures for the input and output swing are "typlcal" as listed on the data sheet, so they will be a little different on every LM741, and that is why the threshold has to be adjustable. The data sheet gives both typical values and also worst case values for the swing. If your LM741 happened to be one with the worst case characteristics, the circuit won't work at all on the 5 volt supply. That very unlikely, but possible. It is a stretch to extrapolate 5 volt characteristics from a data sheet that only gives characteristics at +/- 15 volt power supplies. A true single supply (rail to rail) op-amp would not have those issues.

    The way the diode is connected at the output also provides some "squelch" (as Beau also observed). The output signal has to overcome the 0.6 volt forward drop of the diode before it starts to charge the output sampling capacitor. Sounds below a certain level won't pass that threshold.

    Brilliant! I didn't know it worked like that. Now THATS ingenuitive thinking!! Thank you all very much. You opened up a wide window for me! Now I just need to get some pots., and I'll start building! Here's the code so far if anyone wants to take a look at it:
    
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    
    pingtime          VAR   Word                    ' distance measurement from Ping)))
    cdstime           VAR   Word                    ' main photocell rctime
    leftcdstime       VAR   Word                    ' left photocell rctime
    rightcdstime      VAR   Word                    ' right photocell rctime
    pulsecount        VAR   Word                    ' pulse counter for servos
    dropoffircounter  VAR   Byte                    ' This is to prevent interference between IR modules
    'average           VAR   Word                    ' for       CdS
    'difference        VAR   Word                    '     front     cells
    
    ' -----[noparse][[/noparse] Navigation Variables/Constants ]---------------------------------------------------------------------------------
    
    ' generic values
    
    i                 VAR   Byte                    ' loop counter, whatever
    tmp               VAR   Word                    ' temporary holder
    seed              VAR   Word                    ' random number seed
    
    ' these are for the servo routines
    
    LEFT    CON    13                               ' left wheel port
    RIGHT   CON    12                               ' right wheel port
    SACT    CON    5                                ' times through act routine
    drive   VAR    Word                             ' wheel command combo
    ldrive  VAR    drive.BYTE1                      ' left wheel command
    rdrive  VAR    drive.BYTE0                      ' right wheel command
    aDur    VAR    Byte                             ' duration of pulse left
    'avDir   VAR    Word
    'avDur   VAR    Nib
    
    ' servo drive commands
    
    fd      CON    $6432                            ' forward (left = 100 ((16*6)+4), right = 50 ((16*3)+2))
    rv      CON    $3264                            ' reverse (left = 50 ((16*3)+2), right = 100 ((16*6)+4))
    st      CON    $4b4b                            ' STOP (left = 75 ((16*4)+11), right = 75 ((16*4)+11))
    tr      CON    $644b                            ' turn right (left = 100 ((16*6)+4), right = 75 ((16*4)+11))
    tl      CON    $4b32                            ' turn left (left = 75 ((16*4)+11), right = 50 ((16*3)+2))
    rr      CON    $6464                            ' rotate right (left = 100 ((16*6)+4), right = 100 ((16*6)+4))
    rl      CON    $3232                            ' rotate left (left = 50 ((16*3)+2), right = 50 ((16*3)+2))
    
    ' wander values
    
    'wstate  VAR    Byte 'shared Byte
    
    wDir    VAR    Word 'wander value
    wDur    VAR    Byte 'wander duration
    
    ' -----[noparse][[/noparse] Initialization ]-------------------------------------------------------------------------------------------------
    
    wDur   = 0                                        'clear wander duration
    aDur   = 0                                        'clear act loop counter
    'wstate = 0                                       ' initial wander state
    
    FREQOUT 5, 1000, 2900
    PAUSE 1000
    
    GOTO Main
    
    ' -----[noparse][[/noparse] Main Program ]---------------------------------------------------------------------------------------------------
    
    Main:
    
      GOSUB wander
      GOSUB Check_CDS
      GOSUB avoid
      GOSUB act
    
    GOTO Main
    
    Check_CDS:
    
      HIGH 0
      PAUSE 3
      RCTIME 0, 1, cdstime
      'DEBUG HOME, "cdstime = ", DEC5 cdstime
    
      IF cdstime > 4500 OR cdstime = 00000 THEN
        HIGH 11
     ' ELSEIF cdstime > 10000 THEN
        'GOSUB escape
      ELSEIF cdstime > 4500 AND cdstime < 10000 THEN
        FREQOUT 5,200,5000
        PAUSE 100
        FREQOUT 5,100,1000
        PAUSE 100
        FREQOUT 5,250,4000
        PAUSE 1000
      ELSE
        LOW 11
      ENDIF
    
      IF cdstime > 6000 AND cdstime < 5950 THEN
        FREQOUT 5,200,5000
        PAUSE 100
        FREQOUT 5,100,1000
        PAUSE 100
        FREQOUT 5,250,4000
        PAUSE 1000
      ENDIF
      RETURN
    
    
    avoid:
    
    ' The following IF statement requires a counter variable to prevent interference between the GP2D12's, and the
    ' standard IR module.
    
      FREQOUT 10, 1, 37500                                                ' send frequency pulses of 41,500KhZ to the IR LED
      IF IN9 = 1 THEN                                                     ' if no floor is detected, then
        dropoffircounter = dropoffircounter + 1                           ' add to the counter
      ELSEIF IN9 = 0 THEN                                                 ' if the floor is detected, then
        dropoffircounter = 0                                              ' reset the counter
      ENDIF
    
      IF dropoffircounter > 5 THEN                                        ' if detection threshold for dropoff IR is reached,
        FOR pulsecount = 0 TO 110                                         ' then back up
        PULSOUT 13, 620
        PULSOUT 12, 850
        NEXT
        FOR pulsecount = 0 TO 220                                         ' ...and rotate right ~180 degrees (turn around)
        PULSOUT 13, 820
        PULSOUT 12, 850
        NEXT
        dropoffircounter = 0
      ENDIF
    
      IF IN6 = 1 THEN                                                     ' if left IR detects something,
        PULSOUT 13, 820                                                   ' rotate right
        PULSOUT 12, 850
      ENDIF
    
      IF IN8 = 1 THEN                                                     ' if right IR detects something,
        PULSOUT 13, 620                                                   ' rotate left
        PULSOUT 12, 650
      ENDIF
    
    ' The following IF statement is to prevent from getting stuck in tricky places
    
      IF IN6 = 1 AND IN8 = 1 OR IN6 = 1 AND IN8 = 1 AND pingtime < 600 THEN       ' if left IR and right IR detect something, or if left IR and right IR and ping detect something all at once,
        FOR pulsecount = 0 TO 220                                                 ' then rotate right ~180 degrees
        PULSOUT 13, 820
        PULSOUT 12, 850
        NEXT
      ELSEIF IN6 = 1 AND pingtime < 600 THEN                     ' if left IR and ping detect something all at the same time,
        FOR pulsecount = 0 TO 110                                ' then rotate right ~90 degrees
        PULSOUT 13, 820
        PULSOUT 12, 850
        NEXT
      ELSEIF IN8 = 1 AND pingtime < 600 THEN               ' if right IR and ping detect something all at the same time, then
        FOR pulsecount = 0 TO 110                          ' rotate left ~90 degrees
        PULSOUT 13, 620
        PULSOUT 12, 650
        NEXT
      ENDIF
    
      PULSOUT 15, 5
        PULSIN 15, 1, pingtime
          IF pingtime > 600 THEN avdone
          IF pingtime < 600 THEN avdo
          RETURN
    
    avdo:
    
      LOOKUP i, [noparse][[/noparse]rr, rl, drive], tmp
      drive = tmp
      i = 0                                         'clear history
    
    avdone:
    
      RETURN
    
    'Check_Busy:
      'PAUSE 1                                       ' allow busy to activate
      'DO WHILE (Busy = Yes) : LOOP                  ' wait until not busy
      'RETURN
    
    wander:                                          ' randomly wander around
      IF wDur > 0 THEN wDone1
      RANDOM seed                                    ' random direction
      i = seed & %111                                ' mask off for 0-7 only
      LOOKUP i,[noparse][[/noparse]fd,tl,fd,fd,fd,fd,tr,fd],wDir        'chose direction
      seed = seed + i
      wDur = (seed & %111111) + 20                   ' mask for 64 choices of duration
    
    wDone1:
      wDur = wDur - 1                                'decrement wander counter
      drive = wDir                                   'get direction
      RETURN                                         'completed
    
    'wander:
    
      'BRANCH wstate,[noparse][[/noparse]wcDir,wcDur]                   ' state 2 immed. follows
      'wDur = wDur - 1
      'IF wDur > 0 THEN wDone1
        'drive = wDir                                ' get direction
        'wstate = 0                                  ' reset state
    
    'wDone1:
      'RETURN                                        ' completed
    
    'wcDir:                                          ' choose direction
      'RANDOM seed                                   ' random direction
      'i = seed & %111                               ' mask off FOR 0-7 only
      'LOOKUP i,[noparse][[/noparse]tr,fd,fd,fd,rr,fd,fd,tl],wDir       ' chose direction
      'wstate = 1                                    ' next state
      'RETURN
    
    'wcDur:                                          ' choose duration
       'RANDOM seed                                  ' random direction AND duration
        'wDur = (seed & %111111) + 20                ' mask FOR 64 choices
        'wstate = 2                                  ' next state
        'RETURN
    
    act:                                             ' moves servo motors
        IF aDur > 0 THEN aDec                        ' already doing one, got here
            aDur = SACT                              ' times through this one
            PULSOUT LEFT,ldrive * 10
            PULSOUT RIGHT,rdrive * 10
    
    aDec:                                            ' decrement stuff
            aDur = aDur - 1
    
    aDone:
    RETURN
    
    'escape:
    
    'DO
      'GOSUB Test_Photoresistors
      'GOSUB Average_And_Difference
      'GOSUB Navigate
    'LOOP UNTIL cdstime < 4000
    
    'return
    
    'Test_Photoresistors:
    
    'HIGH 3
    'PAUSE 3
    'RCTIME 3,1,leftcdstime
    
    'HIGH 2
    'PAUSE 3
    'RCTIME 2,1,rightcdstime
    
      'GOTO Average_And_Difference
    
    'Average_And_Difference:
    
      'average = rightcdstime + leftcdstime / 2
      ''difference = average / 6
    
      'GOTO Navigate
    
    'Navigate:
    
      ' Shadow significantly stronger on left detector, turn right.
      'IF (leftcdstime > rightcdstime + difference) THEN
        'PULSOUT 13, 820
        'PULSOUT 12, 850
      ' Shadow significantly on the right detector, turn left.
      'ELSEIF (rightcdstime > leftcdstime + difference) THEN
        'PULSOUT 13, 650
        'PULSOUT 12, 650
      ' Shadows in same neighborhood of intensity on both detectors.
      'ELSE
        'PULSOUT 13, 820
        'PULSOUT 12, 650
     ' ENDIF
    
      'IF IN6 = 1 THEN
        'PULSOUT 13, 820                           ' rotate right
        'PULSOUT 12, 850
      'elseIF IN8 = 1 THEN
        'PULSOUT 13, 620                           ' rotate left
        'PULSOUT 12, 650
      'ELSEIF pingtime < 600 THEN
    
    
    
      'PAUSE 10
    
      'GOTO escape
    
    END
    
    
    



    Anything that is commented out is either not being used, or I am unable to figure out how to properly utilize it. Unfortunately, I am using a standard IR detector (3-pin; on pin 9) from Parallax for drop-off detection. This may not be the wisest thing to do at the moment, but it is all I have. Soon, I'll get a Sharp GP2D12 IR sensor for that. They really are great for the price, power consumption, and ease of use, and are virtually immune to noise/interference. Pins 6 & 8 are connected to two Sharp GP2D12's.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
Sign In or Register to comment.