Simple tiny speaker driver calculations

Discussion in 'Embedded Systems and Microcontrollers' started by Raymond Genovese, Sep 13, 2018.

  1. Raymond Genovese

    Thread Starter Active Member

    Mar 5, 2016
    957
    530
    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.
    SpeakerDriver.jpg
    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.
    Code (C):
    1.  
    2. int Speaker = 2;
    3. unsigned long startTime;
    4. unsigned long currentTime;
    5. unsigned long interval = 2000; // milliseconds
    6.  
    7. void setup() {
    8.   pinMode(Speaker, OUTPUT);
    9. }
    10.  
    11. void loop() {
    12.  
    13.   startTime = millis();
    14.   while ( (currentTime=millis())-startTime<=interval)
    15.   {
    16.   // 500 Hz
    17.   digitalWrite(Speaker, HIGH);
    18.   delayMicroseconds(2000);
    19.   digitalWrite(Speaker, LOW);
    20.   delayMicroseconds(2000);
    21.   }
    22.   startTime = millis();
    23.   while ( (currentTime=millis())-startTime<=interval)
    24.   {
    25.   // 1000 Hz
    26.   digitalWrite(Speaker, HIGH);
    27.   delayMicroseconds(1000);
    28.   digitalWrite(Speaker, LOW);
    29.   delayMicroseconds(1000);
    30.   }
    31.   startTime = millis();
    32.   while ( (currentTime=millis())-startTime<=interval)
    33.   {
    34.   // 2000 Hz
    35.   digitalWrite(Speaker, HIGH);
    36.   delayMicroseconds(500);
    37.   digitalWrite(Speaker, LOW);
    38.   delayMicroseconds(500);
    39.   }
    40.   startTime = millis();
    41.   while ( (currentTime=millis())-startTime<=interval)
    42.   {
    43.   // 4000 Hz
    44.   digitalWrite(Speaker, HIGH);
    45.   delayMicroseconds(250);
    46.   digitalWrite(Speaker, LOW);
    47.   delayMicroseconds(250);
    48.   }
    49.   startTime = millis();
    50.   while ( (currentTime=millis())-startTime<=interval)
    51.   {
    52.   // 8000 Hz
    53.   digitalWrite(Speaker, HIGH);
    54.   delayMicroseconds(125);
    55.   digitalWrite(Speaker, LOW);
    56.   delayMicroseconds(125);
    57.   }
    58. }
    59.  
    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?
     
  2. RichardO

    Senior Member

    May 4, 2013
    2,274
    883
    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.
     
  3. Raymond Genovese

    Thread Starter Active Member

    Mar 5, 2016
    957
    530
    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.
     
  4. RichardO

    Senior Member

    May 4, 2013
    2,274
    883
    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.
     
  5. Raymond Genovese

    Thread Starter Active Member

    Mar 5, 2016
    957
    530
    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?
     
  6. RichardO

    Senior Member

    May 4, 2013
    2,274
    883
    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.
     
  7. Raymond Genovese

    Thread Starter Active Member

    Mar 5, 2016
    957
    530
    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!
     
Loading...