Tuto:Get analog audio(3.5mm Audio Jack) in Raspberry Pi Zero


17 posts
by David Melara » Tue Dec 01, 2015 5:27 am
Hi mates', I'm going to bring you a tutorial or "a way" to get analog audio in the new Raspberry Pi Zero (or any Raspi you want).

Introduction
Few days ago, I went crazy when I saw the news about the zero version of RPi, I thought "hey, this is going to have a lot of potential", I rapidly became sad when I discovered that there weren't any analog audio or video in the board (I mean RCA and 3.5mm), but my hopes for a solution were too strong to throw the towel soon, so I googled a lot and found a solution. There are two unpopulated pins on the board for RCA, Yeah!, it's a matter of soldering two cables.... but still nothing for audio :( (lets talk truth, if I need to make a robot or anything that implements audio output, is too expensive to buy a HDMI to Analog Audio converter) so, I kept looking a way to archive this, and TA-DA!

Information
At
https://learn.adafruit.com/introducing-the-raspberry-pi-zero/audio-outputs
says this:
all you need is access to PWM0_OUT and PWM1_OUT, which are...on GPIO #40 and #45 and are not brought out on the Pi Zero. Tragedy? Give up? No! You can get to PWM0 on GPIO #18 (ALT5) and PWM1 on GPIO #13 (ALT0) or GPIO #19 (ALT5)

So... I have a PWM unfiltered audio source, what should I do? EASY! you can do a RC filter with one resistor and two capacitors.
This is the original audio filter in Raspi Boards.
Image

Note that PWM0 is the Right Audio Source and PWM1 is the Left Audio Source.
Here is a much simpler RC audio filter circuit, this is the one I tried.
Image


Getting access to PWM0 and PWM1
Take a look at this link, especially GPIO 18,13 and 19.
http://elinux.org/RPi_BCM2835_GPIOs

You can see that GPIO18 is present in the old 26 pin headers and 40 pin headers in the BOARD pin 12, this are good news.
But... is you see GPIO13 and 19, you can note that these pins where not present in the old 26 pin header, instead, become present in the new 40 pin header (guess what? Raspi Zero has this connector!!) so you can easily use GPIO13 in BOARD pin 33, or GPIO19 in BOARD pin 35, these are better news than before, we have access to PWM0 and PWM1!!
But the hard part is coming now... if we saw the Elinux link, we can see that PWM0 is the Alternative Function Number 5 of GPIO18, PWM1 is in the Alternative Function Number 0 in GPIO13 and Alternative Function Number 5 in GPIO19.

What is an Alternative Function?
GPIO pins of the Raspi can be used as input, output, pwm, I2c, etc...but, how can you switch a GPIO between its diferent functions? many people do it in C++ compiled programs, or changing the "device tree overlay", I found this to be a time consumption duty (especially those people new to electronics and programming), so I must thank two developers out there, those two who made "Wiringpi" and "PIGPIO" that give us the easy of "one line command" that changes the function of the GPIO. I used Wiringpi, im not going to cover PIGS this time..

Changing GPIO Function using WiringPI
To do this, first we need to install Wiringpi if you don't have it.

sudo apt-get update
sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
(if cant compile, be sure you have the build-essential package)

Now is time to change those GPIO functions!
Now go to command line, you dont need to be root user!
gpio -g mode 18 ALT5 (This links PWM0 to GPIO18)
gpio -g mode 13 ALT0 (This if you want PWM1 in GPIO13)
or
gpio -g mode 19 ALT5 (This if you want PWM1 in GPIO19)

and that's it!

Now, connect the GPIO18 to the right audio filter.
connect the GPIO13 or GPIO19 (the one you select before) to left audio filter.
Be sure everything is fine and ground is properly connected between Raspi, the audio filter and the speaker!!.
This is the 40 pin header pinout, look for the GPIOs we are going to use, be careful, dont make a short circuit!.
Image


Now the real test!
Go to command line, download a mp3 audio test file and play it.
wget https://goo.gl/XJuOUW -O example.mp3 --no-check-certificate
omxplayer example.mp3

If you hear a singing voice you made it! if don't, be sure to have omxplayer and be sure that your distro is configured for analog audio output, you can try in a normal Raspi with jack, if no luck, go to bibliography and go to raspberry audio usage link.
Now you can solder the RCA pins and have Analog Audio and Video!
I hope this help you out there wishing to extract all the juice from this new board, have a nice day!

Some problems and details i must tell
1) I tried this in Raspi 1 B rev.2 using the new Jessie distro.

