My first big disappointment with the Raspberry Pi was the audio output from the headphone jack. When I built my first dawn clock, using an Arduino, I had to buy a separate shield for audio output, and even that was severely limited. With the RPi, I thought, life will be easy because all the audio stuff I need is built-in. Didn’t turn out that way.
In my innocence, I tried hooking up a small speaker directly to the headphone jack. The output was barely audible. Then I did some reading on the RPi forum and found many complaints about just how bad this output is. No only is it weak, requiring amplification, but it’s also subject to loud noises whenever you start and stop playing. (This problem has, allegedly, been reduced with the latest Raspbian builds, but it still seemed pretty bad to me.)
Then I found this topic on the RPi forum and resolved that what I needed was an external, USB sound card. The one suggested in the thread, and which I subsequently bought, is this little beauty from China.
The only problem with this board is that it has a nice, bright LED on board to tell you it’s working. As mentioned in my previous post, this is something of a disadvantage for this project, so out with the black gaffer tape again. That’s why the board is unrecognisable in the picture at the bottom of my previous post.
While it boasts optical output, I was still planning to use the headphone jack, which meant that I still needed some kind of amplifier.
It just so happened that I had a pair of Logitech powered PC speakers hanging about. I ripped them apart (they were glued rather than screwed in key places) and removed the amp board. It was a bit of a squeeze getting it into the dawn clock, but offered the advantage of having a volume knob that, through the judicious drilling of a hole, I could access from the outside.
I had an inkling that all was not well when I heard that dreadful morse-like stuttering you get when your cellphone interferes with audio gear. Oh well, I thought, I’ll just have to remember not to take my cellphone into the bedroom.
But when I set up the dawn clock in the bedroom I found that the Logitech’s susceptibility to RF interference made it unfit for purpose. Something in that room (I suspect an old electric radiator) was bleeding RF noise like a slaughtered pig. (Yes, pigs do emit RF.)
So, I still needed an amplifier. A bit more searching and I came up with this 3W mini-amplifier board (again from Aliexpress in China).
When they said ‘mini’ they weren’t kidding. The actual thing is almost certainly smaller than the image you’re looking at, unless you’re reading this on your phone. The holes for making connections are so tiny, and so close together, that I struggled to find wire from my stock that would work. So soldering was tricky. Also, I wanted to use standard 0.1″ headers for connecting the speakers. In the end, I put the headers on a small proto board and – because I didn’t have bolts thin enough to go through the amp board’s mounting notches – glued the amp to the proto board. It ain’t pretty, but it works.
The input cable from the amp runs to a 3.5mm stereo jack that plugs into the headphone socket of the audio card. The audio card plugs into the USB port of the RPi. I connected two 4 Ohm speakers to the headers on the proto board and … it worked!
The only issue with this set-up is that there’s no hardware volume control. Maybe I could have been clever with some pots on the proto board (that’s why I left space – in case I needed them). But in my blithely optimistic way, I assumed that I would be able to control volume programmatically, from software. Turned out I was right.
I’ll deal with that side of things more in the next post, on software. But as it’s relevant here, I’ll just give a flavour of the software used:
- For playing MP3s (ambient sound and music) I use mpg123. [UPDATE: see below]
- For playing the radio stream, I use mplayer.
- For setting the volume for either, I use amixer.
All are called from within the Python script, of which more later…
[UPDATE: 21/01/2013] I’m now using mplayer for MP3s too – see Part 4 linked to below. Also, after the audio is played and the dawn clock is silent again, I’m getting a distinct and annoying whine, which I think is coming from the cheapo amplifier board. The only solution I’ve so far found for this is … wait for it …
Turn it off and back on again.
I’ve ordered a different amplifier board, so we’ll see if that solves the problem.