Electrical – How to use PWM with an Arduino

arduinoaudio

I bought a trrs audio jack breakout and i tried to use this using arduino. I connected the tip to an analog output (I made a pwm to analog converter), I created program on arduino that send 10% voltage in the pwm and I heared a sound but it didn't changed when i changed the voltage(for 10% to 9%, 11% etc). I have two questions:

  1. Is there any voltage limit in the trrs audio jack?

  2. How can i actually change the sound of what I hear in my headphones (change the sound frequency the sound)

Edit: You told me to change the PWM frequency and I did so and the sound did change, then I used my raspberry pi and I changed the PWM frequency to some values (between 20000-20 hz) and the sound chaged. My problem is that my arduino does not supert changing the PWM frequency freely (I cant change it to any frequency I want). My raspberry pi does support it but I want my project to be portable (I have arduino nano). How can I solve this problem? Is there any PWM chip/module?

Thanks.

Best Answer

PWM is not useful for music. Varying the PWM frequency gets you a tone generator, but for mp3 playback you need at least 16000 samples per second, more probably 22.1k or 44.1k, each with a different, well-controlled amplitude. To create that with PWM, that means the PWM settings are changing 16000 times per second. The PWM frequency itself needs to be 256 times higher to approach 8-bit control over amplitude, so we are talking PWM frequency of 2 MHz, minimum, and that's rather poor audio quality. For "CD quality" 16-bit samples, you'd have 44100 * 65536 = PWM frequency of 2.9 GHz. And that's with a "perfect" analog reconstruction filter that doesn't have any intersymbol interference. That just isn't happening, and it is why audio DACs don't use PWM for waveform synthesis.

Get yourself a better DAC that supports at least 12-bit output (you could use dithering/oversampling and an 8-bit DAC, but the increased sample frequency is going to be more difficult for you). This is basically the same idea as switching from a delta-sigma ADC to successive approximation to direct-conversion, except on the DAC side.

Such a DAC will typically connect via SPI to your processor although I2C is sometimes seen also (both Raspberry Pi and Arduino have these ports available on the expansion connector). Purpose-built Audio DACs frequently use I2S instead (IIRC, the Raspberry Pi processor has this but it is prewired to the onboard audio circuitry)