Electronic – arduino – Audio amplifier controlling volume with Arduino

amplifierarduinoaudiotransistors

I'm building a project that outputs audio via an Arduino to a powered computer speaker. This audio signal comes from a sine-wave-table lookup that I step through at various frequencies, setting a PWM pin to wave_table[0], then wave_table[1], etc…

I'd like to modulate the loudness of this signal, but I don't want to just divide the wave table value by an integer because I'm worried that it will distort the audio — instead of having a range of 0-255 for the sine-wave it might only go 0-3 for example and then it's not a nice sine shape anymore.

Is there a way I can use a second Arduino PWM pin to control an amplifier to change the analog signal coming from the Arduino to modify the volume before it hits the speaker?

Thanks!!

Best Answer

You can probably use a second PWM to generate a voltage and then send that into a VCA, or get an external digital potentiometer (DPOT) and use that to change the gain of an op-amp if you are willing to add additional ICs. However, if you don't need that many volume levels and you have a bunch of hardware PWMs already on your Arduino, you can just wire them together through some series resistors to create a summer circuit. Assuming your computer speaker has an amplifier built in with high input resistance, you should be able to add several (e.g. 4) PWM outputs together through 1K resistors.

As long as the PWMs were synchronized which they should be, you should get 1/4, 2/4, 3/4, and full amplitude using 4 channels (for example). Just make them all generate the same thing and turn some of the PWMs off (by setting it to be a digital 0) if you want to reduce the volume.

[EDIT] Actually, thinking about the problem some more, you can do it with even less hardware if you use the digital outputs as attenuation. Consider the following circuit:

schematic

simulate this circuit – Schematic created using CircuitLab

Each digital I/O basically gives you one bit of volume control, and you can get as many bits as you have extra I/Os. Your highest volume would be with all the digital I/Os set to "high-Z" or input, since your amplifier would just see the PWM. Setting any of the digital I/Os to output with a value of '0' would attenuate the signal, (e.g. if digital 0 was outputting a '0', your audio signal would be 50% attenuated due to voltage division). in the above case, you would have 8 volume levels, but you can easily extend the circuit if you need more bits. You can also make it a logarithmic taper by carefully picking resistor values.