PWM Audio Guide

Various user-contributed guides for hardware-related things
uke
Posts: 41
Joined: Mon May 09, 2016 9:05 am
Has thanked: 2 times
Been thanked: 6 times

Re: PWM Audio Guide

Post by uke » Mon Aug 29, 2016 12:47 pm

RxBrad wrote:So, I built the stereo circuit. I've been scouring the forum, and for life of me, I can't figure out how I'd wire up the circuit, pot, and headphone jack so I get stereo output through headphones, and the left/right channels are COMBINED through the speaker. While MOST games are mono, as previously mentioned, some are stereo (e.g. Sonic ring collection sound effects). If I only wired one channel to the speaker, some sounds wouldn't play.

Can anyone point me in the right direction.

EDIT 3: I believe that all of the stuff below applies to pre-amped signals, and those resistor values would make the audio from the speaker unusably low. I'm also guessing that the higher resistance across the active signal and ground are already accomplished through the pot. The actual answer appears to be in the diagrams here.

EDIT: Figure 2 on this page looks promising. The input would be coming from the headphone jack, and the output going into the speaker. Looking around, I've seen other similar diagrams that recommend 1k resistors in place of the 475ohm resistors. I'm just not 100% sure how to interpret the diagram. Is the bottom line the ground? (EDIT 2: yes it is .. S = shield/ground)

Image
Can anyone help with this? I'm also facing the same problem. Thanks!

User avatar
RxBrad
Posts: 267
Joined: Fri Jul 22, 2016 9:10 am
Has thanked: 121 times
Been thanked: 149 times
Contact:

Re: PWM Audio Guide

Post by RxBrad » Mon Aug 29, 2016 1:11 pm

uke wrote:Can anyone help with this? I'm also facing the same problem. Thanks!
See the link in red from my post that you quoted.

You need to join the L-out and R-out from your headphone jack, then wire that into the positive terminal of your speaker. Before you join L-out and R-out, however, you need to connect a 10 ohm resistor to each wire. You can try higher ohm resistors if you don't have 10 ohm, but the higher you go, the quieter the speaker output gets.

The resistors help prevent electricity from looping back into the amplifier and killing it. They also keep the sound from being a garbled mess.

I haven't tested it yet, but here's what I think needs to be done...

This is all assuming your amp has separate L and R outputs, like the PAM8403:

L & R out on the PAM8403 do not share a common ground. So don't combine the L & R negative terminals. Based on this info, it sounds like you should do this (I'm only about 50% certain on this part):
  • EDIT: The two 10uF caps below didn't seem to do the trick; and there's lots of noise from SD activity on the Pi. Also, I'm finding a lot of resources that say the pot should actually go between the PWM circuit and the amplifier. I imagine this would help with the issue of only getting sound once the audio is turned almost all the way up. I'll revise this once I get clean audio figured out.
  • Amp L+ Out to 10uF cap+, 10uF cap- to potentiometer L-in
  • Amp R+ Out to 10uF cap+, 10uF cap- to potentiometer R-in
  • Amp R- Out (leave empty)
  • Amp L- Out (leave empty)
  • Amp IN Ground to Potentiometer Ground
  • Potentiometer L-out to Headphone Jack L-In
  • Potentiometer R-out to Headphone Jack R-In
  • Potentiometer Ground to Headphone Jack Ground
  • Headphone Jack Ground to Speaker Negative terminal
  • Join Headphone Jack R-out and L-out with resistors as shown below, then attach to speaker Positive terminal
Image
Last edited by RxBrad on Fri Sep 02, 2016 11:06 am, edited 2 times in total.

uke
Posts: 41
Joined: Mon May 09, 2016 9:05 am
Has thanked: 2 times
Been thanked: 6 times

Re: PWM Audio Guide

Post by uke » Mon Aug 29, 2016 3:41 pm

RxBrad wrote:
uke wrote:Can anyone help with this? I'm also facing the same problem. Thanks!
See the link in red from my post that you quoted.

You need to join the L-out and R-out from your headphone jack, then wire that into the positive terminal of your speaker. Before you join L-out and R-out, however, you need to connect a 10 ohm resistor to each wire. You can try higher ohm resistors if you don't have 10 ohm, but the higher you go, the quieter the speaker output gets.

The resistors help prevent electricity from looping back into the amplifier and killing it. They also keep the sound from being a garbled mess.

I haven't tested it yet, but here's what I think needs to be done...

This is all assuming your amp has separate L and R outputs, like the PAM8403:

