Page 1 of 8

PWM Audio Guide

Posted: Sun Jun 05, 2016 11:15 pm
by SP33
This guide covers the basics of PWM audio on the Raspberry Pi Zero
It was based off of this article
https://learn.adafruit.com/adding-basic ... o/overview

Parts Needed:
10UF ELECTROLYTIC CAPACITOR ( x2 for stereo)
0.033UF MONOLITHIC CERAMIC CAPACITOR ( x2 for stereo)
150 OHM RESISTOR ( x2 for stereo)
270 OHM RESISTOR ( x2 for stereo)

I ordered my parts from Tayda Electronics you can click on the links above to see the exact parts I ordered. Keep in mind you must have $5 in your cart to be able to order from them but they are a very good company. You may buy from your favorite electronics site and just use the links for reference as well. There are also slightly varying parts you could use.

Schematic:
https://learn.adafruit.com/assets/28851

Simplified schematic:
Image

Note: The orientation of 270R, 150R and 33N do not matter.
10U Must be oriented correctly, the negative pin is usually denoted by a dashed line on the side of the capacitor and a shorter post.
If you want only left or right channel audio just split the schematic at the ground and only use one side.

My implementation:
Image

Software End:
Add the following text to the end of /boot/config.txt and reboot your pi

Code: Select all

dtoverlay=pwm-2chan,pin=18,func=2,pin2=13,func2=4
You should be able to cut out the part you don't need if you are using only the left or right channel but I will not provide it since it varies based on which you choose.
Thanks to @tindalos for this method

You should now have audio! If you have any questions about anything in the guide or concerns with how I implemented it please let me know!

Re: PWM Audio Guide

Posted: Sun Jun 05, 2016 11:48 pm
by wermy
Hey thanks! I'll be covering this in my next guide, I'll definitely point people here as well. :)

Re: PWM Audio Guide

Posted: Sun Jun 05, 2016 11:52 pm
by Goose_box
This is awesome! Thank you so much for this. Will give it a try tomorrow 8-)

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 11:50 am
by Bubble
Nice job ! Original schematic was really hard to understand.
This is crystal clear :)

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 2:11 pm
by Popcorn
I just finished a mono version of this circuit and it wasn't working until I reversed the GPIO and GND pins going into the circuit. Are we sure the 10uF Capacitor is going the right way around?

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 2:34 pm
by DirtyBullets
dam i don't have 33n's got i have 47n's but to far apart i think?

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 5:51 pm
by SP33
wermy wrote:Hey thanks! I'll be covering this in my next guide, I'll definitely point people here as well. :)
Sounds great, let me know if anything should be changed to help streamline everything!
Popcorn wrote:I just finished a mono version of this circuit and it wasn't working until I reversed the GPIO and GND pins going into the circuit. Are we sure the 10uF Capacitor is going the right way around?
I am fairly certain this is correct, 1461748123's schematic shows the same orientation and I checked around google before I wired everything up. You are saying the whole thing works backwards? Feel free to show what you have so we can figure it out.
DirtyBullets wrote:dam i don't have 33n's got i have 47n's but to far apart i think?
I'm not sure, you might be able to use them but at cost of audio but I honestly don't know for sure. I do know that adafruit used 10nF instead of 33nF.

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 7:11 pm
by Popcorn
SP33 wrote:
Popcorn wrote:I just finished a mono version of this circuit and it wasn't working until I reversed the GPIO and GND pins going into the circuit. Are we sure the 10uF Capacitor is going the right way around?
I am fairly certain this is correct, 1461748123's schematic shows the same orientation and I checked around google before I wired everything up. You are saying the whole thing works backwards? Feel free to show what you have so we can figure it out.
Ok, I minimized my board so I need to provide a diagram but it's exactly the same as what is posted, except that instead of extending out, I just route underneath the resistors that are going overhead to conserve space on the unused terminals that are underneath. I mapped it out in a spreadsheet so you understand what's happening. Basically, because I'm using a one-sided prototype board, I placed the 10uf electrolytic capacitor underneath the monolithic ceramic capacitor to conserve even more space. So, A1 and A4 are the inputs. And D4 and A3 are the outputs. A3 is part of a common ground, just like the diagrams. That goes to Audio - and D4 goes to Audio +. So when I connect A1 to GPIO13 (physical pin 33) and when I connect A4 to GND (physical pin 34), I get nothing. When I put GPIO13 to A4 and GND to A1, I get full and loud audio through my amp.

I did the spreadsheet so, if the coloured cells are touching the next cell, then they are connected irl.

Here's the spreadsheet:

Image/

Here's the top side of the board (yes, bad lighting, I can take another photo tomorrow during the day, it's 3am here!). Don't forget this is inverted. The spreadsheet matches the front-side of this board where Row 4 is Row 1. Flip it horitonally in your head.

Image

Here's the back side which matches the spreadsheet.

Image

I multimetered this and it's fine and also, as I said before, it doesn't work as it is, but as soon as I map GPIO13 to A4 an GND to A1, then suddenly the audio works and it's really loud and clear. Any ideas?

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 8:08 pm
by Popcorn
So, this is interesting. I'm bypassing the circuit completely. I'm going GPIO13 to Audio + on my Audio Amp. An Audio - is going to GND. And the audio is totally clean. (With the circuit, there was a little bit of 60hz hum). So, do we even need the low pass filter? It's not as though we are even going to hear anything above 10kHz on that tiny speaker anyway.

Re: PWM Audio Guide

Posted: Mon Jun 06, 2016 8:32 pm
by SP33
Popcorn wrote:So, this is interesting. I'm bypassing the circuit completely. I'm going GPIO13 to Audio + on my Audio Amp. An Audio - is going to GND. And the audio is totally clean. (With the circuit, there was a little bit of 60hz hum). So, do we even need the low pass filter? It's not as though we are even going to hear anything above 10kHz on that tiny speaker anyway.
I'm not knowledgeable enough on the subject to say for sure but I believe it is just there to keep any low frequencies out and output only what we want to hear. So you might get feedback without it if other electronics are emitting a certain frequency.

As far as your circuit, I don't think it matched what you have in the spreadsheet. Correct me if I'm wrong but it looks like your 270R doesn't connect directly next to the 10uF but before the 150R and 10N, you would have to move the 10uF down to the bottom in your circuit to match the spreadsheet.