2) My first try was in a OSMC for Raspi 1 B rev.2, i used PIGS and Wiringpi, but when I switched the alt. function, the system got stuck, I made many tries but no luck, maybe is the distro used to build the OSMC? is weird because is Jessie too... maybe someone can help me solve this, I dont have a Pi zero right now, im planning using it for Mini Media Center, but this problem stops my beloved idea :(
Edit: After pulling all my hair from the head, I discovered the reason why OSMC crashed when switching alt. functions, OSMC uses LIRC (a kernel module that allows you to connect a infrared receiver to your raspi, so you can use a remote control to drive the interface), the problem was that GPIO18 were configured as the receiver pin, thats why the Raspi crash when changing alt. func. The solution is either disable IR REMOTE or change te GPIO number used for the receiver.

3) I have only tried PWM0 in GPIO18, because is a 26 pin header, maybe it works too on Pi 2?
EDIT: I tested it in Raspi 2, both PWM0 and PWM1 works like a charm, i think the original filter would sound better....
Edit: Yep! the original filter sounds a lot better, is up to you the choice of filter.

4) I speak spanish, so please bear my english, I will try to improve this tutorial in that meaning.

5) Have fun hacking :D


Bibliography
If you love doing something, be sure that something will be done right
User avatar
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
by Dan Lavin » Thu Dec 03, 2015 4:51 pm
Good job! Muy bien!
Posts: 10
Joined: Wed Oct 23, 2013 7:36 pm
by karrika » Thu Dec 03, 2015 5:30 pm
Nice tutorial. One question. What about GPIO 12. It is also labeled as PWM0 but nobody mention it on Adafruit pages.
User avatar
Posts: 848
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland
by David Melara » Thu Dec 03, 2015 6:11 pm
Thanks Dan Lavin!
And Karrika, you are right, GPIO12 is available in the 40 pin connector, I haven't tried using it for PWM0, why don't you test it and tell us what happens?
karrika wrote:Nice tutorial. One question. What about GPIO 12. It is also labeled as PWM0 but nobody mention it on Adafruit pages.
If you love doing something, be sure that something will be done right
User avatar
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
by asidow » Mon Dec 07, 2015 2:51 am
I really appreciate taking the time putting togethor this GREAT tutorial
could you clarify if you recommending to use the simpler RC audio filter circuit? if yes, where does it connect the MCU (PD3) and MCU (PB3)?

My plan is to use 3.5 audio jack for rasp zero is that what you tried and worked for you?


filter.png
filter.png (34.59 KiB) Viewed 10673 times

Posts: 3
Joined: Mon Dec 07, 2015 2:40 am
by David Melara » Mon Dec 07, 2015 7:50 am
asidow wrote:I really appreciate taking the time putting togethor this GREAT tutorial
could you clarify if you recommending to use the simpler RC audio filter circuit? if yes, where does it connect the MCU (PD3) and MCU (PB3)?

My plan is to use 3.5 audio jack for rasp zero is that what you tried and worked for you?


filter.png

Thanks for your comment, let me clarify you the connection.
PWM0 is the right audio channel.
PWM1 is the left audio channel.
The image I used is a example, but it can be taken as a reference, MCU PD3 is the left channel, PWM1
MCU PB3 is the right channel, PWM0
Is up to you the choice of GPIO for either PWM0 or PWM1.
I recommend using the original audio filter, it gives you better quality audio than the simple one, the only problem is that it uses 1 more resistor, and the values for the components are not so easy to get (unless you have luck or buy online :D).
I recommend a lot using the four diodes shown in the original audio filter, they are called "over and under voltage protection", unless you are completely sure that there is never going to be more than 3.3V or a negative voltage in the 3.5mm audio jack (accidents happen everytime) then is safe to ignore them, and Yes! The jack connector works like a charm.
If you love doing something, be sure that something will be done right
User avatar
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
by karrika » Thu Dec 10, 2015 10:59 am
A comment about the protection diodes. What do they protect? They do not connect to the GPIO pins at all. There is a capacitor in series between the audio jack and the GPIO pins. So imho the overvoltage will not find its was to the GPIO pins.
User avatar
Posts: 848
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland
by mahjongg » Thu Dec 10, 2015 11:17 am
No you are mistaken, a sudden voltage jump to say 5V WILL end up on the GPIO, and will cause disaster (a latch-up) there.
That is why there are protection diodes, to make sure you cannot enter a voltage that will cause a latch-up for example with static electricity, or when you plug it into a device that has a substantial potential difference with the PI.
User avatar
Forum Moderator
Forum Moderator
Posts: 8783
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands
by carriba » Thu Dec 10, 2015 2:05 pm
Looks very good to me, worth giving a try during my next spare time :P

