Master the Art of Audio with Your Raspberry Pi Zero and a USB Speaker

Learn how to make your Raspberry Pi Zero speak with this step-by-step guide on connecting and playing sound through a USB speaker. …


Updated October 11, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to make your Raspberry Pi Zero speak with this step-by-step guide on connecting and playing sound through a USB speaker.

The Raspberry Pi Zero is an inexpensive, credit card-sized computer that’s perfect for various projects including home automation, media centers, and more. One of the many ways you can enhance your Raspberry Pi experience is by adding sound capabilities. This guide will show you how to connect a USB speaker to your Raspberry Pi Zero and play audio files using Python.

Materials Needed

  • Raspberry Pi Zero (any model)
  • Micro USB power supply
  • USB Speaker or Headphones
  • SD Card with Raspbian OS installed (we’ll be using Raspbian Stretch Lite in this guide)
  • MicroSD to USB Adapter
  • HDMI cable and monitor (optional, but recommended for setup)
  • Keyboard and mouse (or touchscreen display)
  • Ethernet cable or WiFi dongle (for internet access)

Setting up Your Raspberry Pi Zero

  1. Insert the SD card into your computer’s SD card reader and download the latest version of Raspbian Stretch Lite image.
  2. Use Etcher to flash the Raspbian image onto your SD card.
  3. Once the flashing is complete, safely remove the SD card from your computer and insert it into your Raspberry Pi Zero.
  4. Connect your Raspberry Pi Zero to a monitor using an HDMI cable (optional), keyboard, mouse, and Ethernet or WiFi dongle. Power on the device by connecting the micro USB power supply.
  5. Open a terminal window and run sudo raspi-config to access the Raspberry Pi configuration tool. Go to “Interfacing Options” > “USB” and enable the “USB Audio” option. Save and exit the configuration tool.

Connecting Your USB Speaker or Headphones

  1. Connect your USB speaker or headphones to one of the Raspberry Pi Zero’s USB ports.
  2. Verify that the device is detected by running sudo aplay -l in the terminal. You should see output similar to this:
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: USB [USB Audio Device], device 0: USB Audio [USB Audio Device]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

If you don’t see your speaker listed, make sure it is powered on and try a different USB port.

Installing the Required Packages

Run the following commands in the terminal to install the necessary packages for audio playback:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pygame alsa-utils

Playing Audio with Python

Python’s Pygame library makes it easy to play sound files on your Raspberry Pi Zero. Here’s a simple script that plays an audio file:

import pygame

pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound("your_audio_file.wav") # replace with the path to your audio file
channel = sound.play()
while channel.get_busy():
    continue

Replace your_audio_file.wav with the name and extension of an audio file in the same directory as your script. Save this code in a Python file (e.g., play_sound.py) and run it using python3 play_sound.py.

If you encounter any issues with volume or audio quality, try adjusting the mixer’s volume:

import pygame

pygame.init()
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5) # set volume level between 0.0 and 1.0
sound = pygame.mixer.Sound("your_audio_file.wav")
channel = sound.play()
while channel.get_busy():
    continue

You can also use the aplay command to play audio files directly from the terminal:

aplay -D plughw:1,0 your_audio_file.wav # replace with the path to your audio file

That’s it! You now have a Raspberry Pi Zero that can play sound through a USB speaker or headphones. Experiment with different audio files and enjoy the music.