Latest schematic after trying to reduce distortion in the circuit. I’ve added a volume control, proper biasing of signals from the AtTiny, an active 2-pole filter which gives a really clean output, and a massively minimised output stage which is good enough for testing with headphones. My analogue electronics is hit and miss so these values have been found experimentally.
The volume control is part of the biasing circuit. In previous circuits I was saturating the PWM signal into the op amp causing it to clamp to it’s full possible range. This was great in theory but in practice working at full range caused distortion at the limits; there was noise on the top and flattening out of the signal at low frequencies.
With the circuit operating at 5v the new biased signal going into and coming out of the TL072 is between 1.5v and 3v, giving enough tolerance for a clean signal. This is enough voltage to drive the transistor directly, so it runs unbaised with a 10k resistor to convert this to a base current. The component values are borrowed from this excellent three transistor headphone amp.
The low pass filter part is taken from this excellent example. It might be cutting off the high frequencies a little too soon but I’ve left this in so the high sounds are not too piercing. The nice part about this configuration is that’s a neat footprint on protoboard/breadboard as the two caps sit neatly right next to the op amp (15nF going left from pin 2, 33nF going right from pin 3). This is more a note-to-self for breadboarding future versions, and also why I’ve drawn the circuit including the chip pinouts.