Searching \ for '[EE] Simple Class D audio amp from uC?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/io/audio.htm?key=audio
Search entire site for: 'Simple Class D audio amp from uC?'.

Exact match. Not showing close matches.
PICList Thread
'[EE] Simple Class D audio amp from uC?'
2011\10\08@132555 by Peter Johansson

picon face
I am a lifelong on-and-off amateur electronics hacker who has recently
gotten back on with microcontrollers.  As such I have started
collecting junked electronics for parts and I have a pile of speakers
-- raw 3" drivers, a bunch of low-end cabinets, and a few older
mid-range cabinets -- I have been wanting to do something with.  I
also have a few generic TDAxxxx amps from scrapped TVs I have working
on breadboards.

Yesterday I got the idea to use these as halloween decorations -- whip
up a simple WAV file player and place them around the yard.  The
speakers will be placed in construction-grade trash bags and then
concealed.  I have a number of rejuvenated rechargeable battery packs
(NiMH, Li-ion, SLA) for power to avoid mains current and wires.  The
WAV file will live on an old 64 MB uSD card in a hacked uSD -> SD
adapter.

The initial plan was to use a simple R-2R resistor ladder DAC to an
op-amp buffer/pre-amp and then into one of the TDA amps.  As I was
breadboarding this, I realized it requires quite a number of
components, and thought things might be a whole lot simpler just
driving the speaker with PWM and a MOSFET.  It didn't take much
research before the light bulb came on and I realized, "Oh, so that's
what a Class D amplifier is!"

So I did a little bit of research on simple uC/PWM-driven, Class D
amplifiers and didn't come up with much.  This maks sense given that
an analog/comparator driver makes a whole lot more sense when you are
dealing with an analog input signal.  But since I will be storing a
digital signal to begin with, and I am in no way concerned with
fidelity, the uC approach seems to make a lot more sense.

I'm looking at a simple, single rail, single mosfet design.  Nothing
more than uC PWM pin -> gate driver -> MOSFET -> L/C filter ->
speaker.  Hardly optimal, but I think this should get the job done.

What I am unsure about are PWM frequency and inductor selection.  200
kHz seems an oft-referenced frequency in the analog designs, but it is
easy enough to use much higher frequencies with hardware PWM
generation in the uC.  I would also like to try to use some of the
inductors I have been salvaging as well -- most of which are unmarked
-- and I am guessing that I might just want to select a PWM frequency
based upon the inductors I have available.

I do not have an inductance meter, but I have been thinking about
building one, and this might be the time.  Most of the L-meter designs
I have seen are fixed-frequency, fixed-current but what I think what I
really want is an L-meter that can vary frequency and current, sending
the results back to a PC so that I can graph inductance vs. frequency
and determine saturation at various frequencies.  This should be not
terribly difficult with Perl/Python and some graphing libraries -- but
has anyone done this yet?

But in any case, is this actually necessary?  Would just hand-tweaking
PWM frequency and capacitor values for optimal sound quality be at all
possible?  I figure if I start with a low voltage, current-limited
power supply and a large speaker I'm not likely to fry anything before
I get values into the ballpark.

Class D really does seem to be the way to go in this application since
quiescent current will be practically zero (in the dead-time between
sounds) in addition to providing good efficiency when actually driving
the speaker.

Many thanks in advance for any comments/suggestions/URLs I missed!

-p

2011\10\08@141310 by Bob Blick

face
flavicon
face
On Saturday, October 08, 2011 1:25 PM, "Peter Johansson"  wrote:

> Class D really does seem to be the way to go in this application since
> quiescent current will be practically zero (in the dead-time between
> sounds) in addition to providing good efficiency when actually driving
> the speaker.

It will have a significant "pop" when you engage it. Alternatively you
could ramp up to 50% duty cycle.

I'm interested to hear how it turns out! No pun intended :)

In the beginning I'd suggest a pullup resistor rather than an inductor,
so you won't have any worries about what happens if it runs unloaded.

Cheerful regards,

Bob

-- http://www.fastmail.fm - A no graphics, no pop-ups email service

2011\10\08@143517 by Michael Watterson

