mkende
Posts: 7
Joined: Sun Apr 03, 2016 10:23 pm

Configuring audio out to use only one PWM port

Sun May 08, 2016 10:47 pm

Hi all,

I was wondering if it is possible to have the RPi audio output (when using the jack) use only one of the PWM channels (e.g. PWM1 which correspond to the left channel), and let the other be used by another application.

Currently, if I do

Code: Select all

gpio -g mode 18 pwm
to use the PWM0 port on GPIO 18, then any attempt to output a sound (e.g. with aplay) will first block for a few seconds and then output:

Code: Select all

aplay: pcm_write:1939: write error: Input/output error
.

I did not find any option for the bcm2835 driver but maybe I missed something, or maybe it's possible to patch the driver to achieve this?

(I'm mainly interested by a solution for the RPi Zero, where I'd like a mono audio output on GPIO 19, and be able to use GPIO 18 for something else, but I guess that a solution would work for any RPi).

Thanks!

darkbibble
Posts: 622
Joined: Mon Mar 09, 2015 5:20 pm
Location: corby, england

Re: Configuring audio out to use only one PWM port

Mon May 09, 2016 2:14 pm

here is a tutorial for setting up audio output on the pi zero; https://learn.adafruit.com/adding-basic ... -pwm-audio
it should be easy to adjust it for mono output
Q; How many Windows users does it take to fix a Linux problem??
A; Whats a Linux problem

mkende
Posts: 7
Joined: Sun Apr 03, 2016 10:23 pm

Re: Configuring audio out to use only one PWM port

Mon May 09, 2016 2:25 pm

Thanks, that's indeed the first thing that I looked at. But even in mono output, the driver still uses both PWM channels (I guess that it just outputs the same thing on both).

darkbibble
Posts: 622
Joined: Mon Mar 09, 2015 5:20 pm
Location: corby, england

Re: Configuring audio out to use only one PWM port

Mon May 09, 2016 2:39 pm

on the setup I'm building I'm using both mono and stereo, mono for an internal speaker from the right channel (with mini mono amp) and stereo for headphone output
Q; How many Windows users does it take to fix a Linux problem??
A; Whats a Linux problem

mkende
Posts: 7
Joined: Sun Apr 03, 2016 10:23 pm

Re: Configuring audio out to use only one PWM port

Mon May 09, 2016 2:48 pm

And can you use the other PWM0 output for another purpose when you are in mono output? If so I'm interested by the command that you're using to set the audio output to mono.

darkbibble
Posts: 622
Joined: Mon Mar 09, 2015 5:20 pm
Location: corby, england

Re: Configuring audio out to use only one PWM port

Mon May 09, 2016 2:54 pm

I haven't started on the code yet, still working on wiring and soldering
Q; How many Windows users does it take to fix a Linux problem??
A; Whats a Linux problem

shibus
Posts: 4
Joined: Sat Aug 25, 2018 11:20 pm

Re: Configuring audio out to use only one PWM port

Tue Sep 04, 2018 7:33 am

Has anyone succeeded this?
I want to use one PWM for LED control and the other PWM for monaural audio.

mkende
Posts: 7
Joined: Sun Apr 03, 2016 10:23 pm

Re: Configuring audio out to use only one PWM port

Tue Sep 04, 2018 9:27 pm

For my part, I never found a solution, but I haven't looked into this in the last two years. So maybe the audio driver has improved and there is now an option to control this behaviour. Be sure to report here if you find something.

remistonge
Posts: 2
Joined: Wed Jul 18, 2018 3:24 pm

Re: Configuring audio out to use only one PWM port

Thu Nov 08, 2018 3:37 pm

I am having the exact same problem! Trying to use a mono PWM for audio and the second PWM for an other purpose. Still no solution around...

semih_sky
Posts: 51
Joined: Tue May 08, 2018 12:26 pm
Location: Turkey

Re: Configuring audio out to use only one PWM port

Wed Oct 14, 2020 11:36 am

Hi,

Is there a way to output only mono audio from RPi Zero W via using only one pin?

I'll use this mono output just for word reading audio files, not for music. There is no need to use stereo or high quality audio.

Is this a solution?

https://www.instructables.com/Breadboard-RetroPie/

GPIO 18 -> 33nf capacitor -> breadboard GND
GPIO 18 -> 10uf capicitor -> NPN transistor base pin
NPN transistor emmitter pin -> breadboard GND
NPN transistor collector -> speaker -> breadboard 5 V

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2474
Joined: Thu Jul 11, 2013 2:37 pm

Re: Configuring audio out to use only one PWM port

Wed Oct 14, 2020 1:46 pm

The firmware analogue audio driver will always configure both PWM channels. This isn't going to change.

If you want "mono" output, then open the ALSA sink with 1 audio channel configured in your PCM format - this will cause it to do "dual mono" output on both PWM channels, and you can ignore the one you don't need.
Rockets are loud.
https://astro-pi.org

semih_sky
Posts: 51
Joined: Tue May 08, 2018 12:26 pm
Location: Turkey

Re: Configuring audio out to use only one PWM port

Wed Oct 14, 2020 2:03 pm

I must use only one PWM pin for audio, I've ran out of pins mono is enough for me. If OS won't provide mono audio with only one pin, nothing left to do :(

Thanks.

User avatar
rpdom
Posts: 17973
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Configuring audio out to use only one PWM port

Wed Oct 14, 2020 4:11 pm

Unless you need the other PWM channel for something else, then you can just ignore it. You only need to configure the one gpio pin for PWM output.
Unreadable squiggle

semih_sky
Posts: 51
Joined: Tue May 08, 2018 12:26 pm
Location: Turkey

Re: Configuring audio out to use only one PWM port

Thu Oct 22, 2020 6:14 pm

Should I understand this that I can use only one PWM pin to get mono audio and use other PWM pin for something else like controlling a motor's speed at the same time?

trejan
Posts: 3062
Joined: Tue Jul 02, 2019 2:28 pm

Re: Configuring audio out to use only one PWM port

Thu Oct 22, 2020 6:59 pm

semih_sky wrote:
Thu Oct 22, 2020 6:14 pm
Should I understand this that I can use only one PWM pin to get mono audio and use other PWM pin for something else like controlling a motor's speed at the same time?
No. You can't split it like that with the two PWM channels doing different things.

Return to “Advanced users”