How to design an AirPlay speaker using Raspberry Pi Zero
Source: InternetPublisher:smallembedded Keywords: Speakers AirPlay Raspberry Pi Updated: 2024/06/07
This Pi Zero AirPlay speaker combines modern and classic styles.
background
While looking for a new project to build around the Raspberry Pi Zero, I came across the pHAT DAC from Pimoroni. This little add-on board adds audio playback capabilities to the Pi Zero. Because the pHAT uses the GPIO pins, the USB OTG port can still be used for a wifi dongle. Perfect for a small wireless speaker project, and thus the idea for this project was born!
hardware
This project is fairly simple and requires the following components:
Raspberry Pi Zero
pHAT DAC
Mono 2.5W Class D Amplifier
WiPi Wireless Dongle
Two 100 ohm resistors
Speaker (4-8 ohm)
The Raspberry Pi Zero is obviously the brains of the project and will be running the Shairport software to stream music wirelessly. The pHAT DAC is a neat little add-on board that adds audio to the Raspberry Pi. It has a jack output and you can add RCA connectors to it. The fact that the RCA connectors are not pre-soldered is a benefit as it leaves the audio wires exposed. Adafruit's small mono amplifier then takes the audio from the pHAT and amplifies it, playing the audio from the speakers. A wifi dongle connected via the USB OTG port provides wireless network connectivity for streaming.
I decided to make a mono speaker to keep the whole project not too bulky, making this project with stereo support would mean having a second speaker and replacing the mono amp with a stereo speaker.
I know this isn't the best way to convert stereo to mono, but it works. I tried to solve this from a software perspective by downmixing stereo to mono, but wasn't completely successful. If anyone has a tip on how to achieve this in an easy way, feel free to leave it in the comments!
software
In terms of software, the difficulty is about the same as that of hardware.
Start by downloading the latest Raspbian Jessie image from the official Raspberry Pi website.
Using "dd", I put the downloaded image on an 8Gb microSD card and then used it to boot the Pi Zero.
sudo diskutil list
sudo diskutil unmountDisk /dev/disk3
sudo dd if=Downloads/2015-11-21-raspbian-jessie.img of=/dev/disk3 bs=1m
sudo diskutil unmountDisk /dev/disk3
After booting, setup wifi in the graphical desktop environment by selecting the correct SSID and entering the wifi password. Once the Pi Zero is connected to the network, the software can be updated.
sudo apt-get update
sudo apt-get upgrade
Then it was time to install the project specific software: supporting pHAT DAC and AirPlay software.
pHAT DAC
There is a tutorial on how to install and use the pHAT DAC available on the Pimoroni website. However, my approach was slightly different in that I did not disable the default sound driver.
Device tree overlays are used to describe the hardware. Since the pHAT DAC uses the same hardware as the HiFi Berry, the same overlay can be used by appending the following line to the configuration file:
pi@raspberrypi:~ $ sudo nano /boot/config.txt
# pHAT DAC
dtoverlay=hifiberry-dac
After rebooting, I listed the audio device using the "aplay" application and it was: Card 1 - HiFi Berry.
pi@raspberrypi:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
To make this the default for audio playback, I updated the asound.conf file and replaced all references to "card 0" with "card 1".
pi@raspberrypi:~ $ sudo nano /etc/asound.conf
pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 1
}
Finally reboot to make sure everything is applied.
Shairport
Shairport is an Airtunes emulator that allows a compatible iOS device or iTunes to stream audio to the device running it.
Before you can install and run Shairport, there are a few dependencies that need to be met.
pi@raspberrypi:~ $ sudo apt-get install git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils libmodule-build-perl
pi@raspberrypi:~ $ git clone https://github.com/njh/perl-net-sdp.git perl-net-sdp
pi@raspberrypi:~ $ cd perl-net-sdp/
pi@raspberrypi:~/perl-net-sdp $ perl Build.PL
pi@raspberrypi:~/perl-net-sdp $ sudo ./Build
pi@raspberrypi:~/perl-net-sdp $ sudo ./Build test
pi@raspberrypi:~/perl-net-sdp $ sudo ./Build install
With the dependencies taken care of, you can install the actual Shairport software.
pi@raspberrypi:~ $ git clone https://github.com/hendrikw82/shairport.git
pi@raspberrypi:~ $ cd shairport/
pi@raspberrypi:~/shairport $ make
At this stage, you can test that everything has been installed correctly by running the shairport.pl script manually.
pi@raspberrypi:~/shairport $ ./shairport.pl -a AirPi
After you have verified that everything is working as expected, you can daemonize the shairport application so that it automatically starts at boot time.
pi@raspberrypi:~/shairport $ sudo make install
pi@raspberrypi:~/shairport $ sudo cp shairport.init.sample /etc/init.d/shairport
pi@raspberrypi:~/shairport $ sudo chmod +x /etc/init.d/shairport
pi@raspberrypi:~/shairport $ sudo update-rc.d shairport defaults
Finally, you need to modify the shairport file to specify the name of your AirPlay device. This can be anything you want. In my case, I chose something generic like "AirPi".
pi@raspberrypi:~/shairport $ sudo nano /etc/init.d/shairport
#DAEMON_ARGS="-w $PIDFILE"
DAEMON_ARGS="-w $PIDFILE -a AirPi"
Reboot your Pi. Hairport should start automatically.
shell
It's time to wrap up your working AirPlay speaker into something beautiful and make a nice case for it.
This was actually the hardest part of the project. Mainly because I wanted to make it out of wood, and the shape was a bit complicated. This meant doing some math before using the miter saw to cut the pieces to the correct length, and then making sure I cut the right angles so the pieces would connect correctly. Since I'm not a woodworker, and the tools I have access to aren't the most appropriate, the results aren't always as accurate as you'd expect.
The build was given some emphasis by adding 3D printed parts: the side panels and the speaker grilles. One of the side panels is not glued in place and can be removed if needed to allow access to the electronics. I hesitated about painting the 3D printed parts a different color to get a chrome or brass look, but ended up leaving the parts as is. Overall, it gives the build some funky edge, doesn't it?
This is what the project ended up looking like:
- SN75370 Dual MOS Memory Interface Circuit
- Interface circuit between MMA1220D and microcontroller
- How to design an AirPlay speaker using Raspberry Pi Zero
- Circuit design analysis of smart car image recognition system
- Experiment on using 51 microcontroller to drive relay
- Serial debugger circuit implemented with several discrete components
- Use microcontroller to make simple timing controller for electric fan
- PC infrared interface
- 815 motherboard
- Serial to infrared circuit
- Lost alarm circuit for the elderly
- Electronic multifunctional alarm circuit
- multi-tone alarm
- radio circuit
- A cost-effective active speaker 02
- Class S power amplifier 01
- Cheap and good-quality small audio set 01
- Application of integrated circuit TDA2822
- Overload protection circuit with 25Ω speaker
- Hi-Fi loudspeaker 3-way and 4-way crossovers