L & R out on the PAM8403 do not share a common ground. So don't combine the L & R negative terminals. Based on this info, it sounds like you should do this (I'm only about 50% certain on this part):
  • Amp L+ Out to 10uF cap+, 10uF cap- to potentiometer L-in
  • Amp R+ Out to 10uF cap+, 10uF cap- to potentiometer R-in
  • Amp R- Out to potentiometer Ground
  • Amp L- Out (leave empty)
  • Potentiometer L-out to Headphone Jack L-In
  • Potentiometer R-out to Headphone Jack R-In
  • Potentiometer Ground to Headphone Jack Ground
  • Headphone Jack Ground to Speaker Negative terminal
  • Join Headphone Jack R-out and L-out with resistors as shown below, then attach to speaker Positive terminal
Image
Oh, I have the stantard Adafruit Mono 2.5W Class D Audio Amplifier - PAM8302, so is stereo sound out of jack and mono out of speaker not doable?

User avatar
RxBrad
Posts: 267
Joined: Fri Jul 22, 2016 9:10 am
Has thanked: 121 times
Been thanked: 149 times
Contact:

Re: PWM Audio Guide

Post by RxBrad » Mon Aug 29, 2016 3:53 pm

@uke That amp is for mono output only.

User avatar
rodocop
Posts: 1641
Joined: Mon Aug 22, 2016 3:14 pm
Location: Saskatchewan
Has thanked: 596 times
Been thanked: 583 times

Re: PWM Audio Guide

Post by rodocop » Mon Sep 19, 2016 11:53 am

Quick question, in the guide it mentions that I should use 150 OHM 1/4W resistors, I have some 150 OHM 1W resistors, as well as some 130 OHM 1/4W resistors. I'm new to this kind of project and I am not sure if one would be better than the other? Will the 20 OHM difference make a difference, will one be better to use?

Also, I have some 10uF 63V 105C Radial Electrolytic Capacitors, will these work in place on the 25V ones listed in the guide?

I have all the other parts available, and i'm hoping these two different resistors/capacitors will work. Thanks to anyone who might have some input.

User avatar
Camble
Posts: 881
Joined: Thu May 05, 2016 2:31 am
Location: Scotland
Has thanked: 269 times
Been thanked: 487 times

Re: PWM Audio Guide

Post by Camble » Mon Sep 19, 2016 5:11 pm

@rodocop Use this to work out which values to use, or in your case, what frequency the combination of resistor and capacitor will cut off. You want your filter to cut off frequencies above ~18KHz.

Tim
Posts: 23
Joined: Sun Sep 04, 2016 5:24 am
Been thanked: 3 times

Re: PWM Audio Guide

Post by Tim » Sat Sep 24, 2016 4:08 am

Currently my Audio setup is like this:

Pi---StereoLowpassfilter---2x2.2kohm resistors for mono---Volume pot---headphone jack---pam8403---speaker

I get Audio out of the Jack and the speaker but both are barely hearable. I saw that Most of you use the amplifier also for the headphone Jack but are 3w really good for headphones? Is my problem that i am using too large resistor values?

User avatar
Camble
Posts: 881
Joined: Thu May 05, 2016 2:31 am
Location: Scotland
Has thanked: 269 times
Been thanked: 487 times

Re: PWM Audio Guide

Post by Camble » Sat Sep 24, 2016 4:52 am

@Tim The audio from my headphones was plenty loud enough without the amp. Have you tried made sure the volume is turned up in alsamixer?

User avatar
RxBrad
Posts: 267
Joined: Fri Jul 22, 2016 9:10 am
Has thanked: 121 times
Been thanked: 149 times
Contact:

Re: PWM Audio Guide

Post by RxBrad » Sat Sep 24, 2016 7:13 am

@Tim

If you're doing mono output on both headphones and speaker, you shouldn't need to combine channels with resistors (and yes, those 2.2k resistor are a high enough value to kill a lot of the sound if you do -- most people have used 4 to 20 ohms, and you're using 2200 ohms).

Instead of the dtoverlay in the first post, use this in your config.txt to enable mono sound:

Code: Select all

dtoverlay=pwm,pin=13,func=4
And you only need half of the PWM circuit listed in the first post to get mono sound. Just run it all through the Left channel on the PAM8403:

Image

Tim
Posts: 23
Joined: Sun Sep 04, 2016 5:24 am
Been thanked: 3 times

Re: PWM Audio Guide

Post by Tim » Sun Sep 25, 2016 4:50 am

I got the stereo lowpassfilter because I wanted to do stereo first but the chinese only sent me mono volume wheels so I bridged the two channels together. If I configure mono sound on the raspberry do i loose the sound of one channel?

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests