Raspbian comes with a command called speaker-test which is the first thing I try when setting up audio. The picture below shows what it looks like with the audio correctly configured. More importantly is what it sounds like! If everything is working, you will hear static ("pink noise") from the USB speaker.  After listening for awhile you can press Control-C to end speaker-test.

So you might get lucky and find that your speaker just works, without any tweaking. I was not so lucky. When I initially ran speaker-test, it exited immediately with an error (and there was no sound).

If your speaker is already working, skip ahead to where I talk about the espeak command. Otherwise the next several steps will cover how I diagnosed and fixed the audio issue.

I found this StackExchange writeup about ALSA to be very helpful. One thing it recommends is looking at the files /proc/asound/modulesand /proc/asound/cards. The picture below shows how I used the catcommand to print out the contents of those files.

The picture above shows that 5 audio devices were detected! Why is this? Apparently CircuitPython devices register as audio devices, and in my case the first 3 devices listed are the NeoTrellisM4, the Gemma M0 and the CPX.

The device with index 3 is the real USB speaker. The last device ("bcm2835") is the audio system inside the Raspberry Pi which uses the rPi headphone jack.

The first device listed will be the default audio device, so in my case Raspbian was initially trying to use the NeoTrellis M4 as the speaker (and this failed speaker-test).

Another way to check the audio configuration is with the alsamixer command. The pictures here show that when I first ran it, the "Trellis M4 Express" card appeared first.

The second picture shows how you can press F6 to switch to a different sound card.

The final picture shows how I selected the USB speaker. This allows for controlling the volume (set at 50 in the picture) with the up and down arrow keys.

You can change the volume here and Raspbian will continue to use that setting after you exit alsamixer. However we cannot change the default sound card from here. I'll show how to do that in the next step.

In the above steps, I determined that my USB speaker is the device with index 3 in /proc/asound/cards. By default, Rasbian is using device 0 (the NeoTrellis M4). So I need to change the default value from 0 to 3... but where?

The answer to that question was in the StackExchange link. We can change the default audio card by editing this file: /usr/share/alsa/alsa.conf. This file is owned by the root user, so we need to launch an editor with sudo. You could do it with the nano editor like this:

sudo nano /usr/share/alsa/alsa.conf

Two lines need to be changed in the alsa.conf file.  Look for these 2 lines:

defaults.ctl.card 0

defaults.pcm.card 0

The picture below shows how I changed those values from 0 to 3 because my USB speaker was assigned to audio card 3.

After changing the 2 values in the alsa.conf file, reboot to allow the changed settings to be processed:

sudo reboot

After this reboot, try the speaker-test command again and you should hear some noise from the speaker.  You can run alsamixer again to adjust the volume.

Now, with all that setup done try this from the command line: espeak hello

You should hear your CompuCanvas say, "hello". You will also see a lot of error messages in the terminal window. If you are hearing sound, it's safe to ignore these messages. You can hide them in your command line invocations like this:

espeak "hello world" 2>/dev/null

The last bit (2>/dev/null) hides the error messages by sending them to the /dev/null device (sort of a digital trash can).

Below is a bash script I wrote that you can use to have your CompuCanvas announce itself and its IP address.

#!/bin/bash

function espeaker() {
  espeak -a 50 "$@" 2>/dev/null
}

IP_ADDR=`hostname -I`
while [ -z $IP_ADDR ]; do
  sleep 1
  IP_ADDR=`hostname -I`
done
ESPEAK_IP_ADDR=`echo $IP_ADDR | sed -e 's/\(.\)/\1 /g' | sed -e 's/\./dot/g'`

espeaker "Hello from Comp U Canvas"
sleep 2

espeaker "I P address"
espeaker -s 100 "$ESPEAK_IP_ADDR"
sleep 2

espeaker "repeating I P address"
espeaker -s 100 "$ESPEAK_IP_ADDR"

To try this out, use an editor to create a file for the script:

nano ~/cc-hello.sh

This will open nano on a new file named cc-hello.sh in your home directory. You can paste the above script into that file and save it. Then run the following to make the file executable:

chmod ugo+x ~/cc-hello.sh

Now try to run the script:

~/cc-hello.sh

The ~ in the commands above refers to the user's home directory, which is /home/pi. So the full path of this script is: /home/pi/cc-hello.sh.  I'm going to use that in the next (and final) step of this guide.

The last thing I want to show is how you can make the cc-hello.sh script run when the CompuCanvas boots up.  This is useful when taking the CompuCanvas to a new place (like a meetup), because you will need the IP address to login to the CompuCanvas.

There is probably more than one way to tie cc-hello.sh into the Raspbian boot sequence.  I do it by editing the file /etc/rc.local. That file is owned by the root user, so you'll need sudo to edit it, like this:

sudo nano /etc/rc.local

Notice that the very last line of this file has exit 0.  Add the line shown below just before the exit 0 line:

/home/pi/cc-hello.sh

Then save the changes, and sudo reboot one more time. When the CompuCanvas starts up again you should hear it announce its IP address.

This guide was first published on Dec 11, 2019. It was last updated on Mar 08, 2024.

This page (Audio Addendum) was last updated on Mar 08, 2024.

Text editor powered by tinymce.