Tiny Amplifier With ATtiny

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.

31 thoughts on “Tiny Amplifier With ATtiny

    1. 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.

  1. 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? ;-)

    1. 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.

    1. 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.

      1. 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?

        1. 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.

          1. 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.

    1. 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.

      1. 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.

  2. 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.

    1. 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.

      1. 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!

    1. 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.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.