face picon face
On 08/10/2011 18:25, Peter Johansson wrote:
{Quote hidden}

I wrote a Wave to PWM audio to play sound on the PC speaker in Modula-2 on a 286. Native (DOS only used to load the program and PC speaker really can only be turned on/off). The frequency just needs to be > 2x the max audio frequency. 5bit resolution is really poor but recognisable. 8bit can be reasonable. 10bit PWM resolution is quite good. Unless you are doing HiFi, 16bit is overkill.

Higher means less/easier filtering but more FET switching loss. You can just rely on the mechanical filtering of the loudspeaker for a simple application where speaker & supply is close to FET!

For better output range and no DC offset you need 4 fets in bridge, but only one or other of the low side FETs needs PWM drive. (or 2 x PNP transistors and two FETs, use 4 PIC pins)

About 80KHz is minimum for really good non-HiFi. For HiFi you might want higher PWM frequency. Ensure PWM harmonics are not on an AM station. If in doubt put all in screened box :-)

2011\10\08@144256 by Michael Watterson

face picon face
On 08/10/2011 18:25, Peter Johansson wrote:
> What I am unsure about are PWM frequency and inductor selection.  200
> kHz seems an oft-referenced frequency in the analog designs, but it is

You don't need ANY inductors!

You can with 5V rail drive the 8 Ohm or 16 Ohm speaker direct. With one FET it's unpolar so you only get 1/2 the cone travel.

200kHz is bad choice in UK/Ireland  as its 2KHz off R4LW, but "HiFi" designs would use 170kHz to 1MHz range. Above 500kHz switching losses on higher power designs are challenging. I'd avoid 220KHz to 270KHz too.

About 70KHz to 80KHz is fine. Even 20KHz will work for "telephone" quality sound

2011\10\08@144953 by Peter Johansson

picon face
On Sat, Oct 8, 2011 at 2:13 PM, Bob Blick <spam_OUTbobblickTakeThisOuTspamftml.net> wrote:

> It will have a significant "pop" when you engage it. Alternatively you
> could ramp up to 50% duty cycle.

Is this specific to the single-rail, single-mosfet design?  If so, I
understand what is going on and how to address it.

> In the beginning I'd suggest a pullup resistor rather than an inductor,
> so you won't have any worries about what happens if it runs unloaded.

That would be essentially an RC low-pass filter instead of an LC filter, right?

-p

2011\10\08@151039 by Peter Johansson

picon face
On Sat, Oct 8, 2011 at 2:34 PM, Michael Watterson <.....mikeKILLspamspam@spam@radioway.org> wrote:

> I wrote a Wave to PWM audio to play sound on the PC speaker in Modula-2
> on a 286. Native (DOS only used to load the program and PC speaker
> really can only be turned on/off). The frequency just needs to be > 2x
> the max audio frequency. 5bit resolution is really poor but
> recognisable. 8bit can be reasonable. 10bit PWM resolution is quite
> good. Unless you are doing HiFi, 16bit is overkill.

I'll be using the MSP430 and CCR for PWM, so 16 bits is no more
expensive than any fewer bits.

> Higher means less/easier filtering but more FET switching loss. You can
> just rely on the mechanical filtering of the loudspeaker for a simple
> application where speaker & supply is close to FET!

I don't have any 3V logic-level FETs on-hand, so I was planning to use
the three-transistor gate drive circuit that received a fair bit of
discussion a month or so back, so I think I should be able to turn the
gate on and off rather quickly.  I do plan on doing a finger test on
the TO-220 heat sink tab to get a feel for losses as I up the
frequency.

> For better output range and no DC offset you need 4 fets in bridge, but
> only one or other of the low side FETs needs PWM drive. (or 2 x PNP
> transistors and two FETs, use 4 PIC pins)

I have read the design notes on half-bridge vs. full-bridge drive, but
in this particular application I'd prefer to just stick with a simple
1-rail power supply.  I can cope with only 1/2 of available cone
travel.

> About 80KHz is minimum for really good non-HiFi. For HiFi you might want
> higher PWM frequency. Ensure PWM harmonics are not on an AM station. If
> in doubt put all in screened box :-)

