Connecting a Bluetooth speaker to a Raspberry Pi Zero W running Raspian Stretch Lite. Using a DW-CT14+ Bluetooth amplifier module and speaker with the Zero W for text to speech synthesis.
You can use this as part of another project if you want the Pi to ‘talk’.
If you already have a PI Zero W with Stretch Lite installed and you are familiar with the shell the following steps should enable Bluetooth audio. You might need to remove pulseaudio first if you have that installed.
- sudo apt-get update && sudo apt-get dist-upgrade
- sudo apt-get install bluealsa
- sudo service bluealsa start
- Switch on your bluetooth device
- sudo bluetoothctl
- scan on
- pair XX:XX:XX:XX:XX:XX (replace the XXXXX with your device ID)
- trust XX:XX:XX:XX:XX:XX
- connect XX:XX:XX:XX:XX:XX
- aplay -D bluealsa:HCI=hci0,DEV=XX:XX:XX:XX:XX:XX,PROFILE=a2dp /usr/share/sounds/alsa/* (to test it all works)
- Buy me a coffee here buymeacoff.ee/robotzero 😉
Read the section Simplifying the Bluetooth Connection below to find out how to make the Pi Zero W connect to Bluetooth automatically when it starts up and how to shorten the play command.
If you are starting from scratch or need more detail then please read on…
Headless Install of Raspbian Stretch Lite
I find it much easier to remotely access the Pi from my PC so I can copy and paste commands rather than typing everything using a keyboard connected to the Pi. This is called a headless set up. It’s a little more work to start but makes things a lot easier later.
Download the Lite version here: https://www.raspberrypi.org/downloads/raspbian/ and unzip it.
Burn the img file to a microSD card using Etcher – https://etcher.io/
You need to make some changes to the boot partition of the microSD card. You might have to remove the card from your PC and then insert it again to see two partitions. The first one is the boot partition. Don’t format the card!
To allow the Pi to connect to your WiFi network and enable you to access it from your computer you need to create two files in the root folder – an empty file called ssh and a file called wpa_supplicant.conf
in the file wpa_supplicant.conf add your WiFi connection details following the example below. Change the country code, SSID and psk (wifi password) to your own.
psk="your wifi password"
Remove the microSD and insert it into the Pi Zero W and and power it up. Optionally, if you want to see the Pi booting you can connect it to an HDMI screen.
SSH Remote Access Setup
You will need a SSH client to access the Pi Zero from your computer. Here are some guides for different platforms: Windows, Linux and Mac, iOS or Android I use Putty on my PC and JuiceSSH on my Android phone.
You will also need the IP address of the Pi on your network. You should be able to see this in the control panel of your router or by using a network scan tool. More info here on scanning your network . Below you can see the IP address on my network of the Pi – 192.168.1.108
Follow the instructions for your chosen SSH client and connect to the Pi via its IP address. The login is pi and the password raspberry.
Adding A Bluetooth Connection
Before adding Bluetooth to the installation you need to update Raspbian. Copy and paste the following command into the SSH client. If you are new to Putty, note that you can right click to paste copied text into the client window.
sudo apt-get update && sudo apt-get dist-upgrade
This will take some time to complete.
To enable to connection for the sound over Bluetooth you need to install another package called BlueALSA
sudo apt-get install bluealsa
sudo service bluealsa start
Switch on your Bluetooth device so it’s in connect mode.
sudo bluetoothctl into the SSH client. You should see something like the following:
pair and then highlight the device id (mine is 7B:08:72:E8:DD:13) by holding the left mouse button and dragging over it and then right click to paste it in the current line
trust and right click to paste the ID into the current line
connect and right click to paste the ID into the current line
Exit from the bluetoothctl (type
exit) and paste the line below to quickly test things are working..
aplay -D bluealsa:HCI=hci0,DEV=7B:08:72:E8:DD:13,PROFILE=a2dp /usr/share/sounds/alsa/*
If you can hear the test recording continue with setting up…
Simplifying the Bluetooth Connection
To make it easier to use this connection, type or paste the following command..
In the text editor that opens, paste the text below but replace the device ID with your own
Press Ctrl+x to save the new file. Now try the following command
aplay -D bluealsa /usr/share/sounds/alsa/*
The result should be the same as above. Creating the .asoundrc file means you can just use
-D bluealsa rather than
-D bluealsa:HCI=hci0,DEV=7B:08:72:E8:DD:13,PROFILE=a2dp when you want a command to output using the Bluetooth audio.
The next step is to make sure the Bluetooth system starts up when the Pi Zero boots up.
The easiest way to do this is to run the following command:
sudo nano /etc/rc.local
to open the rc.local file with the nano text editor and add this line above the the ‘exit 0’ line:
echo 'connect 7B:08:72:E8:DD:13 \n quit' | bluetoothctl Replacing the device ID with your own
To check that the Bluetooth device connects when the Pi Zero boots up enter the command
While the PI Zero boots the bluetooth device light flashes when waiting for a connection. When the Pi has booted and connected to the Bluetooth device the led light should be constant.
Log into the Pi and double check it works with
aplay -D bluealsa /usr/share/sounds/alsa/*
Adding a Voice to Your Pi Zero
There are a few options for giving your Pi a voice. The easiest to start with is eSpeak. Install it with the command:
sudo apt-get install espeak
Then try the following two commands:
espeak "Hello. Your Raspberry Pi now has a man's voice" -ven-us+m3 -p40 -s120 --stdout | aplay -D bluealsa
espeak "Hello. Your Raspberry Pi now has a woman's voice" -ven+f3 -k5 -s130 --stdout | aplay -D bluealsa
Pico2wave has a nicer voice. Install with:
sudo apt-get install libttspico-utils
Try this command:
pico2wave -w temp.wav "Hello. My voice is nicer than the e Speak voice, don't you think?" && aplay -D bluealsa temp.wav
You can access the speech synthesis with Python using commands something like this:
I’m writing another blog post about this.
I’ve used the CT14 Bluetooth module for this project. It looks like it was developed for use in consumer Bluetooth speaker boxes and is now sold as a part for hobbyists. It’s available from lots of places:
eBay – https://www.ebay.com/itm/5W-5W-CT14-Micro-4-2-Stereo-Bluetooth-Power-Amplifier-Module-with-Charging-Port-/352245725379
AliExpress – https://es.aliexpress.com/item/CT14-Micro-4-2-est-reo-Bluetooth-amplificador-de-potencia-m-dulo-5VF-5-W-5/32848706849.html
Amazon – https://www.amazon.com/2X5W-Bluetooth-Stereo-Audio-Amplifier/dp/B0796TJ9FT
The speaker is this one – https://www.ebay.es/itm/2x-altavoces-4-ohmios-3W-amplificador-audio-hifi-estereo-speakers-buffer-166/272896246731?hash=item3f89e243cb:g:UzwAAOSwlptZ60UN but any 3W speaker will work fine.
I wouldn’t have been able to work this out without the information below
espeak examples – https://www.dexterindustries.com/howto/make-your-raspberry-pi-speak/
Pre-built PI Zero W IMG download
If you just want to quickly test it works with your device, I’ve created an image of Raspbian with everything ready to run from the command prompt on the Pi. You just need to run the sudo bluetoothctl, scan on, pair, trust and connect sequence to connect to your Bluetooth device. Download it from here and unzip it. Use Etcher to write the installation to a blank microSD card.
If I've saved you some time and you want to say 'Thanks!' you can buy me a coffee here...