Go Down

Topic: What is this amplifier doing, exactly? (Read 7 times) previous topic - next topic

Nick Gammon

Interesting. In this particular case I am feeding in square waves anyway, so that's no great loss.

I mean, you can get amplifier chips for a couple of dollars if you want proper amplification, but I was leaning towards getting the square wave tones out of the thing to be loud enough to hear, and not damage the output pin.

So your suggestion of adding the capacitor, and removing the resistor, could well achieve that with minimal effort.

BTW, if I remove the resistor between collector and base, wouldn't the capacitor need to have the positive side (if it had one) to the Arduino output pin, and not the transistor base, as the output pin would be more positive?


Wire it up like my Mosfet example. The cap keeps the DC out of the speaker, and the sound is nice.

Here's the heart of the code:
Code: [Select]

// info on alarm sound
#include "pitches.h"

// notes in the melody:
int thisNote = 0;
int noteDuration = 0;
int pauseBetweenNotes = 0;
int melody[] = {
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

Code: [Select]

 // create a warble once
 for (thisNote = 0; thisNote < 8; thisNote++)
   // to calculate the note duration, take one second
   // divided by the note type.
   //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
   noteDuration = 1000/noteDurations[thisNote];
   noTone(6);       //apparent known bug - need this for the tone to play next.
   tone(6, melody[thisNote],noteDuration);
   // to distinguish the notes, set a minimum time between them.
   // using the note's duration + 10%:
   pauseBetweenNotes = noteDuration * 1.10;
   // stop the tone playing:
   //  noTone(6);

pitches.h is  tab in my sketch:
Code: [Select]

* Public Constants

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Nick Gammon

So I would remove both 100k, and perhaps add a flyback diode over the speaker.

First experiment ...

Without flyback diode:

With flyback diode:


Nice pictures! Even I didn't expect a normal loudspeaker to cause that spike.

Nick Gammon

Next experiment:

I would try this for fun: Remove the 2.2K resistor between the Arduino and the transistor base and replace it with a 1 uF (not critical) capacitor (positive side to the transistor base).

0.33 uF capacitor added in series with the 2.2K resistor:

0.33 uF capacitor added instead of the 2.2K resistor:

The sound is a bit thin with this one.

Nick Gammon

So I would remove both 100k, and perhaps add a flyback diode over the speaker.

Back to original circuit, (no capacitor), however the 100K resistor from collector to base removed (flyback diode still in place):

Now both 100K resistors removed (looks much the same to me):


The DC bias on the speaker will push (or pull) the diaphragm well away from centre and thus
can give strong even harmonics or worst-case damage the mechanical suspension - not good.

For a novelty speaker driver try a MOSFET driver chip like the MIC4422 which takes logic level
in and can run from 5 to 18V and deliver up to 9A!! (at 18V).  Definitely want to use an output
capacitor to protect the chip and speaker, and check it doesn't overheat, but it is also plenty
fast enough to run as a class-D amplifier very nicely - a fast PWM signal for instance will get
you 8-bit audio of sorts.

[ I won't respond to messages, use the forum please ]

Nick Gammon

This is a variation of what CrossRoads suggested:

I kept the transistor, put 100 ohm and a 0.33 uF in series with the signal to the base.

No resistor from collector to base, and 1K from base to emitter.

Sounds a bit thin still. Maybe I'm not doing it right.


Maybe depends on the speaker too. Mine definitely do not sound thin.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Nick Gammon

Apr 02, 2013, 02:00 am Last Edit: Apr 02, 2013, 02:03 am by Nick Gammon Reason: 1
Wiring it like this (I think):

I get this output:

Change the 2.2K base resistor to 100 ohms (is that good for the output pin?) and I get:

That last one sounds pretty good (and loud!).

Nick Gammon

I was starting to doubt that the thing I had in my hand really was a capacitor (it was just sitting around on a breadboard) so I swapped it out for a 1 uF electrolytic. Got much the same results as with the other capacitor, with the last circuit above (with the 100 ohm resistor) and a 1 uF capacitor in series with that:

I just don't "get" the shape of the blue line. I obviously need to learn more about capacitors and the way they work in circuits like this.


throw this ckt away, use something better.

Bah.  It attempts to be an audio amplifier, but the arduino isn't feeding it "audio" anyway, so there is little point.
Throw it away and replace it with something simpler that ISN'T anything close to an audio amp.  Like the traditional "higher power" transistor switch...

Nick Gammon

The circuit in reply #24 is close to that isn't it, Westfw?


The cap is doing what caps do, it's trying to prevent a change in voltage by supplying a large amount of current when it first switches.  Notice how the upper trace (yellow) pin voltage sags when it switches.  I think the 100 ohm resistor performance shows that the transistor is too small since driving it so hard gets it to produce a much better looking wave.
Experience, it's what you get when you were expecting something else.


Apr 02, 2013, 03:29 am Last Edit: Apr 02, 2013, 03:31 am by oric_dan Reason: 1
Try a 10 or 22 uF cap, instead of 0.33 uF. Maybe also use a 100 ohm R from emitter
to ground. This should greatly improve the low-frequency response. Those rapid decays
in the waveforms are due to the input time-constant being way too small.

The large overshoot without the snubbing diode is the typical inductive kickback that
occurs when you open the current to the speaker.

Go Up