# Simple tiny speaker driver calculations

#### Raymond Genovese

Joined Mar 5, 2016
1,658
I sometimes use a small and cheap speaker (not a piezo element) with a GPIO port from an embedded controller. Typically, I use a simple driver circuit like that below.

Let me be clear that I don’t think that this circuit is ideal. It does have two compelling characteristics; it works and it is simple – containing only a few components. I understand (or at least think I understand) that this is not a class A amplifier approach and that the current taken by R1 is “wasted”. My defense is that if I were designing a product for production, I would consider a better design – but I’m not and I am keen on avoiding the headache (too stupid and lazy would be another way of looking at it).

The speakers come from reclaimed parts or old orders etc…junk box stuff. They come in different sizes and shapes, but small (1-2 inches) and cheap. They are marked 8 -32 ohms and rated .1 to maybe as high as .5 W, usually .2-.25.

R2 is easy and is chosen to conform to the drive capacity of the GPIO and to provide enough current to operate the NPN cleanly (saturated I suspect is the better term). Q1 is typically a 2n2222 or 3904 or 4401 – again, what is on hand. Then I start to “estimate” R1…maybe 82 ohm, we’ll say. I choose R1 acting as though the speaker is a resistor so that I can easily get an approximation of power delivered to the speaker to avoid cooking or overdriving while achieving near-maximum volume.

I then run a simple program like the one below (which is in Arduino C), which operates the speaker at a number of different frequencies so I can actually hear the resulting response and volume.
C:
int Speaker = 2;
unsigned long startTime;
unsigned long currentTime;
unsigned long interval = 2000; // milliseconds

void setup() {
pinMode(Speaker, OUTPUT);
}

void loop() {

startTime = millis();
while ( (currentTime=millis())-startTime<=interval)
{
// 500 Hz
digitalWrite(Speaker, HIGH);
delayMicroseconds(2000);
digitalWrite(Speaker, LOW);
delayMicroseconds(2000);
}
startTime = millis();
while ( (currentTime=millis())-startTime<=interval)
{
// 1000 Hz
digitalWrite(Speaker, HIGH);
delayMicroseconds(1000);
digitalWrite(Speaker, LOW);
delayMicroseconds(1000);
}
startTime = millis();
while ( (currentTime=millis())-startTime<=interval)
{
// 2000 Hz
digitalWrite(Speaker, HIGH);
delayMicroseconds(500);
digitalWrite(Speaker, LOW);
delayMicroseconds(500);
}
startTime = millis();
while ( (currentTime=millis())-startTime<=interval)
{
// 4000 Hz
digitalWrite(Speaker, HIGH);
delayMicroseconds(250);
digitalWrite(Speaker, LOW);
delayMicroseconds(250);
}
startTime = millis();
while ( (currentTime=millis())-startTime<=interval)
{
// 8000 Hz
digitalWrite(Speaker, HIGH);
delayMicroseconds(125);
digitalWrite(Speaker, LOW);
delayMicroseconds(125);
}
}
I realize that the speaker is not a simple resistor. I understand that it is marked as impedance, not resistance, and the value takes into account inductive reactance and all that great EE stuff. I guess my question is can I do better, using this circuit, but without taking the time and effort to actually study RLC circuits and learn all of the associated formulas (again invoking the stupid/lazy defense or, more preferable would be an asset allocation choice defense)?

Example: Given:

Vcc=5V R2=1K Q1=2N3904 SP=32 ohm. .25W what is the calculator-style formulas for calculating R1 to operate the speaker at 85% of is rated power at frequencies of 500, 1000, 2000, 4000, 8000 Hz?

A second question is that I normally do not add a flyback diode across the speaker as I would if it were a relay. I almost never see this in circuits and concluded (possibly erroneously) that it is not necessary – is that true?

#### RichardO

Joined May 4, 2013
2,271
I would put the speaker in place of R1 in your circuit. This gets rid of most of the inefficiencies. It also saves the coupling capacitor. I does need the flyback diode, however.

But... This change *requires* that the transistor is left off whenever you are not generating sound. (In your circuit, leaving the transistor on will get R1 hot really quick and might even let its smoke out if it is rated at less than half a watt).

To answer your second question: I suspect that resistor R1 is damping the overshoot enough that the flyback diode is not absolutely needed. The 2N2222, etc. have fairly high breakdown voltages so this helps as well. I would probably add the diode if I was doing it since it is cheap and simple protection against later problems.