Had not thought about this.  Since I have so many bits to play with, I
could easily adjust the PWM frequency to spread out any noise.  I
think I will definitely have to experiment with this now.  It is
unfortunate that my only measurement equipment would be ab actual AM
radio.

-p

2011\10\08@151828 by Peter Johansson

picon face
On Sat, Oct 8, 2011 at 2:42 PM, Michael Watterson <mikespamKILLspamradioway.org> wrote:

> You don't need ANY inductors!

Right.  As Bob mentioned, an RC filter could be used instead of an LC
filter.  I am familiar with the RC filter network for more generic
PWM, but as everything I saw WRT Class D amplifier design used LC
filters, I figured there was some specific advantage to using LC over
RC in this application.  I probably should have thought to ask just
was that was in my original post.  Can someone enlighten me here?

BTW, many thanks for all the great help here!

-p

2011\10\08@153535 by Michael Watterson

face picon face
On 08/10/2011 20:18, Peter Johansson wrote:
> On Sat, Oct 8, 2011 at 2:42 PM, Michael Watterson<.....mikeKILLspamspam.....radioway.org>  wrote:
>
>> You don't need ANY inductors!
> Right.  As Bob mentioned, an RC filter could be used instead of an LC
> filter.  I am familiar with the RC filter network for more generic
> PWM, but as everything I saw WRT Class D amplifier design used LC
> filters, I figured there was some specific advantage to using LC over
> RC in this application.  I probably should have thought to ask just
> was that was in my original post.  Can someone enlighten me here?
>
> BTW, many thanks for all the great help here!
>
> -p.

it's to do with power and RFI. At low power levels you don't need RC either..

At lower power ( < 1.5W) you can just connect FET or Transistor to PIC and direct to 8 Ohm speaker. Use a >3W speaker or less than 50% duty cycle as with a single FET you only can drive cone one direction.

At 80kHz 16bits is 1/65K mark space and clock interval over 5MHz. 16bits is not "free". Unless you are doing "hiFi" consider 8bit to 10bit (clock 256 to 1024 times frequency).

2011\10\08@160717 by Peter Johansson

picon face
On Sat, Oct 8, 2011 at 3:35 PM, Michael Watterson <EraseMEmikespam_OUTspamTakeThisOuTradioway.org> wrote:

> At 80kHz 16bits is 1/65K mark space and clock interval over 5MHz. 16bits
> is not "free". Unless you are doing "hiFi" consider 8bit to 10bit (clock
> 256 to 1024 times frequency).

Ok.  This will teach me to do the math before posting.

I'm not sure I follow yours, but if I use a 16 MHz base clock and
8-bits, I'm already down to 62,500 kHz PWM -- if I have my math right.

And suddenly I realize the advantage of CCR units that can multiply
the system clock as well as divide from it, and why uCs are generally
not used for real Class D amplifiers.

Despite this, I still believe the design should be just fine for my
application, in addition to being a great learning tool -- which is
why I am really doing this, of course.

-p

2011\10\09@191821 by Lee Mulvogue

flavicon
face

       I'm producing 8bit 16kHz sound directly from internal memory on a
18F2620, using PWM output.  The CCS forum threads listed below may be
of some use to you.
       A few notes that may be of use;
       - I tweaked my PWM frequency so that I had exactly 256 steps of
resolution, so I could directly feed in 8bit sound.  Can't remember
off-hand what the resulting frequency was, but it's around double the
audio frequency, which is the minimum recommendation.
       - Something about decoupling caps.  I haven't tried it yet (been
focusing on the other parts of the project), but supposedly using a
single deoupling capacitor will give you positive and negative swing
rather than just positive.  So you could pump even more power into the
mosfet and decouple it so you get maximum deflection out of the
speaker.
       I'm assuming there's a way of calculating a sweet spot for absolute
max deflection for a given speaker, so you can feed it x voltage and
current to get the optimum volume, but again I got stuck actually
finding a source of speakers for my project that didn't cost a small
fortune, so haven't pursued it further.  Then again, Texas Instruments
seem to have some nice cheap class-D amps that appear to suit my
purposes, and would save a lot of hassles, so I may just end up using
a "proper" amp instead.  Kinda seems to defeat part of the purpose
though, using essentially the final stages of a class-D to output a
low-level sound, then feeding it into an amp that splits it up and
again uses PWM to amplify it!        www.ccsinfo.com/forum/viewtopic.php?t=41473&postdays=0&postorder=asc&start=0
[1]
       www.ccsinfo.com/forum/viewtopic.php?p=135734&highlight=#135734
