12

Is there any way to output audio signals through GPIO and is there any disadvantage with this way?

  • 1
    One very interesting method of outputting any audio signal via any digital I/O pin using minimal hardware is this: romanblack.com/BTc_alg.htm – slebetman Jun 10 '16 at 9:25
9

The audio jack channels (left and right) are provided by PWM driven GPIO (channel 0 by GPIO 12 or 18, and channel 1 by GPIO 13 or 19).

So if you connect appropriate circuitry to those GPIO you will get audio.

See BCM2835 ARM Peripherals for details on the GPIO and PWM peripheral.


EDITED TO ADD

Some background

On recent Pis the audio jack output is provided by PWM channels 0 and 1. PWM channel 0 is fed to GPIO40 which is connected to the (stereo) right channel, and PWM channel 1 is fed to GPIO45 which is connected to the (stereo) left channel.

These PWM channels may additionally be fed to user accessible GPIO.

PWM channel 0 may be routed to GPIO12 and GPIO18.

PWM channel 1 may be routed to GPIO13 and GPIO19.

You route PWM to a GPIO by setting a particular GPIO mode as follows:

  • GPIO12 - set mode ALT0
  • GPIO13 - set mode ALT0
  • GPIO18 - set mode ALT5
  • GPIO19 - set mode ALT5

See page 102 of the Broadcom spec reference above.

There are various ways of setting a GPIO mode.

My pigs utility uses the M command.

E.g. pigs m 18 5 will set GPIO18 to mode ALT5.

  • Do I need to use both channels ? and why there are or there, say I'm using Pi 3 and what is the number of the GPIOs that have hardware PWM ? According to this, there is only one pin has PWM, that is pin 12 (GPIO18). – Trevor Jun 10 '16 at 8:14
  • @IlDivinCodino You can use one or two channels. Stereo requires two, mono one. My answer mentions the usable GPIO, Or means you can use either GPIO (or both if you can find a reason to do so). – joan Jun 10 '16 at 8:21
  • @joan's own (IIRC) pigpio library allows PWM over arbitrary pins - I wonder how many audio channels you could get that way. – Chris H Jun 10 '16 at 9:47
  • 1
    @ChrisH pigpio DMA timed PWM is restricted in that only a fixed number of frequencies may be generated with a limited number of steps between off and fully on. That's fine for lots of applications, but probably not audio (I don't know how PWM is used to generate audio so this is a guess on my part). – joan Jun 10 '16 at 10:01
  • 2
    @joan, It's the discrete frequencies that would be more of an issue (a 50:50 duty cycle is probably best for maximum volume). So it would be good for an alarm but not for general playback. I might have a chance to play with it over the weekend. – Chris H Jun 10 '16 at 10:19
2

Just as Joan's answer already suggests, the Pi's analog audio out is just that. Adafruit has an article describing this circuitry in detail for use with the Pi Zero (which comes without it), see here.

  • you should have an description for the article, just put the link is not a good way to answer in stackexchange. Sometime the link could be die. – Trevor Jun 13 '16 at 7:27
  • 1
    1) it's not just putting the link, it's giving context (albeit just a little as I admit), 2) link rot is not likely a problem as it is an internal link within raspberrypi.SE, not an external link (I thus would argue it will be just as persistent as your question). The idea was to not repeat the information provided there and not closing both questions as dupes to one another (because they are not, imho). – Ghanima Jun 13 '16 at 19:10

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.