#### Raymond Genovese

Joined Mar 5, 2016
1,658
I would put the speaker in place of R1 in your circuit. This gets rid of most of the inefficiencies. It also saves the coupling capacitor. I does need the flyback diode, however.

But... This change *requires* that the transistor is left off whenever you are not generating sound. (In your circuit, leaving the transistor on will get R1 hot really quick and might even let its smoke out if it is rated at less than half a watt).

To answer your second question: I suspect that resistor R1 is damping the overshoot enough that the flyback diode is not absolutely needed. The 2N2222, etc. have fairly high breakdown voltages so this helps as well. I would probably add the diode if I was doing it since it is cheap and simple protection against later problems.
Thanks @RichardO some food for thought there. " (In your circuit, leaving the transistor on will get R1 hot really quick and might even let its smoke out if it is rated at less than half a watt)." Yes, true at least with an 82 ohm resistor. Not so much for 150 ohm which would be a better choice. Of course the idea is not to leave the pin on

I found a couple of educational threads here and here that are helpful.

#### RichardO

Joined May 4, 2013
2,271
Thanks @RichardO some food for thought there. " (In your circuit, leaving the transistor on will get R1 hot really quick and might even let its smoke out if it is rated at less than half a watt)." Yes, true at least with an 82 ohm resistor. Not so much for 150 ohm which would be a better choice. Of course the idea is not to leave the pin on
The larger you make R1, the lower the volume and the less efficient your circuit.

The example with an 8 ohm speaker and a 6.8 ohm resistor is incredibly awful -- except for showing how *not* to do it. Nothing is right about that circuit.

The other example is better. It still uses a resistor to limit the current through the speaker. Again, very inefficient. It is best to choose the speaker impedance (resistance) to deliver the loudness you want and get rid of the resistor.

#### Raymond Genovese

Joined Mar 5, 2016
1,658
The larger you make R1, the lower the volume and the less efficient your circuit.

The example with an 8 ohm speaker and a 6.8 ohm resistor is incredibly awful -- except for showing how *not* to do it. Nothing is right about that circuit.

The other example is better. It still uses a resistor to limit the current through the speaker. Again, very inefficient. It is best to choose the speaker impedance (resistance) to deliver the loudness you want and get rid of the resistor.
I'm not sure I am following you too well. The responses in the threads I linked were the educational part and not the starting circuit (see my initial post with the request for some formulas). Also, when you say "The larger you make R1, the lower the volume.." Of course I am aware of that as per my text in the original post - "I choose R1 acting as though the speaker is a resistor so that I can easily get an approximation of power delivered to the speaker to avoid cooking or overdriving while achieving near-maximum volume."

"It is best to choose the speaker impedance (resistance) to deliver the loudness you want and get rid of the resistor" - that sounds great! How do I do that since the speaker impedance is a characteristic of the speaker? That's what I don't understand about your response. So, given an 8 ohm, .250 W speaker, what does the circuit look like without the resistor and how do you "get the loudness I want"? What about a 32 ohm speaker with a .300 W speaker? In fact, without R1, how would I vary the loudness at all?

#### RichardO

Joined May 4, 2013
2,271
I'm not sure I am following you too well. The responses in the threads I linked were the educational part and not the starting circuit (see my initial post with the request for some formulas). Also, when you say "The larger you make R1, the lower the volume.." Of course I am aware of that as per my text in the original post - "I choose R1 acting as though the speaker is a resistor so that I can easily get an approximation of power delivered to the speaker to avoid cooking or overdriving while achieving near-maximum volume."
I am going to talk about the circuit that has the speaker in series with the resistor since my opinion is that is the only way that makes sense.

Let's assume that you are using a 32 ohm speaker. For simplicity, let's also assume that it is purely resistive -- no inductance. If you make the resistor value equal to the speaker resistance, they will both dissipate the same amount of power. In our example, that would be roughly 2.5 volts squared/32 ohms = 0.2 watts each. With a 50% duty cycle square wave the average (RMS) power would be half that. (The power into the resistor and speaker will be less than this estimate because the transistor has the saturation voltage loss). Note that that the peak current would be about 5 volts /64 ohms = 0.08 amps. The base current is normally set to 1/10 of the collector current so the port would have to source about 8ma. Some ports can do this some can't.

In the example, the circuit is only about 50% efficient -- Half the power goes into the resistor and half into the speaker (again ignoring the losses of the transistor).
If you increase the resistor value to either reduce the volume or the amount of power used the circuit will get less efficient. For example, with a very large value resistor, nearly all the power will be wasted in the resistor.

"It is best to choose the speaker impedance (resistance) to deliver the loudness you want and get rid of the resistor" - that sounds great! How do I do that since the speaker impedance is a characteristic of the speaker? That's what I don't understand about your response. So, given an 8 ohm, .250 W speaker, what does the circuit look like without the resistor and how do you "get the loudness I want"? What about a 32 ohm speaker with a .300 W speaker? In fact, without R1, how would I vary the loudness at all?
I see your confusion. What I meant, was that instead of using a high value resistor in combination with a low resistance speaker, you should just use a speaker of the same value as the series combination of the speaker and resistor. Admittedly, this is an impractical way of doing it since are not likely to find the speaker resistance you need. In your case, use a 32 ohm speaker rather than an 8 ohm speaker (assuming thatthe 32 ohm speaker is loud enough).

How do you adjust the volume? I was taught to never use hardware when you can do it in software. In this case, you can reduce the volume by turning on the transistor for a shorter portion of the time. For example, if you turn on the transistor for 25% of the time the power will be cut in half compared to a 50% duty cycle. The duty cycle will have to be chosen by trial and error since both the ear and the speaker effect the perceived loudness. At high frequencies, the speaker will be less efficient so the duty cycle will have to be greater.

I hope I have helped rather than muddied things further.

#### Raymond Genovese

Joined Mar 5, 2016
1,658
I am going to talk about the circuit that has the speaker in series with the resistor since my opinion is that is the only way that makes sense.

Let's assume that you are using a 32 ohm speaker. For simplicity, let's also assume that it is purely resistive -- no inductance. If you make the resistor value equal to the speaker resistance, they will both dissipate the same amount of power. In our example, that would be roughly 2.5 volts squared/32 ohms = 0.2 watts each. With a 50% duty cycle square wave the average (RMS) power would be half that. (The power into the resistor and speaker will be less than this estimate because the transistor has the saturation voltage loss). Note that that the peak current would be about 5 volts /64 ohms = 0.08 amps. The base current is normally set to 1/10 of the collector current so the port would have to source about 8ma. Some ports can do this some can't.

In the example, the circuit is only about 50% efficient -- Half the power goes into the resistor and half into the speaker (again ignoring the losses of the transistor).
If you increase the resistor value to either reduce the volume or the amount of power used the circuit will get less efficient. For example, with a very large value resistor, nearly all the power will be wasted in the resistor.

I see your confusion. What I meant, was that instead of using a high value resistor in combination with a low resistance speaker, you should just use a speaker of the same value as the series combination of the speaker and resistor. Admittedly, this is an impractical way of doing it since are not likely to find the speaker resistance you need. In your case, use a 32 ohm speaker rather than an 8 ohm speaker (assuming thatthe 32 ohm speaker is loud enough).

How do you adjust the volume? I was taught to never use hardware when you can do it in software. In this case, you can reduce the volume by turning on the transistor for a shorter portion of the time. For example, if you turn on the transistor for 25% of the time the power will be cut in half compared to a 50% duty cycle. The duty cycle will have to be chosen by trial and error since both the ear and the speaker effect the perceived loudness. At high frequencies, the speaker will be less efficient so the duty cycle will have to be greater.

I hope I have helped rather than muddied things further.
No, that helps. I think we are on the same page. There is no argument about the wasted current with R1. I knew that and said as much in my first post, "and that the current taken by R1 is “wasted”.

I was thinking that I was missing something when you referred to "get rid of the resistor" in post #4. I read through your most recent response and it helps because, essentially, it confirms my understanding.....I think I am doing it the way I thinking I am doing it.

The task at hand, if you will, is to simply use a small collection (maybe 7-8) of cheap speakers. As far as I am concerned, these are for simple square wave use. Constant volume as simple annunciators. Multiple tones some primitive sound effects.

Their characteristics just do not make dedicating a real amplifier feasible. I would say, however, that I am thinking about buying these https://www.ebay.com/itm/10PCS-PAM8...m=142872939646&_trksid=p2047675.c100005.m1851

for speakers that are a bit larger – and at less than 0.35 each!