Interfacing an LM386 Audio Amp with a Propeller
soshimo
Posts: 215
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:
Again, TIA.
Post Edited (soshimo) : 12/28/2008 11:46:58 PM GMT
Here is the circuit I plan on using:
Again, TIA.
Post Edited (soshimo) : 12/28/2008 11:46:58 PM GMT
Comments
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.
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my
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
Post Edited (grasshopper) : 12/28/2008 5:39:47 AM GMT
The LM386 is a well proven chip. Just follow the recipe.
@ 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.
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
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
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, nn5i@arrl.net
@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
Z is the final output value, A and B are different components to be mixed.
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
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
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?
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
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
*** 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
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.
You would most likely see problems if you are using a breadboard.