Shop OBEX P1 Docs P2 Docs Learn Events
Interfacing an LM386 Audio Amp with a Propeller — Parallax Forums

Interfacing an LM386 Audio Amp with a Propeller

soshimososhimo Posts: 215
edited 2009-01-20 18:44 in Propeller 1
I want to be able to feed the output from prop and feed it into an lm386 op am circuit (it's what I have laying around and I want to play with sound - I'm prototyping the propeller). The only problem is the circuit I had in mind, and the one I've used before on stamp applications requires a +5v supply. I am not an engineer and I wanted to be sure before I blew up my prop that there wouldn't be any issues with this approach.

Here is the circuit I plan on using:
simpleaudioamp.png

Again, TIA.

Post Edited (soshimo) : 12/28/2008 11:46:58 PM GMT
780 x 500 - 5K

Comments

  • Sleazy - GSleazy - G Posts: 79
    edited 2008-12-28 02:50
    This looks good. Make sure to select your Capacitor/Resistor values according to the application notes located in the datasheet.

    You wont risk blowing up your prop, the signal enters the positive terminal of the op-amp. Usually, the input impedance of the positive terminal is very high with this non-inverting mode, so you wont risk any over-current from the prop. This is considered a gainstage buffer amplifier: The positive input terminal shouldnt risk sending non-negligible current back to the prop. The input bias currents of these types of amps are very low. You can always capacitively couple the prop pin signal to the amp also. If you are still worried about things reversing, throw some 1n914 signal diodes directly on the signal line, and between the supplies/signal line to clamp the signal voltage to the 3.3v rails. This dumps problem current from overvolt back into the power rails. For instance, if somehow the chip fails, and somehow the 5v rail is directed backward to the prop, the diodes will dump any extra voltage, greater than 3.3v, into the power rails. This would most likely end up breaking your regulator before your prop, and thats always a cheaper solution. None of this I consider to be necessary, just redundancy of failsafe.

    I like to use op-amps with an external feedback network. This lets you put signal diodes in the feedback loop, increasing reverse-current protection. Im not sure if this one allows that. The chip does allow you to capacitively couple an external gain resistor from ground to pin 1, for external gain control as a supplement to the internal.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-28 03:46
    It does look good (the drawing), but it's not going to work (the circuit).

    Op-amps have extremely high gain.· You have no feedback loop around the op-amp, and·the negative input tied to ground.· You have the positive input tied to something (the Propeller output, through a pot) that will never be lower than ground (thus never below the negative input), and seldom be at ground.· If that positive input to the op-amp gets very close to ground, you'll get highly amplified noise at the output.· When it's more than a few microvolts above ground, the op-amp output will be driven to its most-positive·level possible.· So you won't ever get anything out of the speaker that you want to hear, or anything that came out of the Prop.

    I prescribe a little more study of operational amplifiers and feedback.· S.G. was right about one thing, though -- it won't hurt the Prop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • william chanwilliam chan Posts: 1,326
    edited 2008-12-28 03:56
    LM386 is not an Op-Amp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-28 05:10
    This thread is timely - I'm also interested in the 386 because it is a chip that has been around forever and my local electronics shop stocks it (unlike the amp on the prop proto board). The LM386 is on ebay for 10 for $7.20.

    The gain appears internally set to 20 (though you can change this):

    http://www.hobby-hour.com/electronics/lm386-power-audio.php

    I'm not sure what the input needs. Does it need a blocking capacitor going into the pot? Probably best to put it in. And you would need the 10k/10nF (which I presume are a low pass filter).

    And also I'm not sure what the voltage range is for a prop pin configured as an audio output. 1V peak to peak? 3V3 peak to peak? (Maybe some kind soul could put a scope on one). I am guessing at AC voltages with 10k on the low pass filter coming out of the prop and a 10k volume pot that the volts are about 1.7V at max volume.

    Anyway, say you had a 1V signal, and a gain of 20, ok that is 20V but say you ran it from 10V then you would have the volume pot at half volume before it distorted and ballpark that could work very well.

    The 386 can also drive a speaker, and fairly loud too. Louder than a headphone amp.

    Post Edited (Dr_Acula (James Moxham)) : 12/28/2008 5:36:33 AM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-12-28 05:33
    I am not 100% on this but driving a speaker; it may be better to have 12 volts and -12 volts. This lets the speaker oscillate in both directions vs. one direction i.e +5 to ground. Otherwise I don't think that it wont be very loud. Also a coupling capacitor coming out of the propeller would help keep DC out of the op-amp.

    Post Edited (grasshopper) : 12/28/2008 5:39:47 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-28 05:36
    You don't need the negative volts. C1 (250uF) takes care of that. A simplistic answer I know - pls let me know if you want the long answer.

    The LM386 is a well proven chip. Just follow the recipe.
  • soshimososhimo Posts: 215
    edited 2008-12-28 06:03
    @Carl The circuit currently works, and has worked fine in a halloween gag I've used for years. I probably transcribed it wrong or forgot a component (I did it in a hurry while I was typing this). It's being driven by a basic stamp and plays midi and rttl sequences. Maybe it's the magic pixie dust I sprinked in - I don't know - but it works. Like I said, I am not an engineer - but I do have electronics theory training - it's just very rusty (military - 4 years of electronic theory crammed into 18 months, and yes, we went over op-amps throughly, along with just about every other semi-conducting device and associated circuitry you can think of at that time [noparse][[/noparse]1986], but thank you for the advice).

    @ william chan - you are right the lm386 is a low voltage audio pre-amplifier, my mistake for the heading, although op-amp is a loose term that covers a lot of different devices and I think the 386 can be considered an op-amp.

    @SG - I saw that capacitive gain control - you can add a capacitor on pin 1 I believe and get 200x gain. I think I tried it once on the BS2 and couldn't go very high on the volume without chopping the signal.

    @ grasshopper and dr_acula - I actually found a section in the datasheet (I should have gone there first probably, stupid me) that said you can capacitivly couple the input if there are supply differentials. SG also gave me some outstanding feedback that I'm going to look into (the first was check the datasheet - doh). I just wanted something quick and dirty to check out some music synthesis libraries.

    Thanks everyone for your feedback - my instructors in the Navy always said the only stupid question is the one not asked. I can now confidently move forward with my next set of experiments with the prop chip.
  • soshimososhimo Posts: 215
    edited 2008-12-28 06:16
    @ Dr_Acula - could I use a voltage divider to lower the voltage into the amplifier? Also, not sure if the low pass will work - isn't synthesized music full of high order harmonics? They would get filtered out and I would get a muddy sound. I don't know - I wasn't an audio technician - I worked on RF stuff in the Navy.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-28 06:28
    The voltage divider is already there - it is the volume pot R1

    If you find the volume is too loud, you can always add resistors at the top of the pot, eg another 10k fixed resistor. I doubt it will be too quiet though with a gain of 20.

    Re the low pass filter, don't have a calculator in front of me but I think the one on the picaxe demo board is above audible frequencies. I think its purpose is to turn pwm into a smooth signal. If the pwm frequency is a lot higher than audio, and the low pass is a bit higher, then it will all work fine. I'm thinking you replicate the audio output of the demo board but feed the signal into a 10k pot instead of the max headphone amp, and then replicate the 386 circuit as per the manual. I'd be surprised if that didn't work. And it certainly won't harm the propeller.

    Post Edited (Dr_Acula (James Moxham)) : 12/28/2008 6:34:43 AM GMT
  • soshimososhimo Posts: 215
    edited 2008-12-28 07:03
    Excellent Dr_Acula - that sounds logical. Basically treat the headphone amp as a box on a block diagram - which is exactly what I was going for. The low pass makes sense now and a little study reveals that it's primary purpose is to remove those high order harmonics, leave just the fundamental to feed a smother, less "spikey" signal to the amplifier. That totally makes sense. Again, thanks for the input and I'll be sure to share with you the results.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-28 07:18
    Thanks for the feedback - I am thinking of putting this in a circuit I am designing so I'd be very interested to hear how you get on. Do you have a 386 and propeller ready to test?
  • soshimososhimo Posts: 215
    edited 2008-12-28 08:26
    Dr_Acula said...

    I am thinking of putting this in a circuit I am designing so I'd be very interested to hear how you get on. Do you have a 386 and propeller ready to test?

    Yes, I have a prop and several of the 386 chips. I probably won't get to it tonight but first thing in the morning I'm going to check it out. Also, do you know anything about the sound generation code in obex? I was reading up on different methods of sound generation and using pwm, with a low-pass coincidentally, you can vary the output voltage by varying the duty cycle, thereby producing an analog waveform. So the pwm basically acts as a DAC. I'm wondering if that is the technique used in the in the various code examples since the propeller is plenty fast enough. On other development systems, arduino notably, this technique is not as viable as the processing power required to generate the waveform leaves nothing much else you can do.

    I'm still toying with the idea of having a dedicated sound generation chip though - be it a pic or avr I don't know (I think a prop would be a bit overkill) for my design ideas. I also have an idea to integrate some nvram, for a wave table storage, with the avr or pic uprocessor and have a music synthisizer. That will free up more cogs for video and logic.

    Post Edited (soshimo) : 12/28/2008 8:35:24 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-28 09:51
    I'm not a propeller expert on the code though I understand electronics and also pwm and a fair bit about audio.

    If the propeller makes sound that way, then that is fantastic news. It ought to be able to, given it can generate a waveform up in the megahertz to drive a VGA screen.

    With wavetable synthesis, you just need piles of memory. An SD card would be a great start. Then you read in the data and if you are synthesising, say, a piano with many keys decaying at different rates, just add up the various waveforms at whatever point they are up to and work out a final number and then turn that into a pwm value.

    Sure, many chips can produce horrible audio with square waves and one tone at a time. But I'm not so sure many chips have the flexibility of the propeller to produce audio that is actually pleasing to the ear.

    I'm drawing up the schematic for a standalone terminal based on the propeller (see Oldbitcollector's threads) and one of the design criteria I've been asked to add is audio. Back in the olden days, control G sounded the bell on a terminal, and indicated that the user had just made some dumb mistake with their code. But instead of a boring old beep, wouldn't it be great to have a nice polyphonic bell sound based on a church bell or something?
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-28 14:55
    william chan said...
    LM386 is not an Op-Amp.

    Oops.· I read his subject line.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • soshimososhimo Posts: 215
    edited 2008-12-28 20:51
    @Carl Yes, my mistake on the subject line - it should have read Interfacing an LM386 Audio Amp with the propeller. The 386 is not an opamp, but is an audio power amplifier with built-in biasing and feedback - hence the distinct lack of feedback as you pointed out. You were absolutely right Carl in your answer, even though my subject line threw you off. Again, thanks everyone for the feedback.

    @Dr_Acula - The prop is plenty powerful enough to synthesize music since you could actually use an arduino (or similar atmega/avr chip) to drive a pwm for varying the voltage. The problem is you won't much bandwidth left to do anything else. I think a single cog is fast enough though at 20MIPS to produce multi-voiced audio. So I thought I can either dedicate a cog or go with a dedicated chip. Though it seems a bit overkill considering they make chips specifically for synthesizing music - but what the hell, it's a DIY project right?

    Post Edited (soshimo) : 12/29/2008 12:27:08 AM GMT
  • soshimososhimo Posts: 215
    edited 2008-12-29 00:25
    @ Dr_Acula - You mentioned mixing the different waveforms that are read from the wavetable to create the polyphonic voice - which totally make sense (I'm an amateur musician and play with midi and mixing software all the time). How would you do the mixing though? Since it's digital data, would this technique work?

    Z is the final output value, A and B are different components to be mixed.
    Z = A + B – A·B / 256
    
    



    The theory behind it (and I take no credit if it's actually sound - I found it with some research) is that if you just take the average of the sum of the two values, A and B, you lose precision. Basically, by dividing by 2 you reduce the range of each component to 0-127. This makes one signal noticeably quieter than the other and it's exacerbated for each element or voice added to the mixing. What the equation does above is, after normalizing to reduce each input to 0 or 1, yield the desired result on output without the attenuation. The attenuation for the average weight mixing technique is roughly 6db x log2(num voices) and an 8-bit signal only has about 48db to begin with.

    Post Edited (soshimo) : 12/29/2008 12:59:51 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-29 01:56
    If you are working in 256 bit precision, then whatever you do with the maths, it still is limited to 256 bit precision at the end.

    If, hypothetically, you had a signal with 150 and another with 80 and you add them up and divide by two, I think that ends up being the best you can do.

    If you only could do 8 bit maths, what would not work as well is to take 150 and divide it by 2 and 80 and divide it by 2 and add those up.

    I'd be very much hoping that this would all be done in 16 bit maths though rather than 8 bit. So you might have a wavetable downloaded from somewhere else and it was recorded with a high quality microphone in a studio etc or came directly out of a synth. And that will be a 16 bit wav file. And you might mix it with 10 other wav files, so you are going to need to add numbers up to 655,350. Then you divide that by 10 and I think that should have more than enough precision. If not, you could multiply all the numbers by 20 before you started, then give them weightings, then divide at the end. Can the prop do floating point? If so, maybe multiply everything by 1000 before you start. Then the weigtings can be very precise - 0.365 of a violin plus 0.773 of a piano etc.

    But I'd hope that final number doesn't end up being truncated down to 8 bits.

    I doubt many people could pick 14 bit vs 16 bit precision, so you can get to a point where there isn't any need to try to make it more precise.

    How are things going testing the LM386? I'm thinking of putting this on a board I'm designing and it would be great to have someone say that it does actually work.

    Midi on a Propeller? Sounds nifty!

    Post Edited (Dr_Acula (James Moxham)) : 12/29/2008 2:01:21 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-29 06:24
    Soshimo - is this the circuit you are using? [noparse][[/noparse]with reference to the audio part]

    I'm pretty sure it will work as I've used this chip in other circuits but I haven't breadboarded it with a propeller.

    A few potential issues - first is I'm not sure if the 1uF ought to be a bipolar (and thus possibly a greencap/polyester). I'd err towards a non polarised capacitor.

    Second, The 5V supply should be ok for the amp but obviously 9V will give more volume before distortion. I'd be interested in hearing how loud it can go.

    Third, does the volume pot end up about midway for average volume (log pot), or does it end up right over one side?
  • soshimososhimo Posts: 215
    edited 2008-12-29 07:10
    @ Dr_Acula - Success, of sorts. No, my power supply is not like the one in the schematic you sent. I am using larger caps as I was driving servos with it and needed to compensate for large changes in current which was browning out my prop. That said, it's definitely NOT the PS I want to use. I used a circuit right out of the app notes for the 386 (which is basically exactly the same as the one I posted - with slight modifications in the values). I first turned it on and heard a very loud low frequency buzz - though I could discern the tune underneath, barely as it was very noisy. I added a cap across the vcc and vdd pins of the amp and the low frequency buzz went away, though I could still hear a definite hiss at higher volumes. The pot I'm using is obviously a +- 20% because I measured it at 8.5k and it's marked for 10k. That being said, I didn't get any distortion, just more hiss as I said, and the fall off was not bad. The output was discernible at about 10% volume (and I don't have the best hearing). I don't think though that 20x gain is going to be enough. I didn't scope the output of the prop so I'm not sure what the voltage level is but it's obviously barely enough at 20x gain to drive a small 8 ohm speaker from an external PC speaker I salvaged. I think I remember going through this with the bs2 and ended up adding the bypass to get the 200x. You can also add some series resistance to lower the gain - e.g. 1.2k will bring it to 50x. I was thinking of adding a switch to bypass the resistor to go from 50x to 200x - so you can have a selectable hi or low power output. I also want to add a bass boost feedback circuit (found on the app notes) with a switch as well, but I need to pick up some components for that. All in all, I would say the experiment was a success and with some better ps filtering (and figure out that hiss) it's definitely a go to use the 386 as a cheap replacement for the max chip used in the prop demo board. Now question, is stereo as simple as duplicating the circuit for a left and right speaker? The code I found in obex actually plays 16bit wav files and uses two output pins for stereo but I'm only using one pin right now.

    Update
    One thing I noticed about the different wave files for testing is that the higher frequencies are not as attenuated as the lower frequencies so I may need to adjust the low pass filter values. It could also be my hearing, but it's pretty noticeable. At full volume a low frequency sound can barely be heard across the room but a high frequency sound can be heard clearly.

    Final Update
    Looks like 200x and 50x are definitely the range I want (I have a crappy speaker too, I'll be looking for candidates for cannibalizing tomorrow). My circuit looks exactly like the one you posted earlier Dr_Acula, other than the PS difference but don't feel like breadboarding another PS tonight. Maybe tomorrow, plus I only have one 3.3v regulator and I'm not sure the lm317 will go that low, but if it does I'll probably end up using that. I also see that you can add stereo by just adding another chip. Total part count: 9 for 200x gain (10 for 50x gain) and it would be one less but I didn't have any .05u caps so I had to put two .1u in series. Not bad - the most expensive item will probably be the pot (if you can get the 386's for $0.72 each like you said Dr_Acula).

    Post Edited (soshimo) : 12/29/2008 8:21:00 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-29 12:41
    This thread might be of interest: http://forum.allaboutcircuits.com/showthread.php?t=2878

    Same problem.

    Sounds good though.

    317 can go to 1.2V so 3V3 will be fine.

    Looking at the distortion curves, the distortion goes up ++ at 0.2W. So this is not an amp to annoy the neighbours with.

    I'd be interested to hear how you go with the bass boost, and 10uF and 0.1uF across the power supply pins.

    Post Edited (Dr_Acula (James Moxham)) : 12/29/2008 1:05:58 PM GMT
  • soshimososhimo Posts: 215
    edited 2008-12-29 16:21
    @ Dr_A - yep, definitely a ground loop somewhere as I've eliminated all the low frequency noise with the filter caps across vcc and vdd of the chip. I've got the circuit on a separate breakout breadboard and I'm thinking that may be a problem (I've heard that breadboards have notoriously noisy grounds and it doesn't help that I'm connecting it with a 6" 22awg wire off the main breadboard). I'm also suspect of one solder joint on the speaker. I was in a hurry last night when I soldered the wires to the speaker and one of the wires was jostled. I reheated the joint and it looks nice and shiny but I'm still paranoid. I'll be replacing the speaker out today with a better one as well that hopefully has more dynamic range (I've also heard that PC speakers are on off only with lot's of inductance so that may also be contributing to the "scratchiness" of the playback. Now to get an SD card and breakout board so I can play samples larger than 32kb! Oh, and test stereo.

    *** Update ***
    The better speaker definitely helped and I also realized at the same time that 200x is going to be too much as I was clipping at about 20% volume (now that I can hear the actual sound I can tell that some of the noise I was hearing was distortion).

    There is definitely something weird going on with my ground though. At lowest volume, so basically the signal should be ground, I hear an echo of the sound being played only severely distorted (and at very low volume - kinda sounds like what it would sound comming out of a piezo). I can hear the sound lower in volume as I turn the pot until almost at the end it switches (never get's completely silent) and I then hear that echo. The only thing I can think of is that somehow the audio is getting dumped onto the ground and modulating my ground so I hear the signal.

    Post Edited (soshimo) : 12/29/2008 7:27:17 PM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-29 23:30
    I've just ordered some LM386 chips so I can test this out as well. Zero volume should be zero volume.

    I've also ordered some TEA2025 chips from Futurlec. They are 50c each. Stereo amplifier on a 16 pin DIP. The power depends on the supply voltage: 0.1W at 3V but 2.4W at 12V. They look a bit more advanced than the LM386 in that they have soft clipping and also thermal shutdown.
  • soshimososhimo Posts: 215
    edited 2008-12-30 04:53
    I think the problem was my pot. I swapped it out and the strange distorted sound at lowest volume went away. I measured the pot and it never quite gets to a short and then jumps up to like 2k ohms so I think something is wrong internally with that pot. After replacing it I'm getting much better results. I also switched sound libraries and the high frequency tones went away so I'm not sure if it's a problem with that driver or what but all the other drivers seem to work fine. I'll look into those other chips you mentioned - is the part count for support circuitry on par with the 386?
  • Sleazy - GSleazy - G Posts: 79
    edited 2009-01-20 11:11
    Carl, Im pretty sure this is a moot-point, but this IC has an internal feedback loop, and is designed to have a fixed gain without the commonly seen external feedback connection.· I checked the data sheet, and this diagram matches one of the reccomended configurations.
    Carl Hayes said...
    It does look good (the drawing), but it's not going to work (the circuit).

    Op-amps have extremely high gain.· You have no feedback loop around the op-amp, and·the negative input tied to ground.· You have the positive input tied to something (the Propeller output, through a pot) that will never be lower than ground (thus never below the negative input), and seldom be at ground.· If that positive input to the op-amp gets very close to ground, you'll get highly amplified noise at the output.· When it's more than a few microvolts above ground, the op-amp output will be driven to its most-positive·level possible.· So you won't ever get anything out of the speaker that you want to hear, or anything that came out of the Prop.

    I prescribe a little more study of operational amplifiers and feedback.· S.G. was right about one thing, though -- it won't hurt the Prop.
  • T ChapT Chap Posts: 4,223
    edited 2009-01-20 18:44
    I use this identical circuit and it works fine. If you connect an 8ohm speaker, you may encounter some distortion, as it is rated for 30ohm is I recall. Using a series resistor helped when I was encountering distortion on an 8ohm speaker. My project is using a 5v supply.

    You would most likely see problems if you are using a breadboard.
Sign In or Register to comment.