12

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

Van Tr
  • 324
  • 2
  • 3
  • 11

3 Answers3

11

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.

joan
  • 71,852
  • 5
  • 76
  • 108
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.

Ghanima
  • 15,958
  • 17
  • 65
  • 125
1

Raspberry Pi 4 / Raspberry Pi Zero Systems with Debian/Rasbian Buster

If you are using the latest version of the Raspberry Pi OS at the time of writing you may require this dtoverlay to remap audio from the AV Jack to the GPIO pins.

dtoverlay=audremap,pins_12_13 # for pins 12 and 13
dtoverlay=audremap,pins_18_19 # for pins 18 and 19

See pinout.xyz for information on these pins.

GPIO12 & GPIO13 are Physical pin numbers 32 and 33.

GPIO18 & GPIO19 are Physical pin numbers 12 and 35.

These commands remap or redirect the analog (PCM) audio to the GPIO pins.

If you view the signals on a scope you will observe that they are square waves. If you want "analog" output, such as that which comes out of the AV Jack, you will require a low and high pass filter combination. Such a filter exists on the R-Pi 4 board before the 3.5 mm jack. There is no such filter when routing signals to GPIO.

user3728501
  • 248
  • 4
  • 11