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 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.

  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:HCI=hci0,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 Raspbian Stretch 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 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.

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: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..

nano ~/.asoundrc
In the text editor that opens, paste the text below but replace the device ID with your own

defaults.bluealsa.interface "hci0"
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: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 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. 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:

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

Here’s a video of the quick installation of Bluetooth as part of this project: https://youtu.be/2qDCxVRbge0?t=615

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:

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.

References

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

https://github.com/Arkq/bluez-alsa
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