Small microcontrollers can pack quite a punch. With the right code optimizations and proper use of the available limited memory, even small microcontrollers can do things they were never intended to. Even within the realm of intended use, however, there are still lots of impressive uses for these tiny cheap processors like [Lukasz]’s audio amplifier which uses one of the smallest ATtiny packages around in the video embedded below.
Since the ATtiny is small, the amplifier is only capable of 8-bit resolution but thanks to internal clock settings and the fast PWM mode he can get a sampling rate of 37.5 kHz. Most commercial amplifiers shoot for 42 kHz or higher, so this is actually quite close for the limited hardware. The fact that it is a class D amplifier also helps, since it relies on switching and filtering to achieve amplification. This allows the amplifier to have a greater efficiency than an analog amplifier, with less need for heat sinks or oversized components.
All of the code that [Lukasz] used is available on the project site if you’ve ever been curious about switching amplifiers. He built this more as a curiosity in order to see what kind of quality he could get out of such a small microcontroller. It sounds pretty good to us too! If you’re more into analog amplifiers, though, we have you covered there as well.
Good job, but shouldn’t the circuit also contain a flyback diode or something to prevent inductive voltage spikes back-propagating into the attiny and damaging/destroying it?
Typically pins have diodes preventing both over and under voltages. One thing to keep in mind is that the supply decoupling cap needs to be close, and capable of handling the spike. Another factor is that the pin is always being driven. Inductive spikes tend to occur when the pin goes high impedance.
Normally the diodes are there anyway, independently of driving the pin.
I couldn’t find a schematic on the website. Did I miss it? But the software is there. All it does is reading the ADC and writing that value into the PWM output register. So the ATtiny is just a converter analog voltage -> PWM signal. So, is it on me this time? Is it my turn to point out that this sounds like a perfect job for a 555? ;-)
schematic is in the linked video, 26 seconds in. cool project though, certainly beats the socks off the self oscillating lm386.
Reading a DAC and turning it to PWM is more or less all a Class D amplifier is anyway, except without the discrete levels, rather fully analogue with any pulse-width, rather than the fixed choice of perhaps 256 you get in this design. In an analogue design the frequency of the PWM affects both the frequency-response of the amp, and also the fidelity, the effective bit-rate of sound reproduction.
Remove the output capacitor and use two outputs of the attiny to drive the speaker. This will give dual voltage swing with 4x power.
Nope. See the other comments elsewhere here. You’ll only double the voltage swing if the output current is zero. But in this case the speaker resistance is much, much lower than the output resistance of the ATtiny: You’re driving essentially into a short, so you’re limited by the device’s maximum output current.
Adding an additional complementary output device in series like you suggest might double the potential output voltage, but also doubles the output resistance, so won’t increase the output current to the speaker at all.
Um, if you double the voltage to the speaker, the wattage goes up by 4 (V^2/R). With the doubled resistance cutting the available power by 2, you’re still left with double the power. Am I missing something else?
But you are not doubling the voltage to the speaker.
True, you’re doubling the supplied voltage, but also doubling the series resistance. The current supplied to the speaker remains (essentially) the same.
To put some real numbers on it:The ATtiny output looks like switches to the rails with a 50 ohm resistor in series with the output pin. Driving an 8-ohm load to mid-rail (as used in this implementation) will give a peak load current of (2.5V/58 ohms)=43 mA (in excess of the ATtiny’s Absolute Maximum specification, btw, but we’ll let that pass). Driving the 8-ohm load with two complementary outputs, as you suggest, will yield a peak load current of (5V/108 ohms)=46 mA.
So you’ll see an increase in output power of 15%. Not four times, not double. 15%.
You’d be much better off putting another speaker in series. Or using something like (say) 32 ohm earbuds, which actually would be a fine application for this, if you don’t mind hitting your ears with high intensity 37 kHz ultrasound.
OK, but if I see it properly, driving the speaker differentially allows for zero quiet current. This configuration means silence produced 50% duty cycle on PWM output. It is sort of analog of an A class analogue amplifier, while bridge PWM is analog of an AB class analogue amplifier.
Brilliant. On github please add the hex file for programming. And if you use Burkhard’s Sparrow from Scap – even Programming via the Internet http://www.elektronik-labor.de/AVR/Sparrow/SimpleSparrow.html for anybody to try it out.
How about digital inputs?
50% less programming!
“which uses one of the smallest ATtiny packages around”
I was disappointed to not see the ATtiny9/10 in SOT23-6.
Now use h-bridge IC to boost the output power. L293D, DRV8829 or DRV8844, etc…
No need.
You can use 2 outputs instead of having the speaker tied to ground for differential PWM.
A +6dB boost.
But Attiny pins still can’t provide enough current (and voltage). No matter if you connect them differentialy or not… H-Bridge ICs can provide 1A at 24V or more…
You mean, kina like https://hackaday.com/2014/12/28/class-d-amp-with-an-h-bridge/ ?
By its own, it is an interesting experiment for learning.
It is not worth the engineering effort to try to expand it as proper Class D amplifiers (e.g. PAM8403) with much higher power, higher PWM frequency are available for far less.
In some parts of the world, availability of some specialized function components is limited, and you would need to wait for the delivery weeks a time. It is always beneficial to have an option to apply something universal, like a cheap ubiquitous microcontroller, or a 555 timer for that matter, when in pinch.
An ATtiny with a 5V supply will provide a 3V (+/- 1.5V) swing at 20 mA. Using a single output pin and that output cap, the appropriate speaker impedance would be 1.5V/0.02A = 75 ohms. It’s a pretty horrible match going into a standard 8 ohm speaker — not very efficient at all.
(and running it in bridge mode, as janostman suggested, just makes it worse, as the output drive is current limited, not voltage limited)
I can see the objection in using an output transformer to match impedance, but it would be very simple to just parallel the four available outputs to at least get the output impedance down to 20 ohms or so, vastly improving the output power into 8 ohms.
Does it need to match the DC impedance, when it is driven in class D mode? Wouldn’t it kind-of work as a buck converter using the speaker’s coil as the inductor?
Power would be lost in DC resistance of speaker (which is unavoidable in any case) and in resistance of the output mosfets in the microcontroller. The power lost in mosfet resistance would depend on the peak current, which could be quite low @ 37kHz.
Class D operation is not some kind of magic voodoo mode where you can conjure current out of the ether or something. It just means the output devices operate in saturation: hard on or hard off, nominally (ideally) dropping no voltage and dissipating no power. Without some kind of impedance transformation (nominally a transformer, but other methods are possible), the current through the output devices IS the current through the speaker. If there was a proper output filter on this the speaker would not even be able to tell if it was being driven by a Class A or D amp. Though as it stands now, without an output filter, the speaker is seeing a full-power 37 kHz square wave at idle, which would be very interesting to see when you scale this up to a 500-watt theater speaker. Built-in smoke effects!
You might have lower losses at PWM frequency, BUT the audio frequency components where you’ll have lots of losses are still flowing through the driver.
I actually used a 100ohm speaker in my playWatch.
8ohms is like a short circuit for the poor AVR.
Is possible to add guitar pedal effects, etc? Reverb, delay, clipping distort? Hmmm
Now you’re talking!
Compression/distortion, probably. That’s just scaling the output before sending it back out. (Not sure you have the cycles to do it, but in principle…)
Reverb and delay require a lot of memory to store the echoes, and you’re not going to pull that off with the 512-1024 bytes (or whatever) of RAM in an ATtiny. 1024 bytes / 37 kHz = 0.028 seconds = 28 ms. A very quick slapback delay. (And see above about computation…)
You probably want a faster chip to be doing live DSP. Or…. just drop the sample rate way down, add a lowpass filter, and experiment with what you’ve got. That’s what I’d do.
I’m flattered
I can’t seem to post a few links to TinyD on hackaday.io!?
Whatever I type with a few links just vanishes, no reason given.
http://www.microchip com/wwwproducts/en/ATtiny85
hackaday io/project/19178/
youtu be/oxtLfcPF988