[2]
       Lee  BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px;
}
Links:
------
[1]
www.ccsinfo.com/forum/viewtopic.php?t=41473&amp;postdays=0&amp;postorder=asc&amp;start=0
[2]
http://www.ccsinfo.com/forum/viewtopic.php?p=135734&amp;highlight=#13573

2011\10\09@202050 by Bob Blick

face
flavicon
face
On Monday, October 10, 2011 10:18 AM, "Lee Mulvogue"  wrote:

>        - Something about decoupling caps.  I haven't tried it yet (been
> focusing on the other parts of the project), but supposedly using a
> single deoupling capacitor will give you positive and negative swing
> rather than just positive.  So you could pump even more power into the
> mosfet and decouple it so you get maximum deflection out of the
> speaker.
Even important at low volumes to reduce distortion and power
consumption.

Cheerful regards,

Bob

-- http://www.fastmail.fm - Choose from over 50 domains or use your own

2011\10\09@204705 by Bob Blick

face
flavicon
face
I should also add the correction, it's "coupling" not "decoupling".

Bob

On Sunday, October 09, 2011 5:20 PM, "Bob Blick"  wrote:
{Quote hidden}

> -

2011\10\09@210459 by Peter Johansson

picon face
On Sun, Oct 9, 2011 at 7:18 PM, Lee Mulvogue <leespamspam_OUTbaudalign.com> wrote:

>        - I tweaked my PWM frequency so that I had exactly 256 steps of
> resolution, so I could directly feed in 8bit sound.

This is my plan as well.

>        I'm assuming there's a way of calculating a sweet spot for absolute
> max deflection for a given speaker, so you can feed it x voltage and
> current to get the optimum volume,

I do know that when driving a speaker with typical audio gear you can
hear the speaker "bottom out" when the voice coil reaches full travel
and smacks up against the back of the magnet housing.  I think you
would want to find that level and then back off a bit.  Most people
use amps way underpowered for their speakers, so the amp starts
distorting long before the cone reaches max travel, and the clipping
tears the cone or surround.

>   but again I got stuck actually
> finding a source of speakers for my project that didn't cost a small
> fortune, so haven't pursued it further.

I have had excellent luck repairing vintage speakers from the 70s.
Speakers from the early 70s are actually better, as they contain cloth
surrounds which can be re-doped (made air-tight again) with Permatex
High Tack 98 Gasket Sealant.  Dealing with dry-rotted foam is a bit
more difficult.  You can pay $30 for a proper re-foam kit, or you can
try forming your own cloth surround as a replacement, and then doping
it with the above mentioned stuff.  The result will be more than good
enough for basement/shop/outdoor use, and if you do a decent job on a
pair of high-end speakers, they might even sound better than what you
have now.  Home audio went all to crap around 1980, so avoid the
generic crap that came out from then onwards.  High-end speakers from
this era are of course still worthwhile.  My primary speakers are a
pair of B&W DM-12s I pulled from a dumpster and restored for under
$50.  I couldn't replace them for anything less than a grand new, even
though they are 30 years old.

-p.

2011\10\10@035536 by alan.b.pearce

face picon face
> I'll be using the MSP430 and CCR for PWM, so 16 bits is no more
> expensive than any fewer bits.

You may want to look at Olins development environment for PICs. He did a project called HAL that does something similar to what you want, and all the details are in the files, including a PC app for mangling WAV files into data that could stored in the micro.

http://www.embedinc.com/pic/dload.htm

You will need Olins development environment to get the necessary include files and so on to understand all the macros he uses.


-- Scanned by iCritical.

2011\10\10@143254 by Peter Johansson

picon face
On Mon, Oct 10, 2011 at 3:55 AM,  <@spam@alan.b.pearceKILLspamspamstfc.ac.uk> wrote:

> You may want to look at Olins development environment for PICs. He did a project called HAL that does something similar to what you want, and all the details are in the files, including a PC app for mangling WAV files into data that could stored in the micro.

I was just planning to use the SOX audio library to convert whatever
input I want into raw 8-bit LPCM audio samples at my desired sample
rate.

I did some more math last night and realized that things are going to
be much tighter than I first thought...

As I mentioned, I will be running MCLK at 16 MHz, and driving TIMER_A
at this frequency as well as the MSP430 does not include a clock
multiplier.  8-bit audio will provide a 62.5 kHz PWM frequency.  A
little lower than I was initially thinking in terms of inductor
selection, but as I am not concerned with audio frequencies above 5
kHz this should be acceptable for my needs.

The next selection is audio sample rate.  It looks like there are
three choices here, based on whether I let each audio sample run for
2, 3 or 4 PWM cycles:

2 PWM cycles = 31,250      Hz sample rate
3 PWM cycles = 20,833.33 Hz sample rate
4 PWM cycles = 15,625      Hz sample rate

If I go with a 31,250 Hz sample rate, at only gives me 512 clocks to
fetch a sample from SD-flash and update CCR1.  That's not a lot on an
MCU that requires 4-6 cycles per instruction!

And now I realize why Bob was interested to know when I get it working.  ;-)

-p.

2011\10\11@025334 by Michael Watterson

face picon face
On 10/10/2011 19:32, Peter Johansson wrote:
> 4 PWM cycles = 15,625      Hz sample rate

Allows up to 7.5KHz audio.
Also PAL line rate

2011\10\11@041132 by Michael Rigby-Jones

flavicon
face


> -----Original Message-----
> From: KILLspampiclist-bouncesKILLspamspammit.edu [RemoveMEpiclist-bouncesTakeThisOuTspammit.edu] On
Behalf
> Of Peter Johansson
> Sent: 10 October 2011 19:33
> To: Microcontroller discussion list - Public.
> Subject: Re: [EE] Simple Class D audio amp from uC?
>
> On Mon, Oct 10, 2011 at 3:55 AM,  <spamBeGonealan.b.pearcespamBeGonespamstfc.ac.uk> wrote:
>
> > You may want to look at Olins development environment for PICs. He
did a
> project called HAL that does something similar to what you want, and
all
> the details are in the files, including a PC app for mangling WAV
files
{Quote hidden}

You don't actually need to use an inductor at that rate unless the
highest quality audio is a priority.  The speaker itself is an inductor,
and a typical small low quality speaker will roll off well before 20kHz
anyway.

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

2011\10\11@150331 by Peter Johansson

picon face
On Tue, Oct 11, 2011 at 4:11 AM, Michael Rigby-Jones
<TakeThisOuTMichael.Rigby-JonesEraseMEspamspam_OUToclaro.com> wrote:

> You don't actually need to use an inductor at that rate unless the
> highest quality audio is a priority.  The speaker itself is an inductor,
> and a typical small low quality speaker will roll off well before 20kHz
> anyway.

Right, but I'm planning on making use of these 100 Watt 3-way speaker
cabinets I have been collecting, as well as some 50 amp MOSFETS I have
that won't turn on fully at (5V - BJT driver voltage drop) but are
just fine with a 6 volt supply and some 12V SLA batteries capable of
10s of amps.  Even though audio quality is not a concern I am pretty
sure I am going to need to have an inductor in there to limit current,
and a rather beefy one at that.

-p.

2011\10\11@175346 by Walter Banks

flavicon
face


Peter Johansson wrote:

> Class D really does seem to be the way to go in this application since
> quiescent current will be practically zero (in the dead-time between
> sounds) in addition to providing good efficiency when actually driving
> the speaker.

There is an application note that is about 10 years old on our website to do something very similar using the COP8.

The pdf is located
http://bytecraft.com/apps/cop8capp.pdf

Source code is located
http://bytecraft.com/Complex_Applications

Regards,


walter..
--
Walter Banks
Byte Craft Limited
http://www.bytecraft.com


More... (looser matching)
- Last day of these posts
- In 2011 , 2012 only
- Today
- New search...