Raspberry Pi Zero W Bluetooth Speaker Connection Voice

pi-zero-w-bluetooth-speaker.jpg

Connecting a Bluetooth speaker to a Raspberry Pi Zero W running Raspberry Pi OS 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 Raspberry Pi OS 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.

  1. sudo apt-get update && sudo apt-get dist-upgrade
  2. sudo apt-get install bluealsa
  3. sudo service bluealsa start
  4. Switch on your bluetooth device
  5. sudo bluetoothctl
  6. scan on
  7. pair XX:XX:XX:XX:XX:XX (replace the XXXXX with your device ID)
  8. trust XX:XX:XX:XX:XX:XX
  9. connect XX:XX:XX:XX:XX:XX
  10. exit
  11. aplay -D bluealsa:DEV=XX:XX:XX:XX:XX:XX,PROFILE=a2dp /usr/share/sounds/alsa/* (to test it all works)
  12. 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 Raspberry Pi OS Lite

Follow the instruction here if you don’t already have access to the Pi via SSH. This makes the following much easier.

Adding A Bluetooth Connection

Before adding Bluetooth to the installation you need to update Pi OS. 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.

Type sudo bluetoothctl into the SSH client. You should see something like the following:

bluetoothctl

Type scan on

Bluetooth Scan On

Type 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

Bluetooth Pairing

Type trust and right click to paste the ID into the current line

Bluetooth Trust

Type connect and right click to paste the ID into the current line

Bluetooth Connect

Exit from the bluetoothctl (type exit) and paste the line below to quickly test things are working..

aplay -D bluealsa:DEV=57:68:AC:88:32:9A,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..

nano ~/.asoundrc

In the text editor that opens, paste the text below but replace the device ID with your own

defaults.bluealsa.device "7B:08:72:E8:DD:13"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

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:DEV=7B:08:72:E8:DD:13,PROFILE=a2dp when you want a command to output using the Bluetooth audio.

If you want to switch on the Bluetooth device after the Pi has finished booting you don’t need to do the following because it will automatically connect.

If you want the Pi to connect to the Bluetooth device as the Pi boots (the Bluetooth device is on and waiting for connections) then complete the following steps.

Run the command:

sudo nano /etc/systemd/system/aplay.service

To create a new file with the nano text editor. Paste the following lines:

[Unit]
Description=BlueALSA aplay service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/bluealsa-aplay 7B:08:72:E8:DD:13

[Install]
WantedBy=multi-user.target

(Replace the device ID with your own)

To make the new script run on boot, run the command:

sudo systemctl enable aplay

For more information about this step see the Pi systemd docs here: https://www.raspberrypi.org/documentation/linux/usage/systemd.md

To check that the Bluetooth device connects when the Pi Zero boots up enter the command sudo reboot

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. For the Pi Zero an older version has to be installed to avoid an “Illegal instruction” error. Paste the following commands one at a time in the shell:

wget http://archive.raspberrypi.org/debian/pool/main/s/svox/libttspico-utils_1.0+git20130326-3+rpi1_armhf.deb

wget http://archive.raspberrypi.org/debian/pool/main/s/svox/libttspico0_1.0+git20130326-3+rpi1_armhf.deb

sudo apt-get install -f ./libttspico0_1.0+git20130326-3+rpi1_armhf.deb ./libttspico-utils_1.0+git20130326-3+rpi1_armhf.deb

The speech command looks like this:

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:

os.system("espeak 'Hello {}' -ven+f5 -k5 -s150 --stdout | aplay -D bluealsa".format(name))

This project uses this technique to give a voice to a face recognition system: Face Recognition Party Greeter

Items Used

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:

AliExpress – https://es.aliexpress.com/item/CT14-Micro-4-2-est-reo-Bluetooth-amplificador-de-potencia-m-dulo-5VF-5-W-5/32848706849.html

References

I wouldn’t have been able to work this out without the information below

https://github.com/Arkq/bluez-alsa
https://bugs.launchpad.net/raspbian/+bug/1835974
https://forum.armbian.com/topic/6480-bluealsa-bluetooth-audio-using-alsa-not-pulseaudio/
https://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis)#Cepstral_Text_to_Speech
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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top