1

Okay, kinda a noob here. I've been looking around, trying to find an answer to playing audio via the PWM pins. I tried rerouting the audio to no avail. I've tried making a script that uses PWM to play audio over the pins. I just can't get it to work at all.

Pi GPIO connections: Speaker+ on GPIO 18 (PWM 0); Speaker- on GND

Visual representation (- = speaker negative, + = speaker positive):

..-..+..............
....................

I am not sure what I am doing wrong. Any help is appreciated.

P.S. Yes, the speaker and all the pins on my Pi work.

1 Answer 1

3

Default audio uses PWM (AFAIK pins 40,41).

It is POSSIBLE to enable PWM on header pins (which will disable headphone jack), and there are a few tutorials explaining this - mainly intended for those models without headphone jack, but will work on any Pi. Frankly there seems little point.

Enable with dtoverlay audremap:-

Name:   audremap

Info:   Switches PWM sound output to GPIOs on the 40-pin header

Usage:  dtoverlay=audremap,<param>=<val>

Params: swap_lr                 Reverse the channel allocation, which will also
                                swap the audio jack outputs (default off)
        enable_jack             Don't switch off the audio jack output. Does
                                nothing on BCM2711 (default off)
        pins_12_13              Select GPIOs 12 & 13 (default)
        pins_18_19              Select GPIOs 18 & 19
        pins_40_41              Select GPIOs 40 & 41
        pins_40_45              Select GPIOs 40 & 45 (don't use on BCM2711 - the
                                pins are on different controllers)

BUT you will have to supply the filtering circuitry (explained in tutorials)

NOTE this will generate line level output; it won't drive a speaker.

3
  • You sure? I added dtoverlay=audremap,pins_18_19 and it started driving the speaker just fine.
    – Foxy6670
    Commented Jan 22, 2023 at 14:47
  • It depends on the (unspecified) "speaker". A typical loudspeaker is low impedance and will produce almost inaudible sounds and will place a DC load which will overload the GPIO. It may work for a high impedance headphone.
    – Milliways
    Commented Jan 22, 2023 at 21:04
  • It appears to work really well on my iMac G3 speakers. It's just a little quiet even on max volume. But other than that it's 100% usable.
    – Foxy6670
    Commented Jan 22, 2023 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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