Adding a USB Speaker to Raspberry Pi

I bought a simple USB speaker for my raspberry pi. I plugged it in and… it didnt work. Here is how to configure it.

Use arecord to record a sample, then use aplay to play it back and test the audio output. In my case the sound comes out the headphones but not the USB speaker.

arecord -Dac108 -f S32_LE -r 16000 -c 4 hello.wav
aplay hello.wav

In the home or root folder (e.g. home/pi/) there is file called .asoundrc which describes the audio configuration.

cd ~
sudo nano .asoundrc

This should bring up nano with a file like this:

pcm.!default {
  type asym
  playback.pcm {
    type plug
    slave.pcm "output"
  }
  capture.pcm {
    type plug
    slave.pcm "input"
  }
}

pcm.output {
  type hw
  card 0
}

ctl.!default {
  type hw
  card 0
}

Notice the card attributes are set to 0. Exit .asoundrc and then use aplay to list the available output devices

# Ctrl + X
aplay -l

You should see something like this…

pi@raspberrypi:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
  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 1: Device [USB2.0 Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Here it says card 0 is the headphones, and the USB is card 1. In the configuration file the cards are set to 0 i.e. the headphones.

We need to change asoundrc to use card 1. Go back into .asoundrc in nano and change it.

sudo nano .asoundrc
pcm.!default {
  type asym
  playback.pcm {
    type plug
    slave.pcm "output"
  }
  capture.pcm {
    type plug
    slave.pcm "input"
  }
}

pcm.output {
  type hw
  card 1
}

ctl.!default {
  type hw
  card 1
}

Exit Nano and save it, then test it again. Using aplay hello.wav. If there is still no sound try and change the volume using the alsamixer

aplay hello.wav
alsamixer

Install Home Assistant on a Raspberry Pi

First setup a basic raspberry pi. Then follow the steps in the manual home assistant installation.

In this case the commands on the home assistant guide did not install some packages, but we can install them manually.

sudo apt-get install libopenjp2-7-dev
sudo apt-get install libtiff-tools

After testing using the “hass” command and checking the page at the raspberry pi’s ip address + :8123, then need to set home assistant to run in the background as a daemon using another guide they provided.

Raspberry Pi Home Server

Setup a raspberry pi home server using Windows, raspberry pi OS Lite, balena etcher, WIFI and SSH. Get a basic local linux server running at home.

First we need a raspberry pi (3), an SD card (32GB+ recomended), an SD card reader (e.g. a USB SD card reader) and a power supply for the pi (USB power can be unstable).

Plug in the SD card to a Windows machine and format it using PowerShell. Type:

diskpart
list disk # Look for which disk number is the SD card using the details
select disk <disk_number_here>
detail disk # Check you have selected the right disk
clean # Wipe the disk clean
exit

Then open Windows disk management tool (e.g. search for disk management), right click the SD card and new volume. Use FAT32. Name the drive letter P for Pi

Then download balena etcher and the raspbian lite Linux distribution, and user balena etcher to burn raspbian onto the SD card. Remove the SD card when etcher has checked and confirmed it wrote successfully, then re-insert after a few seconds.

Go back into PowerShell and cd P:\ or change into whatever you named the SD card.

To enable SSH on first boot create type New-Item SSH. Then create a configuration file for the WIFI settings using New-Item wpa_supplicant.conf.

Open wpa_supplicant.conf with a command like code wpa_supplicant.conf or notepad wpa_supplicant.conf and then add this information where ssid is the name of your router and psk is the router password.

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
 ssid="YOURSSID"
 scan_ssid=1
 psk="YOURPASSWORD"
 key_mgmt=WPA-PSK
}

The SD card is now ready to go in the raspberry pi for first boot. After a few minutes it will show up on your router page. Log into the pi using ssh pi@raspberrypi password: raspberry or use pi@ipaddress e.g. pi@192.168.1.52

14. If we have used the pi on this network before and get this warning:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for raspberrypi has changed,
and the key for the corresponding IP address 192.168.0.27
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Then type ssh-keygen -R raspberrypi or ssh-keygen -R <pi-ip_address> to reset it, then try log in again.

Once logged in change the password using passwd command, and do a sudo apt-get update and then sudo apt-get -y upgrade to update the the raspberry pi.