PWM audio amplifier circuit crashes arduino.

I'm trying to implement a variable frequency buzzer using a PWM pin, and it works well other than occasionally resetting my arduino. Here's the circuit:


The speaker is 8 ohm and hooked to the pin connector in the diagram. The transistor bases go to pin 13. This also has the nice side effect that I can hear my program uploading.
My code causing the problem is this:

analogWrite(13,100);

I borrowed an oscilloscope, and saw that the 5v line has some pretty big spikes above and below 5v which I suspect is causing my resets.

What's the best way to fix this? I decoupling capacitor between +5 and gnd?

decoupling capacitor between +5 and gnd?

Yes and you need a big one. I would start off with a value of 220uF and go up it that is not a cure.
You can also use a series inductor for the amplifier supply like the last circuit on this page:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Hi, where are you getting the 5V for the collector of T1. I hope it not the 5V from the arduino.
If you are, any output amplification like that is actually power amplification, which means you are drawing current from the arduino regulated supply probably beyond its rated current.
Provide a separate 5V filtered supply and your problem will be solved.

Tom....... :slight_smile:

TomGeorge:
Hi, where are you getting the 5V for the collector of T1. I hope it not the 5V from the arduino.

Why, yes I am which seems like it might be a problem.

I probably should have powered it from Vin (a small 6v SLA battery).

No decoupling will overcome the problem, no need for a separate supply.

You are missing the base resistor! Without that you will destroy your arduino and/or transistors sooner or later...

Wire a 1K resistor between the pin and the transistors.

// Per.