Excellent job :!:
User avatar
Posts: 64
Joined: Tue Nov 18, 2014 5:55 pm
by David Melara » Thu Dec 10, 2015 5:14 pm
Thanks fo your comments.
Remember, a capacitor act as a conductor in that small time (5 tau) before charging up, so the voltage reaches the GPIO and when charged it will discharge itself with the resistor near him, going through the GPIO, damaging it.
If you love doing something, be sure that something will be done right
User avatar
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
by asidow » Thu Dec 10, 2015 5:42 pm
I'm having issues getting the exact parts, so could you clarify based on your simple circuit do you get acceptable audio out to your speaker?
Posts: 3
Joined: Mon Dec 07, 2015 2:40 am
by Douglas6 » Thu Dec 10, 2015 6:22 pm
Lady Ada has a tutorial for this on her learning system: https://learn.adafruit.com/adding-basic ... ry-pi-zero
User avatar
Posts: 3588
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by David Melara » Thu Dec 10, 2015 6:37 pm
I have acceptable audio with the simple one, try it in a breadboard, if you dont like it, you can easily test other values easy to get for you until you reach an acceptable quality, lady ada post explains how to get the cuttof frequency, you can test another values using that formula. Or in android use the electrodroid app, it has a cuttof frequency calculator for Low Pass circuits.
If you love doing something, be sure that something will be done right
User avatar
Posts: 26
Joined: Sun Jul 13, 2014 8:32 pm
Location: El Salvador
by karrika » Sat Dec 12, 2015 9:49 am
mahjongg wrote:No you are mistaken, a sudden voltage jump to say 5V WILL end up on the GPIO, and will cause disaster (a latch-up) there.
That is why there are protection diodes, to make sure you cannot enter a voltage that will cause a latch-up for example with static electricity, or when you plug it into a device that has a substantial potential difference with the PI.


Thank you for the correction. I will add the diodes to my DiscoCap board (for PiZero).
User avatar
Posts: 848
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland
by asidow » Sat Dec 12, 2015 5:29 pm
does anyone know when pizero will be out?
Posts: 3
Joined: Mon Dec 07, 2015 2:40 am
by Douglas6 » Sat Dec 12, 2015 5:33 pm
asidow wrote:does anyone know when pizero will be out?
The Pi Zero is "out"; I have one myself. It's also sold out at most locations. Keep trying, they're making more.
User avatar
Posts: 3588
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by obscene » Fri Apr 15, 2016 10:53 pm
[Spanish]
Hola, antes que nada gracias por el tiempo para hacer este tutorial.
He seguido una guia de Adafruit: https://learn.adafruit.com/adding-basic ... -pwm-audio
Sin embargo aun no logro obtener el audio por el jack. Cuando intento escuchar un audio siempre me arroja el siguiente mensaje:
Code: Select all
pi@raspberry: ~$aplay  /usr/share/sounds/alsa/Front_Center.wav
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1239: Channels count non available

¿Qué puede estar mal?

[English]
Hi!, thank you for your time in this tuto.Hola, antes que nada gracias por el tiempo ha hacer el tutorial.
He seguido el tutorial de adafruit: https://learn.adafruit.com/adding-basic ... -pwm-audio
Sin embargo aun no logro obtener el audio por el jack. Cuando intento escuchar un audio, siempre me arroja el siguiente mensaje:
I have follow adafruit tuto: https://learn.adafruit.com/adding-basic ... -pwm-audio
But I don't get the audio. When I try to listen a track audio, the terminal allways showme the next message:
Code: Select all
pi@raspberry: ~$aplay  /usr/share/sounds/alsa/Front_Center.wav
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1239: Channels count non available

So, what is wrong?

PD: My apologies for my bad English.
Posts: 1
Joined: Fri Apr 15, 2016 10:43 pm