0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yes it does. But that's not how it's done. \$\endgroup\$ – Ignacio Vazquez-Abrams Sep 19 '16 at 0:39
  • 1
    \$\begingroup\$ A prize piece of advice from a combat robotics book I read: Don't make it if you can buy it. Yes, it might seem like it's cheaper, but (in my experience) not only do you waste a huge amount of time, you end up spending more in order to make it work. \$\endgroup\$ – dpdt Sep 19 '16 at 1:08
  • \$\begingroup\$ @EJP: The question isn't about PWM. PWM is what has been tried so far, but the question is about generating audio waveforms. \$\endgroup\$ – Ben Voigt Sep 19 '16 at 3:59
3
\$\begingroup\$

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)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I udrerstood. I will buy 12bit DAC. Thank you very much. Sorry i can't give you reputation beacause i don't have 15 reputations..... \$\endgroup\$ – Gal Aharon Sep 18 '16 at 21:20
  • \$\begingroup\$ Alternatively, connect the headphones to the UART and set the baud rate to a megabaud or so, then add some clever programming. \$\endgroup\$ – user253751 Sep 19 '16 at 0:41
  • \$\begingroup\$ @immibis: But remember that you can only control 8 data bits out of every 10 UART symbols. And UARTs are designed to be tolerant of gaps between bytes, while those will sure screw with the resulting audio. But it isn't a bad idea for getting as close as possible with the peripherals available. \$\endgroup\$ – Ben Voigt Sep 19 '16 at 0:44
  • \$\begingroup\$ @immibis: Do you mean I should buy uart to analog converter? Donesn't it too slow? \$\endgroup\$ – Gal Aharon Sep 19 '16 at 3:52
  • \$\begingroup\$ @galaharon: I think immibis means that the serial FIFO (whether for asynchronous serial "UART" or a synchronous serial bus) can be used to transition between patterns automatically over time, which eases the interrupt handling burden. The serial pin wouldn't be commands to a DAC (although it could be fast enough, a common 460800 kbaud could send 8 bit samples 44100 per second), but the actual PWM waveform that should be filtered down by an "analog reconstruction filter" (can be as simple as RC lowpass). immibis suggests that b/c my calculation of 2 MHz minimum is achievable by USART. \$\endgroup\$ – Ben Voigt Sep 19 '16 at 3:58
0
\$\begingroup\$

I hope you are AC coupling the pin to your headphones, and attenuating the signal as well. I think a 5v PWM would over-drive most headphones, and the DC component will not do them any good at all. Link to the trrs module you have, it may save use guessing.

1) There is, but you are not going to reach it with an Arduino

2) It sounds like you're using a PWM output (but you may not be, I don't know). The voltage corresponds to the mark/space ratio, the average DC output. Using the standard PWM library, it doesn't matter what ratio is programmed, the frequency stays the same, so the sound will be very similar.

I would expect 9%, 10% and 11% PWM to sound indistinguishable. You may well hear a difference between 33% and 50%, as the ratios of low even and odd harmonics might be sufficiently different, sort of clarinet versus violin playing the same note type differences.

If you want to explore more interesting sounds, then try bit-banging the pin directly. You might find a sigma delta library which would do a better job of rendering an MP3 file into an audio output.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ but how can I change the sound that I hear. I want to convert an mp3 file to binary and send the mp3 file (converted to binary) to the headphones (using the trrs module) do I need to change the frequency of the pwm in order to hear different sounds? \$\endgroup\$ – Gal Aharon Sep 15 '16 at 12:04
  • \$\begingroup\$ Changing Frequency will produce different sound tunes. Because Physics. \$\endgroup\$ – ammar.cma Sep 15 '16 at 12:08
  • \$\begingroup\$ microbit-micropython.readthedocs.io/en/latest/tutorials/… But any accurate reproduction of music will require an ADC to get fine control over the voltage _on_every_sample. \$\endgroup\$ – Sean Houlihane Sep 18 '16 at 20:41
  • \$\begingroup\$ Wrong link - I meant this one, look under PWM. microbit-micropython.readthedocs.io/en/latest/pin.html \$\endgroup\$ – Sean Houlihane Sep 18 '16 at 20:47
  • 1
    \$\begingroup\$ Yes. DAC would be more useful in this case... (unlucky, since many MCUs come with ADCs built in) \$\endgroup\$ – Sean Houlihane Sep 18 '16 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.