One-transistor audio amplifier for Arduino projects

For my recent Question Block Lamp project, I wanted to be able to play sound effects. Initially I was a little concerned that it would be difficult, but after I found the awesome Mario Piano Sheet Music project and Arduino’s tone library, it looked like everything was going to be super easy!

However, after writing the code and wiring up the circuit, I ran into a little problem: the sound that came out of the speaker was too quiet. It was audible, but wasn’t exactly robust, and that was with it into my breadboard. Installed, the speaker was going to be stuck on the inside of a 3mm acrylic box, so it seemed like there was significant risk of the sound effects being inaudible.

Sound volume is directly proportional to current. When you’re hooking up a speaker to an Arduino as described above, the reason for the softness of the sound comes down to the fact that the digital out pins on AVR microcontrollers (ATMegas for official Arduino boards, or the ATTiny in my case) can only source or sink around 50 mA, which is why the tone examples have you hooking up the speaker through a 100-ohm resistor. Most speakers, including the cheap ones at RadioShack, can handle a lot more current than that, and without the resistor you’d probably fry the pin on your microcontroller.

So the problem has an obvious diagnosis, but what about the solution? I’ve read about audio amplifiers in the DIY context in the past, but truth be told all that did was make me nervous. I thought it was likely to be complex, require a number of additional components on the board, and would add cost to the project.

Yet again, it turns out that I was over-thinking the problem. In most cases where you need to use a professionally designed audio amplifier, the audio is a complex waveform, and the way you amplify it can radically change how it sounds. But when you’re using Arduino’s tone library, your waveform is actually incredibly simple: it’s nothing more than a square wave that swings from +5v to ground on the frequency of the note you’re playing.

What’s so great about that? All you need to do to amplify the current of a square wave is a single transistor. It’s really easy to wire up: just connect the Arduino’s output pin to the transistor’s base pin through a 100-ohm resistor, then the collector pin to +5v and the emitter pin to the speaker’s + lead.

This solution costs almost nothing and is staggeringly effective. When I tested it, there was a clear, significant volume increase. Even from inside the sealed box, it was more than loud enough to get the job done. Isn’t it great when things work out so well?

Advertisements

22 thoughts on “One-transistor audio amplifier for Arduino projects

  1. Farldarm

    something else you might try with the speaker. Connect it to VCC instead of GND. The avr used in the arduino can sink more current than it can source so you can use a smaller resistor and get louder output.Just a thought,[:Ray Moore

    Reply
  2. Rohit

    I think George has raised a valid point. NPN BJTs are better at sinking current than at sourcing. I too am of the opinion that you may get slightly better performance if the NPN emitter is grounded and the speaker is connected from Vcc to the collector.

    Reply
  3. Max Dobres

    Brian,Looks like an elegent solution for amplication. However I am new to this and did not quite understand your connection instructions.I assume from your diagrem that VCC and GND on the ATTINY go to Arduino pins 8 & 4 respectively.I see Pin 6 connected through the resistor. You then say "collector pin to +5v and the emitter pin to the speaker's + lead.I don't quite understand this:Could you help me by spelling out in more detail what the other end of the resistor is connected to. ie what is the +5v connection and GNDA connection.As you might gues I am a software person with limited H/W knowledge.Thanks

    Reply
  4. Bryan Duxbury

    Hey Georgia – Which component is getting hot, the speaker or the transistor? You should make sure that both components will be capable of handling the amount of current you'll be passing through it. For the transistor, make sure that the collector-emitter current is high enough, and for the speaker make sure that the max wattage is greater than the amount you'll need.

    Reply
    1. Wes

      Indeed; experimentally I see 2 amps at 6 volt power supply. Assuming that the purpose is just to reproduce square waves is there a shortcut or is it necessary to bias the transistor to be non-conducting when the has the voltage low on the base resistor? Can that be accomplished with a voltage divider of resistors between the supply voltage and base and base and ground?

      Reply
      1. Wes

        I need to withdraw this comment. The issue turned out to be a poorly placed test lead. Correcting that I do not see any large current drain. I thought I was seeing the current drain described by Alex D. BTW I am using a 2N3904.

  5. jonrabbette

    Hi there, I’m new to this game. From the ATTiny where do pin 4 & 8 connect to on the Arduino, is it the VCC & Ground pin on the Arduino?

    Reply
  6. francescosoave

    hi there, thanks for the tutorial! for now I was trying to connect the speaker straight to the pin 9, without resistor. however I basically only hear noise coming out from it..do you know what could be the cause? thanks!

    Reply
    1. bryanduxbury Post author

      The pin on the microcontroller can’t drive a huge amount of current, so that might affect your output volume. It also might not be in “output” mode. But other than these guesses, hard to say without a schematic and a sketch!

      Reply
      1. francescosoave

        Hi there..thank you for the reply..it was a really silly mistake..I didn’t read about the sound format required by the library..once that was fixed everything was ok..one tip that might be useful for others..if the song contains metadata there will be crackle noise at the end of each song. Removing them (audition can do this easily when exporting) will solve the issue. too bad the quality required is so low ;/ thanks for your help!

  7. Aasif Shah

    Hello Bryan! Thanks for this awesome tutorial. I did’t try it yet but i was thinking if I should add a filtering circuit prior amplification? I am generating PWM audio through arduino (8 bit 8kHz mono) and it sounds a little bit noisy. i already tried RC filter with different cutoff frequencies, but i don’t see any change in the noise. Only the output would decrease (if i use higher R value). So while considering amplification using your method, i am guessing it would also amplify the noise, which won’t be nice for me :/.
    Thanks

    Reply
    1. bryanduxbury Post author

      Yeah, this setup will only make your audio louder, not less noisy. You probably want to use something like an op-amp-based 2-stage low pass filter if you want to remove high-frequency noise.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s